Tanoda
pb_PrefabBrowserItemButton.cs
Go to the documentation of this file.
1using System;
2using UnityEngine;
3using UnityEngine.UI;
5using GILES;
6using System.Collections;
7using System.Collections.Generic;
8using System.Threading.Tasks;
9using TMPro;
10using TriLibCore;
11
12#if UNITY_EDITOR
13
14using UnityEditor;
15
16#endif
17
18using UnityEngine.Video;
19using Object = UnityEngine.Object;
20#if !UNITY_WEBGL
21using Valve.VR.InteractionSystem;
22#endif
23
24namespace GILES.Interface
25{
31 {
32 private const int PREVIEW_LAYER = 20;
33 private const int PREVIEW_VIDEO_LAYER = 21;
34 private const int PreviewWidth = 256;
35 private const int PreviewHeight = 256;
36 public string prefabId = "";
37 public string fullFilePath = "";
38 private static readonly Quaternion CAMERA_VIEW_ANGLE = Quaternion.Euler(30f, -30f, 0f);
39 public GameObject asset;
40 public RawImage previewImg;
41 public bool global = false;
42
43 public float cameraRotateSpeed = 50f;
44 private Quaternion cameraRotation = CAMERA_VIEW_ANGLE;
45 internal RawImage previewComponent;
46 private bool doSpin = false;
47 private Texture2D previewImage;
48 internal GameObject instance;
49 private Light[] sceneLights;
50 private bool[] lightWasEnabled = null;
51 private bool isPreview = false;
52 private bool inSetup = true;
53 private bool destroyAfterRender = false;
54 internal bool forceRender = false;
55 public GameObject PrefabGlobalFileBadge;
56
57 private Camera _previewCamera = null;
58
59 private Camera previewCamera
60 {
61 get
62 {
63 if (asset == null)
64 {
65 return null;
66 }
67 if (_previewCamera == null)
68 {
69 var cam = PreviewCamerasHolder.instance.holderTransform.Find("Prefab Browser Asset Preview Camera 4 " + asset.name);
70 if (cam)
71 {
72 _previewCamera = cam.GetComponent<Camera>();
73 }
74 else
75 {
76 _previewCamera = new GameObject().AddComponent<Camera>();
77 _previewCamera.gameObject.transform.SetParent(PreviewCamerasHolder.instance.holderTransform);
78 _previewCamera.gameObject.name = "Prefab Browser Asset Preview Camera 4 " + asset.name;
79 _previewCamera.cullingMask = 1 << PREVIEW_LAYER;
80 _previewCamera.transform.localRotation = CAMERA_VIEW_ANGLE;
81 _previewCamera.nearClipPlane = 0.01f;
82 _previewCamera.gameObject.SetActive(false);
83 }
84 }
85
86 return _previewCamera;
87 }
88 }
89
90 private static RenderTexture _renderTexture;
91
92 private static RenderTexture renderTexture
93 {
94 get
95 {
96 if (_renderTexture == null)
97 {
98
99 if (MiscLogicManager.instance.LowMemoryMode)
100 _renderTexture = new RenderTexture(16, 16, 0, RenderTextureFormat.Default, RenderTextureReadWrite.Default);
101 else
102 _renderTexture = new RenderTexture(PreviewWidth, PreviewHeight, 0, RenderTextureFormat.Default, RenderTextureReadWrite.Default);
103 _renderTexture.autoGenerateMips = false;
104 _renderTexture.useMipMap = false;
105 }
106
107 return _renderTexture;
108 }
109 }
110
111 private static Light _previewLightA = null;
112
113 private static Light previewLightA
114 {
115 get
116 {
117 if (_previewLightA == null)
118 {
119 GameObject go = new GameObject();
120 go.name = "Asset Preview Lighting";
121 go.transform.localRotation = Quaternion.Euler(15f, 330f, 0f);
122 _previewLightA = go.AddComponent<Light>();
123 _previewLightA.type = LightType.Directional;
124 _previewLightA.intensity = .5f;
125 }
126
127 return _previewLightA;
128 }
129 }
130
131 private static Light _previewLightB = null;
132
133 private static Light previewLightB
134 {
135 get
136 {
137 if (_previewLightB == null)
138 {
139 GameObject go = new GameObject();
140 go.name = "Asset Preview Lighting";
141 go.transform.localRotation = Quaternion.Euler(15f, 150f, 0f);
142 _previewLightB = go.AddComponent<Light>();
143 _previewLightB.type = LightType.Directional;
144 _previewLightB.intensity = .5f;
145 }
146
147 return _previewLightB;
148 }
149 }
150
155 protected override void Start()
156 {
157 base.Start();
158 if (!targetGraphic || global)
159 onClick.AddListener(OpenPropertiesWindow);
160 }
161
162 private void OpenPropertiesWindow()
163 {
164 doSpin = false;
165
166 PropertiesHelper.instance.OpenAsset(this);
167 forceRender = true;
168 }
169
170 internal void ForcePointerExit()
171 {
172 isPreview = false;
173 OnPointerExit(null);
174 isPreview = true;
175 }
176
177 private string st = "";
178
179 public void Initialize(bool previewOnly = false)
180 {
181 isPreview = previewOnly;
182 if (isPreview)
183 {
184 DestroyImmediate(instance);
185 }
186
187 prefabId = asset.DemandComponent<pb_MetaDataComponent>().GetFileId();
188
189
190 if (MiscLogicManager.instance.LowMemoryMode)
191 previewImage = new Texture2D(16, 16);
192 else
193 previewImage = new Texture2D(PreviewWidth, PreviewHeight);
194
195 //DEBUG FAST LOAD, TODO: fix this
196 //if (!SetupAndRenderPreview(previewImage))
197 //{
198 // pb_ObjectUtility.Destroy(previewImage);
199 // previewImage = null;
200 //}
201 bool nullImage = false;
202 if (!HasPreviewImage())
203 {
204 pb_ObjectUtility.Destroy(previewImage);
205 nullImage = true;
206 }
207
208
209 if (!previewOnly)
210 {
211 //gameObject.AddComponent<Mask>();
212 gameObject.AddComponent<VerticalLayoutGroup>();
213 Image image = gameObject.DemandComponent<Image>();
214 image.color = new Color(1f,1f,1f,25f/255f);
215 image.sprite = null;
216 //StartCoroutine(DelayVerticalSet());
217 //image.enabled = false;
218 }
219
220 GameObject description = gameObject.AddChild();
221
222
223 if (!nullImage)
224 {
225 previewComponent = previewImg == null ? description.AddComponent<RawImage>() : previewImg;
226 if (previewImg != null)
227 {
228 Destroy(description);
229 }
230 previewComponent.texture = previewImage;
231 if (!isPreview)
232 gameObject.AddComponent<DragAndDropItem>();
233 FormattingComponents();
234 }
235 else
236 {
237 if (asset.GetComponent<RawImage>())
238 {
239 previewComponent = previewImg == null ? description.AddComponent<RawImage>() : previewImg;
240 if (previewImg != null)
241 {
242 Destroy(description);
243 }
244#if DANA
245 previewComponent.texture = GlobalTextureHolder.instance.GetLowRes(asset.name);
246#else
247 previewComponent.texture = asset.GetComponent<RawImage>().texture;
248#endif
249 SizeToParent(previewComponent, 0.05f);
250 FormattingComponents();
251 if (!isPreview)
252 gameObject.AddComponent<DragAndDropItem>();
253
254 if (asset.GetComponent<VideoPlayer>())
255 RenderVideoPreview();
256 }
257 else
258 {
259 Text text = description.AddComponent<Text>();
260 text.font = pb_GUIUtility.DefaultFont();
261 text.alignment = TextAnchor.MiddleCenter;
262 text.text = asset.name/*.ToUpper()*/;
263 instance = (GameObject)GameObject.Instantiate(asset, Vector3.zero, Quaternion.identity);
264 MoveToLayer(instance.transform, PREVIEW_LAYER);
265 instance.SetActive(false);
266 }
267 }
268 if (previewComponent)
269 PreviewGenerator.Instance?.AddWork(asset, previewComponent);
271 }
272
273 public static Vector2 SizeToParent(RawImage image, float padding = 0)
274 {
275 float w = 0, h = 0;
276 var parent = image.transform.parent.GetComponent<RectTransform>();
277 var imageTransform = image.GetComponent<RectTransform>();
278
279 // check if there is something to do
280 if (image.texture != null)
281 {
282 if (!parent) return imageTransform.sizeDelta; //if we don't have a parent, just return our current width;
283 padding = 1 - padding;
284 float ratio = image.texture.width / (float)image.texture.height;
285 var bounds = new Rect(0, 0, parent.rect.width, parent.rect.height);
286 if (Mathf.RoundToInt(imageTransform.eulerAngles.z) % 180 == 90)
287 {
288 //Invert the bounds if the image is rotated
289 bounds.size = new Vector2(bounds.height, bounds.width);
290 }
291 //Size by height first
292 h = bounds.height * padding;
293 w = h * ratio;
294 if (w > bounds.width * padding)
295 { //If it doesn't fit, fallback to width;
296 w = bounds.width * padding;
297 h = w / ratio;
298 }
299 }
300 imageTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, h);
301 imageTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, h);
302 return imageTransform.sizeDelta;
303 }
304
305 protected override void OnDestroy()
306 {
307 if (instance)
308 pb_ObjectUtility.Destroy(instance);
309 if (_previewCamera)
310 Destroy(_previewCamera.gameObject);
311
312 base.OnDestroy();
313 }
314
315#if !UNITY_WEBGL
316
317 private void Update()
318 {
319 if (MiscLogicManager.instance.LowMemoryMode)
320 return;
321
322 if (doSpin/* || isPreview*/ || forceRender)
323 {
324 previewCamera?.transform.RotateAround(Vector3.one * 100, Vector3.up, cameraRotateSpeed * Time.deltaTime);
325 RenderPreview();
326 }
327 }
328
329#endif
330
331 private bool IsPicture(string ext)
332 {
333 ext = ext.ToLower();
334
335 return ext == "png" || ext == "jpg" || ext == "jpeg" || ext == "bmp";
336 }
337
342 public void Instantiate()
343 {
344 forceRender = false;
345 Camera cam = Camera.main;
346 GameObject go;
347
348 Vector3 org = pb_Selection.activeGameObject == null ? Vector3.zero : pb_Selection.activeGameObject.transform.position;
349 Vector3 nrm = pb_Selection.activeGameObject == null ? Vector3.up : pb_Selection.activeGameObject.transform.localRotation * Vector3.up;
350
351 Plane plane = new Plane(nrm, org);
352
353 Ray ray = new Ray(cam.transform.position, cam.transform.forward);
354
355 float hit = 0f;
356
357 var spawn = GameObject.Find("SpawnPoint");
358
359 if (!spawn)
360 {
361 throw new Exception("SpawnPoint not found!");
362 }
363
364 if (SpawningPOI.instance.NextSpawnIsPOI)
365 {
366 go = (GameObject)pb_Scene.Instantiate(asset, SpawningPOI.instance.SpawnParent.position, Quaternion.identity, SpawningPOI.instance.SpawnParent);
367 SpawningPOI.instance.NextSpawnIsPOI = false;
368 }
369 else
370 {
371 if (spawn)
372 {
373 go = (GameObject)pb_Scene.Instantiate(asset, spawn.transform.position, Quaternion.identity, pb_Scene.instance.transform);
374 }
375 else //should not run
376 {
377 if (plane.Raycast(ray, out hit))
378 go = (GameObject)pb_Scene.Instantiate(asset, pb_Snap.Snap(ray.GetPoint(hit), .25f), Quaternion.identity, pb_Scene.instance.transform);
379 else
380 go = (GameObject)pb_Scene.Instantiate(asset, pb_Snap.Snap(ray.GetPoint(10f), .25f), Quaternion.identity, pb_Scene.instance.transform);
381 }
382 }
383
384 if (go.GetComponent<AssetUnloader>())
385 {
386 DestroyImmediate(go.GetComponent<AssetUnloader>());
387 }
388
389 int namedLikeThis = 0;
390 for (int i = 0; i < pb_Scene.instance.transform.childCount; i++)
391 {
392 if (pb_Scene.instance.transform.GetChild(i).name == go.name.Replace("(Clone)", ""))
393 {
394 namedLikeThis++;
395 }
396 }
397 if (namedLikeThis > 0)
398 {
399 go.DemandComponent<RenameObject>().NewName = go.name.Replace("(Clone)", "_") + namedLikeThis;
400 }
401 if (go.GetComponent<RawImage>())
402 {
404 go.GetComponent<RawImage>().texture = GlobalTextureHolder.instance.GetFullRes(asset.name);
405 else
406 Debug.LogWarning("GlobalTextureHolder does NOT have image: " + asset.name);
407 }
408 go.name = go.name.Replace("(Clone)", "");
409 go.AddComponent<StaticGO>();
410 go.AddComponent<Rigidbody>();
411 go.GetComponent<Rigidbody>().isKinematic = true;
412#if !UNITY_WEBGL
413 go.AddComponent<Interactable>();
414 go.AddComponent<ThrowableCanDisable>();
415 go.GetComponent<ThrowableCanDisable>().attachmentFlags = Hand.AttachmentFlags.ParentToHand | Hand.AttachmentFlags.TurnOnKinematic;
416#endif
417 foreach (var rb in go.GetComponentsInChildren<Rigidbody>())
418 {
419 rb.collisionDetectionMode = CollisionDetectionMode.Discrete;
420 rb.isKinematic = true;
421 //rb.detectCollisions = false;
422 rb.useGravity = false;
423 }
424
425 Undo.RegisterStates(new List<IUndo>() { new UndoSelection(), new UndoInstantiate(go) }, "Create new object");
426
428
429 pb_Hierarchy.instance.RebuildInspector();
430
431 bool curSelection = pb_Selection.activeGameObject != null;
432
433 if (!curSelection)
435
436 if (true || IsPicture(PropertiesHelper.instance.fileType.text)) //always close
437 {
438 if (this.name.Contains("Properties"))
439 {
441#if !UNITY_WEBGL
442 gameObject.SetActive(false);
443#endif
444 }
445 }
446
447 if (go.GetComponentInChildren<MeshCollider>())
448 {
449 var bounds = Macro.GetBounds(go);
450 if (bounds.size.x >= 5 || bounds.size.y >= 5)
451 {
452 go.transform.localScale /= (bounds.size.x) * 10;
453 }
454 if (bounds.size.x <= 0.05f|| bounds.size.y <= 0.05f)
455 {
456 go.transform.localScale *= 5;
457 }
458 }
459
460 }
461
467 {
468 forceRender = false;
469 var spawn = GameObject.Find("ModelEditorRoot");
470
471 if (!spawn)
472 {
473 throw new Exception("SpawnPoint not found!");
474 }
475
476 var go = (GameObject)pb_Scene.Instantiate(asset, spawn.transform.position, Quaternion.identity, spawn.transform);
477
478 if (go.GetComponent<AssetUnloader>())
479 {
480 DestroyImmediate(go.GetComponent<AssetUnloader>());
481 }
482
483 go.name = go.name.Replace("(Clone)", "");
484 go.GetComponentInChildren<MeshCollider>().enabled = false;
485 go.GetComponentInChildren<MeshRenderer>().gameObject.AddComponent<MeshInfo>();
486
487 foreach (var rb in go.GetComponentsInChildren<Rigidbody>())
488 {
489 rb.collisionDetectionMode = CollisionDetectionMode.Discrete;
490 rb.isKinematic = true;
491 rb.detectCollisions = false;
492 rb.useGravity = false;
493 }
494
496
497 var mr = go.GetComponentInChildren<MeshRenderer>();
498 Vector3 objectSizes = mr.bounds.max - mr.bounds.min;
499 float objectSize = Mathf.Max(objectSizes.x, objectSizes.y, objectSizes.z);
500 float cameraView = 2.0f * Mathf.Tan(0.5f * Mathf.Deg2Rad * Camera.main.fieldOfView); // Visible height 1 meter in front
501 float distance = 2.0f * objectSize / cameraView;
502 distance += 0.5f * objectSize;
503 pb_SceneCamera.Focus(go.transform.position, distance);
506
507 pb_InputManager.instance.currentEditor.skipOnGUI = true;
508 }
509
510 public override void OnPointerEnter(PointerEventData eventData)
511 {
512 if (previewComponent)
513 PreviewGenerator.Instance?.RenderSpin(asset, previewComponent);
514 return;
515 if (doSpin)
516 return;
517
518 if (!isPreview || targetGraphic)
519 {
520 PropertiesHelper.instance.preview.ForcePointerExit();
521 }
522 if (previewComponent == null)
523 return;
524
525 if (PropertiesHelper.instance.propertiesWindow.activeSelf &&
526 gameObject != PropertiesHelper.instance.propertiesWindow)
527 return;
528
529 if (previewImage == null)
530 {
531 if (instance != null && instance.GetComponent<VideoPlayer>())
532 {
533 instance.SetActive(true);
534 instance.GetComponent<VideoPlayer>().Play();
535 }
536 }
537#if UNITY_WEBGL
538 return;
539#endif
540
541 if (MiscLogicManager.instance.LowMemoryMode)
542 return;
543
544 if (!SetupPreviewRender())
545 return;
546
547 previewComponent.texture = renderTexture;
548
549 doSpin = true;
550 }
551
552 public override void OnPointerExit(PointerEventData eventData)
553 {
555 return;
556 if (isPreview && eventData != null) return;
557
558 if (PropertiesHelper.instance.propertiesWindow.activeSelf &&
559 gameObject != PropertiesHelper.instance.propertiesWindow)
560 return;
561
562 if (previewImage == null)
563 {
564 if (instance != null && instance.GetComponent<VideoPlayer>())
565 {
566 instance.GetComponent<VideoPlayer>().Pause();
567 instance.SetActive(false);
568 }
569 return;
570 }
571
572 if (previewComponent == null)
573 return;
574
575#if UNITY_WEBGL
576 return;
577#endif
578 if (gameObject != PropertiesHelper.instance.propertiesWindow)
579 StartCoroutine(StopSpinAfterAFrame());
580
581 cameraRotation = previewCamera.transform.localRotation;
582
583 RenderPreview();
584
585 //previewImage.ReadPixels(new Rect(0,0,renderTexture.width,renderTexture.height), 0, 0);
586 //previewImage.Apply();
587 //
588 //previewComponent.texture = previewImage;
589 //
590 //RenderTexture.active = null;
591 //
592 //renderTexture.DiscardContents();
593 //renderTexture.Release();
594 //
596 //
597 //pb_ObjectUtility.Destroy(instance);
598 }
599
600 private IEnumerator StopSpinAfterAFrame()
601 {
602 yield return new WaitForEndOfFrame();
603 doSpin = false;
604 }
605
606 private static readonly object key = new object();
607
608 private void OnPostRenderCam(Camera camera)
609 {
610 return;
611 lock (key)
612 {
613 //if (!inSetup && previewImage && camera == previewCamera && renderTexture && (doSpin || isPreview) && instance && instance.activeSelf)
614 if (camera == previewCamera && previewComponent && previewImage && renderTexture && instance && instance.activeSelf)
615 {
616 try
617 {
618 previewImage.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
619 }
620 catch (Exception e)
621 {
622 Debug.Log("ejj de jó vagy: " + e);
623 return;
624 }
625
626 previewImage.Apply();
627
628 previewComponent.texture = previewImage;
629 //if (PropertiesHelper.instance.propertiesWindow.activeSelf && gameObject != PropertiesHelper.instance.propertiesWindow)
630 //{
631 // Camera.onPostRender -= OnPostRenderCam;
632 // pb_ObjectUtility.Destroy(instance);
633 // return;
634 //}
635 RenderTexture.active = null;
636
637 renderTexture.DiscardContents();
638 renderTexture.Release();
639 if (Camera.onPostRender != null) Camera.onPostRender -= OnPostRenderCam;
640 if (destroyAfterRender)
641 {
642 pb_ObjectUtility.Destroy(instance);
643 destroyAfterRender = false;
644 }
645 }
646 }
647 }
648
649 private IEnumerator ReadPixelsWOError()
650 {
651 yield return new WaitForEndOfFrame();
652 doSpin = false;
653
654 cameraRotation = previewCamera.transform.localRotation;
655
656 RenderPreview();
657
658 previewImage.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
659 previewImage.Apply();
660
661 previewComponent.texture = previewImage;
662
663 RenderTexture.active = null;
664
665 renderTexture.DiscardContents();
666 renderTexture.Release();
667 }
668
669 private bool HasPreviewImage()
670 {
671 if (asset.GetComponentInChildren<Renderer>() == null) return false;
672 if (asset.GetComponentInChildren<TextMeshPro>()) return false;
673
674 return true;
675 }
676 private bool SetupAndRenderPreview(Texture2D texture)
677 {
678 if (!SetupPreviewRender())
679 return false;
680 inSetup = true;
681 RenderPreview();
682
683 texture.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
684 texture.Apply();
685
686 RenderTexture.active = null;
687
688 renderTexture.DiscardContents();
689 renderTexture.Release();
690 inSetup = false;
691 pb_ObjectUtility.Destroy(instance);
692
693 return true;
694 }
695
696 private bool SetupPreviewRender()
697 {
698 if (MiscLogicManager.instance.LowMemoryMode)
699 return false;
700 if (asset.GetComponentInChildren<Renderer>() == null) return false;
701 if (asset.GetComponentInChildren<TextMeshPro>()) return false;
702
703 sceneLights = Object.FindObjectsOfType<Light>();
704 lightWasEnabled = new bool[sceneLights.Length];
705 bool ok = true;
706 if (!instance)
707 {
708 instance = (GameObject)GameObject.Instantiate(asset, Vector3.one * 100, Quaternion.identity);
709 ok = false;
710 }
711 if (instance.GetComponent<AssetUnloader>())
712 {
713 DestroyImmediate(instance.GetComponent<AssetUnloader>());
714 }
715
716 Renderer renderer = instance.GetComponentInChildren<Renderer>();
717
718 if (!renderer)
719 return false;
720
721 if (!ok)
722 instance.transform.position = -renderer.bounds.center;
723
724 MoveToLayer(instance.transform, PREVIEW_LAYER);
725
726 previewCamera.transform.localRotation = cameraRotation;
727
729 pb_AssetPreview.PrepareCamera(previewCamera, instance, PreviewWidth, PreviewHeight);
730
731 previewCamera.targetTexture = renderTexture;
732 return true;
733 }
734
735 private void MoveToLayer(Transform root, int layer)
736 {
737 root.gameObject.layer = layer;
738 foreach (Transform child in root)
739 MoveToLayer(child, layer);
740 }
741 private void FormattingComponents()
742 {
743 var cellSize = GetComponentInParent<pb_GridLayoutGroup>().cellSize;
744 var vlg = gameObject.GetComponent<VerticalLayoutGroup>();
745 var rt = GetComponent<RectTransform>();
746 vlg.padding.right = (int)(cellSize.x - cellSize.y);
747 //StartCoroutine(DelayTextFormatting());
748
749 }
750 private void topLeft(GameObject uiObject)
751 {
752 RectTransform uitransform = uiObject.GetComponent<RectTransform>();
753
754 uitransform.anchorMin = new Vector2(0, 1);
755 uitransform.anchorMax = new Vector2(0, 1);
756 uitransform.pivot = new Vector2(0, 1);
757 }
758 private void RenderPreview()
759 {
760 if (!asset)
761 return;
762
763 if (MiscLogicManager.instance.LowMemoryMode)
764 return;
765
766 if (asset.GetComponent<VideoPlayer>())
767 {
768 if (instance == null)
769 {
770 instance = (GameObject)GameObject.Instantiate(asset, Vector3.zero, Quaternion.identity);
771 if (instance.GetComponent<AssetUnloader>())
772 {
773 DestroyImmediate(instance.GetComponent<AssetUnloader>());
774 }
775
776 MoveToLayer(instance.transform, PREVIEW_LAYER);
777 }
778 return;
779 }
780
781 if (!instance)
782 {
783 instance = (GameObject)GameObject.Instantiate(asset, Vector3.zero, Quaternion.identity);
784 Destroy(instance.GetComponentInChildren<Collider>());
785 Renderer renderer = instance.GetComponentInChildren<Renderer>();
786 if (!renderer)
787 return;
788 instance.transform.position = -renderer.bounds.center;
789 instance.SetActive(true);
790 }
791
792 if (sceneLights != null)
793 for (int i = 0; i < sceneLights.Length; i++)
794 {
795 lightWasEnabled[i] = sceneLights[i].enabled;
796 sceneLights[i].enabled = false;
797 }
798
799 RenderTexture.active = renderTexture;
800
801 instance.SetActive(true);
802
803 previewLightA.gameObject.SetActive(true);
804 previewLightB.gameObject.SetActive(true);
805
806 if (!isPreview)
807 Camera.onPostRender += OnPostRenderCam;
808
809 previewCamera.Render();
810
811 if (isPreview)
812 {
813 previewImage.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
814 previewImage.Apply();
815
816 if (!previewComponent)
817 {
818 previewComponent = previewImg;
819 }
820 previewComponent.texture = previewImage;
821
822 RenderTexture.active = null;
823
824 renderTexture.DiscardContents();
825 renderTexture.Release();
826 }
827
828 instance.SetActive(false);
829
830 previewLightA.gameObject.SetActive(false);
831 previewLightB.gameObject.SetActive(false);
832
833 if (sceneLights != null)
834 for (int i = 0; i < sceneLights.Length; i++)
835 {
836 sceneLights[i].enabled = lightWasEnabled[i];
837 }
838 }
839
840 private int _stopafterframe = 2;
841
842 private void RenderVideoPreview()
843 {
844 if (asset.GetComponent<VideoPlayer>())
845 {
846 if (instance == null)
847 {
848 instance = (GameObject)GameObject.Instantiate(asset, Vector3.zero, Quaternion.identity);
849 MoveToLayer(instance.transform, PREVIEW_VIDEO_LAYER);
850 var video = instance.GetComponent<VideoPlayer>();
851 video.audioOutputMode = VideoAudioOutputMode.None;
852 video.sendFrameReadyEvents = true;
853 //video.playbackSpeed = 0.2f/*0.0625f*/; //WebGL minimum limit
854 video.frameReady += Pb_PrefabBrowserItemButton_frameReady;
855 }
856 }
857 }
858
859 private void Pb_PrefabBrowserItemButton_frameReady(VideoPlayer source, long frameIdx)
860 {
861 _stopafterframe--;
862 if (_stopafterframe <= 0)
863 {
864 source.sendFrameReadyEvents = false;
865 source.frameReady -= Pb_PrefabBrowserItemButton_frameReady;
866 source.playbackSpeed = 1;
867 source.Stop();
868 instance.SetActive(false);
869 }
870 }
871
872 public void MakeGlobal()
873 {
874 if (asset)
875 {
876 var uf = asset.GetComponent<UploadedFile>();
877 uf.global = true;
879 //Instantiate(PrefabGlobalFileBadge, transform);
880 }
881 }
882 }
883
884#if UNITY_EDITOR
885
886 [CustomEditor(typeof(pb_PrefabBrowserItemButton))]
887 public class pb_PrefabBrowserItemButtonEditor : Editor
888 {
889 public override void OnInspectorGUI()
890 {
891 base.OnInspectorGUI();
892 if (GUILayout.Button("Initialize"))
893 {
894 (target as pb_PrefabBrowserItemButton).Initialize(true);
895 }
896 }
897 }
898
899#endif
900}
UnityEngine.UI.Button Button
Definition: Pointer.cs:7
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
System.Drawing.Image Image
Definition: TestScript.cs:37
UnityEngine.Color Color
Definition: TestScript.cs:32
void SwitchToModelEditor()
static CanvasManager instance
Drag and Drop item.
void StartLateRebuild(bool force=false)
static FileDragAndDrop instance
static pb_PrefabBrowser instance
static Vector2 SizeToParent(RawImage image, float padding=0)
override void OnPointerExit(PointerEventData eventData)
override void OnPointerEnter(PointerEventData eventData)
static void RegisterStates(IEnumerable< IUndo > targets, string message)
Definition: Undo.cs:226
static void Focus(Vector3 target)
static UnityEngine.GameObject Instantiate(UnityEngine.GameObject original)
Definition: pb_Scene.cs:133
static void SetSelection(IEnumerable< GameObject > selection)
Definition: pb_Selection.cs:92
static void Clear()
Definition: pb_Selection.cs:61
static GameObject activeGameObject
Definition: pb_Selection.cs:82
bool HasTexture(string filename)
Texture2D GetLowRes(string filename)
Texture2D GetFullRes(string filename)
static GlobalTextureHolder instance
Definition: Macro.cs:12
static Bounds GetBounds(GameObject go)
Definition: Macro.cs:482
static ModelEditorLogicManager instance
static PreviewCamerasHolder instance
void AddWork(GameObject asset, RawImage t)
void RenderSpin(GameObject asset, RawImage previewComponent)
static PreviewGenerator Instance
Represents a Class to destroy every Asset (Textures, Materials, Meshes) loaded by TriLib for this Gam...
Definition: AssetUnloader.cs:7
UnityEngine.Object Object