3
3
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
4
4
5
5
using System ;
6
- using System . Linq ;
7
- using UnityEngine ;
8
6
using System . Collections . Generic ;
9
- using UnityEditorInternal ;
10
- using UnityEditor . VersionControl ;
7
+ using System . Linq ;
11
8
using UnityEditor . SceneManagement ;
12
- using Object = UnityEngine . Object ;
9
+ using UnityEditor . VersionControl ;
10
+ using UnityEditorInternal ;
11
+ using UnityEngine ;
12
+
13
+ using UnityObject = UnityEngine . Object ;
13
14
14
15
namespace UnityEditor
15
16
{
@@ -71,8 +72,39 @@ public Styles()
71
72
const float kIconSize = 24 ;
72
73
Vector2 previewDir ;
73
74
74
- PreviewRenderUtility m_PreviewUtility ;
75
- List < GameObject > m_PreviewInstances ;
75
+ class PreviewData : IDisposable
76
+ {
77
+ bool m_Disposed ;
78
+ GameObject m_GameObject ;
79
+
80
+ public readonly PreviewRenderUtility renderUtility ;
81
+ public GameObject gameObject { get { return m_GameObject ; } }
82
+
83
+ public PreviewData ( UnityObject targetObject )
84
+ {
85
+ renderUtility = new PreviewRenderUtility ( ) ;
86
+ renderUtility . camera . fieldOfView = 30.0f ;
87
+ UpdateGameObject ( targetObject ) ;
88
+ }
89
+
90
+ public void UpdateGameObject ( UnityObject targetObject )
91
+ {
92
+ UnityObject . DestroyImmediate ( gameObject ) ;
93
+ m_GameObject = EditorUtility . InstantiateForAnimatorPreview ( targetObject ) ;
94
+ renderUtility . AddManagedGO ( gameObject ) ;
95
+ }
96
+
97
+ public void Dispose ( )
98
+ {
99
+ if ( m_Disposed )
100
+ return ;
101
+ renderUtility . Cleanup ( ) ;
102
+ UnityObject . DestroyImmediate ( gameObject ) ;
103
+ m_Disposed = true ;
104
+ }
105
+ }
106
+
107
+ Dictionary < int , PreviewData > m_PreviewInstances = new Dictionary < int , PreviewData > ( ) ;
76
108
77
109
bool m_HasInstance = false ;
78
110
bool m_AllOfSamePrefabType = true ;
@@ -281,7 +313,7 @@ private void DoPrefabButtons(PrefabType prefabType, GameObject go)
281
313
{
282
314
if ( GUILayout . Button ( "Revert" , "MiniButtonMid" ) )
283
315
{
284
- List < Object > hierarchy = new List < Object > ( ) ;
316
+ List < UnityObject > hierarchy = new List < UnityObject > ( ) ;
285
317
GetObjectListFromHierarchy ( hierarchy , go ) ;
286
318
287
319
Undo . RegisterFullObjectHierarchyUndo ( go , "Revert to prefab" ) ;
@@ -290,7 +322,7 @@ private void DoPrefabButtons(PrefabType prefabType, GameObject go)
290
322
PrefabUtility . RevertPrefabInstance ( go ) ;
291
323
CalculatePrefabStatus ( ) ;
292
324
293
- List < Object > newHierarchy = new List < Object > ( ) ;
325
+ List < UnityObject > newHierarchy = new List < UnityObject > ( ) ;
294
326
GetObjectListFromHierarchy ( newHierarchy , go ) ;
295
327
RegisterNewComponents ( newHierarchy , hierarchy ) ;
296
328
}
@@ -316,7 +348,7 @@ private void DoPrefabButtons(PrefabType prefabType, GameObject go)
316
348
317
349
if ( GUILayout . Button ( "Apply" , "MiniButtonRight" ) )
318
350
{
319
- Object prefabParent = PrefabUtility . GetPrefabParent ( rootUploadGameObject ) ;
351
+ UnityObject prefabParent = PrefabUtility . GetPrefabParent ( rootUploadGameObject ) ;
320
352
string prefabAssetPath = AssetDatabase . GetAssetPath ( prefabParent ) ;
321
353
322
354
bool editablePrefab = Provider . PromptAndCheckoutIfNeeded (
@@ -362,22 +394,22 @@ private void DoPrefabButtons(PrefabType prefabType, GameObject go)
362
394
public void RevertAndCheckForNewComponents ( GameObject gameObject )
363
395
{
364
396
// Take a snapshot of the GO hierarchy before the revert
365
- var hierarchy = new List < Object > ( ) ;
397
+ var hierarchy = new List < UnityObject > ( ) ;
366
398
GetObjectListFromHierarchy ( hierarchy , gameObject ) ;
367
399
368
400
Undo . RegisterFullObjectHierarchyUndo ( gameObject , "Revert Prefab Instance" ) ;
369
401
PrefabUtility . RevertPrefabInstance ( gameObject ) ;
370
402
CalculatePrefabStatus ( ) ;
371
403
372
404
// Take a snapshot of the GO hierarchy after the revert
373
- var newHierarchy = new List < Object > ( ) ;
405
+ var newHierarchy = new List < UnityObject > ( ) ;
374
406
GetObjectListFromHierarchy ( newHierarchy , gameObject ) ;
375
407
376
408
// Add RegisterCreatedObjectUndo for any new components added during the revert so that they are removed if undo is triggered
377
409
RegisterNewComponents ( newHierarchy , hierarchy ) ;
378
410
}
379
411
380
- private void GetObjectListFromHierarchy ( List < Object > hierarchy , GameObject gameObject )
412
+ private void GetObjectListFromHierarchy ( List < UnityObject > hierarchy , GameObject gameObject )
381
413
{
382
414
Transform transform = null ;
383
415
List < Component > components = new List < Component > ( ) ;
@@ -404,7 +436,7 @@ private void GetObjectListFromHierarchy(List<Object> hierarchy, GameObject gameO
404
436
}
405
437
}
406
438
407
- private void RegisterNewComponents ( List < Object > newHierarchy , List < Object > hierarchy )
439
+ private void RegisterNewComponents ( List < UnityObject > newHierarchy , List < UnityObject > hierarchy )
408
440
{
409
441
var danglingComponents = new List < Component > ( ) ;
410
442
@@ -507,7 +539,7 @@ private void DoTagsField(GameObject go)
507
539
{
508
540
m_Tag . stringValue = tag ;
509
541
Undo . RecordObjects ( targets , "Change Tag of " + targetTitle ) ;
510
- foreach ( Object obj in targets )
542
+ foreach ( UnityObject obj in targets )
511
543
( obj as GameObject ) . tag = tag ;
512
544
}
513
545
EditorGUI . EndProperty ( ) ;
@@ -558,55 +590,49 @@ private void DoStaticToggleField(GameObject go)
558
590
EditorGUI . EndProperty ( ) ;
559
591
}
560
592
561
- Object [ ] GetObjects ( bool includeChildren )
593
+ UnityObject [ ] GetObjects ( bool includeChildren )
562
594
{
563
595
return SceneModeUtility . GetObjects ( targets , includeChildren ) ;
564
596
}
565
597
566
598
void SetLayer ( int layer , bool includeChildren )
567
599
{
568
- Object [ ] objects = GetObjects ( includeChildren ) ;
600
+ UnityObject [ ] objects = GetObjects ( includeChildren ) ;
569
601
Undo . RecordObjects ( objects , "Change Layer of " + targetTitle ) ;
570
602
foreach ( GameObject go in objects )
571
603
go . layer = layer ;
572
604
}
573
605
574
606
public override void ReloadPreviewInstances ( )
575
607
{
576
- CreatePreviewInstances ( ) ;
577
- }
578
-
579
- void CreatePreviewInstances ( )
580
- {
581
- if ( m_PreviewInstances == null )
582
- m_PreviewInstances = new List < GameObject > ( targets . Length ) ;
583
-
584
- for ( int i = 0 ; i < targets . Length ; ++ i )
608
+ foreach ( var pair in m_PreviewInstances )
585
609
{
586
- GameObject instance = EditorUtility . InstantiateForAnimatorPreview ( targets [ i ] ) ;
587
- m_PreviewInstances . Add ( instance ) ;
588
- m_PreviewUtility . AddSingleGO ( instance ) ;
610
+ var index = pair . Key ;
611
+ if ( index > targets . Length )
612
+ continue ;
613
+
614
+ var previewData = pair . Value ;
615
+ previewData . UpdateGameObject ( targets [ index ] ) ;
589
616
}
590
617
}
591
618
592
- void InitPreview ( )
619
+ PreviewData GetPreviewData ( )
593
620
{
594
- if ( m_PreviewUtility == null )
621
+ PreviewData previewData ;
622
+ if ( ! m_PreviewInstances . TryGetValue ( referenceTargetIndex , out previewData ) )
595
623
{
596
- m_PreviewUtility = new PreviewRenderUtility ( ) ;
597
- m_PreviewUtility . camera . fieldOfView = 30.0f ;
598
- CreatePreviewInstances ( ) ;
624
+ previewData = new PreviewData ( target ) ;
625
+ m_PreviewInstances . Add ( referenceTargetIndex , previewData ) ;
599
626
}
627
+
628
+ return previewData ;
600
629
}
601
630
602
631
public void OnDestroy ( )
603
632
{
604
- if ( m_PreviewUtility != null )
605
- {
606
- m_PreviewUtility . Cleanup ( ) ;
607
- m_PreviewUtility = null ;
608
- m_PreviewInstances . Clear ( ) ;
609
- }
633
+ foreach ( var previewData in m_PreviewInstances . Values )
634
+ previewData . Dispose ( ) ;
635
+ m_PreviewInstances . Clear ( ) ;
610
636
}
611
637
612
638
public static bool HasRenderableParts ( GameObject go )
@@ -789,52 +815,50 @@ public override void OnPreviewSettings()
789
815
if ( ! ShaderUtil . hardwareSupportsRectRenderTexture )
790
816
return ;
791
817
GUI . enabled = true ;
792
- InitPreview ( ) ;
793
818
}
794
819
795
820
private void DoRenderPreview ( )
796
821
{
797
- GameObject go = m_PreviewInstances [ referenceTargetIndex ] ;
822
+ var previewData = GetPreviewData ( ) ;
798
823
799
- Bounds bounds = new Bounds ( go . transform . position , Vector3 . zero ) ;
800
- GetRenderableBoundsRecurse ( ref bounds , go ) ;
824
+ Bounds bounds = new Bounds ( previewData . gameObject . transform . position , Vector3 . zero ) ;
825
+ GetRenderableBoundsRecurse ( ref bounds , previewData . gameObject ) ;
801
826
float halfSize = Mathf . Max ( bounds . extents . magnitude , 0.0001f ) ;
802
827
float distance = halfSize * 3.8f ;
803
828
804
829
Quaternion rot = Quaternion . Euler ( - previewDir . y , - previewDir . x , 0 ) ;
805
830
Vector3 pos = bounds . center - rot * ( Vector3 . forward * distance ) ;
806
831
807
- m_PreviewUtility . camera . transform . position = pos ;
808
- m_PreviewUtility . camera . transform . rotation = rot ;
809
- m_PreviewUtility . camera . nearClipPlane = distance - halfSize * 1.1f ;
810
- m_PreviewUtility . camera . farClipPlane = distance + halfSize * 1.1f ;
832
+ previewData . renderUtility . camera . transform . position = pos ;
833
+ previewData . renderUtility . camera . transform . rotation = rot ;
834
+ previewData . renderUtility . camera . nearClipPlane = distance - halfSize * 1.1f ;
835
+ previewData . renderUtility . camera . farClipPlane = distance + halfSize * 1.1f ;
811
836
812
- m_PreviewUtility . lights [ 0 ] . intensity = .7f ;
813
- m_PreviewUtility . lights [ 0 ] . transform . rotation = rot * Quaternion . Euler ( 40f , 40f , 0 ) ;
814
- m_PreviewUtility . lights [ 1 ] . intensity = .7f ;
815
- m_PreviewUtility . lights [ 1 ] . transform . rotation = rot * Quaternion . Euler ( 340 , 218 , 177 ) ;
837
+ previewData . renderUtility . lights [ 0 ] . intensity = .7f ;
838
+ previewData . renderUtility . lights [ 0 ] . transform . rotation = rot * Quaternion . Euler ( 40f , 40f , 0 ) ;
839
+ previewData . renderUtility . lights [ 1 ] . intensity = .7f ;
840
+ previewData . renderUtility . lights [ 1 ] . transform . rotation = rot * Quaternion . Euler ( 340 , 218 , 177 ) ;
816
841
817
- m_PreviewUtility . ambientColor = new Color ( .1f , .1f , .1f , 0 ) ;
842
+ previewData . renderUtility . ambientColor = new Color ( .1f , .1f , .1f , 0 ) ;
818
843
819
- var prefabType = PrefabUtility . GetPrefabType ( go ) ;
844
+ var prefabType = PrefabUtility . GetPrefabType ( previewData . gameObject ) ;
820
845
var allowSRP = ! ( prefabType == PrefabType . DisconnectedModelPrefabInstance || prefabType == PrefabType . ModelPrefab ) ;
821
- m_PreviewUtility . Render ( allowSRP ) ;
846
+ previewData . renderUtility . Render ( allowSRP ) ;
822
847
}
823
848
824
- public override Texture2D RenderStaticPreview ( string assetPath , Object [ ] subAssets , int width , int height )
849
+ public override Texture2D RenderStaticPreview ( string assetPath , UnityObject [ ] subAssets , int width , int height )
825
850
{
826
851
if ( ! HasStaticPreview ( ) || ! ShaderUtil . hardwareSupportsRectRenderTexture )
827
852
{
828
853
return null ;
829
854
}
830
855
831
- InitPreview ( ) ;
832
-
833
- m_PreviewUtility . BeginStaticPreview ( new Rect ( 0 , 0 , width , height ) ) ;
856
+ var previewUtility = GetPreviewData ( ) . renderUtility ;
857
+ previewUtility . BeginStaticPreview ( new Rect ( 0 , 0 , width , height ) ) ;
834
858
835
859
DoRenderPreview ( ) ;
836
860
837
- return m_PreviewUtility . EndStaticPreview ( ) ;
861
+ return previewUtility . EndStaticPreview ( ) ;
838
862
}
839
863
840
864
public override void OnPreviewGUI ( Rect r , GUIStyle background )
@@ -845,18 +869,18 @@ public override void OnPreviewGUI(Rect r, GUIStyle background)
845
869
EditorGUI . DropShadowLabel ( new Rect ( r . x , r . y , r . width , 40 ) , "Preview requires\n render texture support" ) ;
846
870
return ;
847
871
}
848
- InitPreview ( ) ;
849
872
850
873
previewDir = PreviewGUI . Drag2D ( previewDir , r ) ;
851
874
852
875
if ( Event . current . type != EventType . Repaint )
853
876
return ;
854
877
855
- m_PreviewUtility . BeginPreview ( r , background ) ;
878
+ var previewUtility = GetPreviewData ( ) . renderUtility ;
879
+ previewUtility . BeginPreview ( r , background ) ;
856
880
857
881
DoRenderPreview ( ) ;
858
882
859
- m_PreviewUtility . EndAndDrawPreview ( r ) ;
883
+ previewUtility . EndAndDrawPreview ( r ) ;
860
884
}
861
885
862
886
// Handle dragging in scene view
@@ -926,7 +950,7 @@ public void OnSceneDrag(SceneView sceneView)
926
950
case EventType . DragExited :
927
951
if ( dragObject )
928
952
{
929
- Object . DestroyImmediate ( dragObject , false ) ;
953
+ UnityObject . DestroyImmediate ( dragObject , false ) ;
930
954
HandleUtility . ignoreRaySnapObjects = null ;
931
955
dragObject = null ;
932
956
evt . Use ( ) ;
0 commit comments