@@ -26,46 +26,58 @@ public void Migrate(GameObject gameObject)
2626 var boundingBox = gameObject . GetComponent < BoundingBox > ( ) ;
2727 var boundsControl = gameObject . AddComponent < BoundsControl > ( ) ;
2828
29- // migrate logic settings
30- boundsControl . Target = boundingBox . Target ;
31- boundsControl . BoundsOverride = boundingBox . BoundsOverride ;
32- boundsControl . CalculationMethod = MigrateCalculationMethod ( boundingBox . CalculationMethod ) ;
33- boundsControl . BoundsControlActivation = MigrateActivationFlag ( boundingBox . BoundingBoxActivation ) ;
34-
35- // only carry over min max scaling values if user hasn't attached min max scale constraint component yet
36- if ( gameObject . GetComponent < MinMaxScaleConstraint > ( ) == null )
3729 {
38- MinMaxScaleConstraint scaleConstraint = gameObject . AddComponent < MinMaxScaleConstraint > ( ) ;
30+ Undo . RecordObject ( gameObject , "BoundsControl migration: swapping BoundingBox with BoundsControl." ) ;
31+
32+ // migrate logic settings
33+ boundsControl . Target = boundingBox . Target ;
34+ boundsControl . BoundsOverride = boundingBox . BoundsOverride ;
35+ boundsControl . CalculationMethod = MigrateCalculationMethod ( boundingBox . CalculationMethod ) ;
36+ boundsControl . BoundsControlActivation = MigrateActivationFlag ( boundingBox . BoundingBoxActivation ) ;
37+
38+ // only carry over min max scaling values if user hasn't attached min max scale constraint component yet
39+ if ( gameObject . GetComponent < MinMaxScaleConstraint > ( ) == null )
40+ {
3941#pragma warning disable 0618
40- scaleConstraint . ScaleMinimum = boundingBox . ScaleMinimum ;
41- scaleConstraint . ScaleMaximum = boundingBox . ScaleMaximum ;
42+ // create a minmaxscaleconstraint in case there's a min max scale set up
43+ if ( boundingBox . ScaleMinimum != 0.0f || boundingBox . ScaleMaximum != 0.0f )
44+ {
45+ MinMaxScaleConstraint scaleConstraint = gameObject . AddComponent < MinMaxScaleConstraint > ( ) ;
46+ scaleConstraint . ScaleMinimum = boundingBox . ScaleMinimum ;
47+ scaleConstraint . ScaleMaximum = boundingBox . ScaleMaximum ;
48+ }
4249#pragma warning restore 0618
50+ }
51+
52+ // migrate visuals
53+ boundsControl . DrawTetherWhenManipulating = boundingBox . DrawTetherWhenManipulating ;
54+ boundsControl . HandlesIgnoreCollider = boundingBox . HandlesIgnoreCollider ;
55+ boundsControl . FlattenAxis = MigrateFlattenAxis ( boundingBox . FlattenAxis ) ;
56+ boundsControl . BoxPadding = boundingBox . BoxPadding ;
57+ string configDir = GetBoundsControlConfigDirectory ( boundingBox ) ;
58+ MigrateBoxDisplay ( boundsControl , boundingBox , configDir ) ;
59+ MigrateLinks ( boundsControl , boundingBox , configDir ) ;
60+ MigrateScaleHandles ( boundsControl , boundingBox , configDir ) ;
61+ MigrateRotationHandles ( boundsControl , boundingBox , configDir ) ;
62+ MigrateProximityEffect ( boundsControl , boundingBox , configDir ) ;
63+
64+ // debug properties
65+ boundsControl . DebugText = boundingBox . debugText ;
66+ boundsControl . HideElementsInInspector = boundingBox . HideElementsInInspector ;
67+
68+ // events
69+ boundsControl . RotateStarted = boundingBox . RotateStarted ;
70+ boundsControl . RotateStopped = boundingBox . RotateStopped ;
71+ boundsControl . ScaleStarted = boundingBox . ScaleStarted ;
72+ boundsControl . ScaleStopped = boundingBox . ScaleStopped ;
73+
74+ // destroy obsolete component
75+ Object . DestroyImmediate ( boundingBox ) ;
4376 }
4477
45- // migrate visuals
46- boundsControl . DrawTetherWhenManipulating = boundingBox . DrawTetherWhenManipulating ;
47- boundsControl . HandlesIgnoreCollider = boundingBox . HandlesIgnoreCollider ;
48- boundsControl . FlattenAxis = MigrateFlattenAxis ( boundingBox . FlattenAxis ) ;
49- boundsControl . BoxPadding = boundingBox . BoxPadding ;
50- string configDir = GetBoundsControlConfigDirectory ( boundingBox ) ;
51- MigrateBoxDisplay ( boundsControl , boundingBox , configDir ) ;
52- MigrateLinks ( boundsControl , boundingBox , configDir ) ;
53- MigrateScaleHandles ( boundsControl , boundingBox , configDir ) ;
54- MigrateRotationHandles ( boundsControl , boundingBox , configDir ) ;
55- MigrateProximityEffect ( boundsControl , boundingBox , configDir ) ;
56-
57- // debug properties
58- boundsControl . DebugText = boundingBox . debugText ;
59- boundsControl . HideElementsInInspector = boundingBox . HideElementsInInspector ;
60-
61- // events
62- boundsControl . RotateStarted = boundingBox . RotateStarted ;
63- boundsControl . RotateStopped = boundingBox . RotateStopped ;
64- boundsControl . ScaleStarted = boundingBox . ScaleStarted ;
65- boundsControl . ScaleStopped = boundingBox . ScaleStopped ;
66-
67- // destroy obsolete component
68- UnityEngine . Object . DestroyImmediate ( boundingBox ) ;
78+ // look in the scene for app bars and upgrade them too to point to the new component
79+ MigrateAppBar ( boundingBox , boundsControl ) ;
80+
6981 }
7082
7183 private string GetBoundsControlConfigDirectory ( BoundingBox boundingBox )
@@ -75,18 +87,30 @@ private string GetBoundsControlConfigDirectory(BoundingBox boundingBox)
7587 if ( scene != null )
7688 {
7789 string scenePath = scene . path ;
78- string dirPath = System . IO . Path . GetDirectoryName ( scenePath ) ;
79- string configPath = System . IO . Path . Combine ( dirPath , "BoundsControlConfigs/" ) ;
80- return configPath ;
90+ string sceneDir = System . IO . Path . GetDirectoryName ( scenePath ) ;
91+
92+ const string configDir = "BoundsControlConfigs" ;
93+ string configPath = System . IO . Path . Combine ( sceneDir , configDir ) ;
94+ if ( AssetDatabase . IsValidFolder ( configPath ) )
95+ {
96+ return configPath ;
97+ }
98+ else
99+ {
100+ string guid = AssetDatabase . CreateFolder ( sceneDir , configDir ) ;
101+ return AssetDatabase . GUIDToAssetPath ( guid ) ;
102+ }
81103 }
82104
83105 return "" ;
84106 }
85107 private string GenerateUniqueConfigName ( string directory , GameObject migratingFrom , string configName )
86108 {
87- return directory + migratingFrom . name + migratingFrom . GetInstanceID ( ) + configName + ".asset" ;
109+ return directory + "/" + migratingFrom . name + migratingFrom . GetInstanceID ( ) + configName + ".asset" ;
88110 }
89111
112+ #region Flags Migration
113+
90114 private BoundsCalculationMethod MigrateCalculationMethod ( BoundingBox . BoundsCalculationMethod calculationMethod )
91115 {
92116 switch ( calculationMethod )
@@ -173,9 +197,13 @@ private RotationHandlePrefabCollider MigrateRotationHandleColliderType(BoundingB
173197 return RotationHandlePrefabCollider . Sphere ;
174198 }
175199
200+ #endregion Flags Migration
201+
202+ #region Visuals Configuration Migration
203+
176204 private void MigrateBoxDisplay ( BoundsControl control , BoundingBox box , string configAssetDirectory )
177205 {
178- BoxDisplayConfiguration config = new BoxDisplayConfiguration ( ) ;
206+ BoxDisplayConfiguration config = ScriptableObject . CreateInstance < BoxDisplayConfiguration > ( ) ;
179207 AssetDatabase . CreateAsset ( config , GenerateUniqueConfigName ( configAssetDirectory , box . gameObject , "BoxDisplayConfiguration" ) ) ;
180208
181209 config . BoxMaterial = box . BoxMaterial ;
@@ -187,7 +215,7 @@ private void MigrateBoxDisplay(BoundsControl control, BoundingBox box, string co
187215
188216 private void MigrateLinks ( BoundsControl control , BoundingBox box , string configAssetDirectory )
189217 {
190- LinksConfiguration config = new LinksConfiguration ( ) ;
218+ LinksConfiguration config = ScriptableObject . CreateInstance < LinksConfiguration > ( ) ;
191219 AssetDatabase . CreateAsset ( config , GenerateUniqueConfigName ( configAssetDirectory , box . gameObject , "LinksConfiguration" ) ) ;
192220
193221 config . WireframeMaterial = box . WireframeMaterial ;
@@ -201,7 +229,7 @@ private void MigrateLinks(BoundsControl control, BoundingBox box, string configA
201229
202230 private void MigrateScaleHandles ( BoundsControl control , BoundingBox box , string configAssetDirectory )
203231 {
204- ScaleHandlesConfiguration config = new ScaleHandlesConfiguration ( ) ;
232+ ScaleHandlesConfiguration config = ScriptableObject . CreateInstance < ScaleHandlesConfiguration > ( ) ;
205233 AssetDatabase . CreateAsset ( config , GenerateUniqueConfigName ( configAssetDirectory , box . gameObject , "ScaleHandlesConfiguration" ) ) ;
206234
207235 config . HandleSlatePrefab = box . ScaleHandleSlatePrefab ;
@@ -217,7 +245,7 @@ private void MigrateScaleHandles(BoundsControl control, BoundingBox box, string
217245
218246 private void MigrateRotationHandles ( BoundsControl control , BoundingBox box , string configAssetDirectory )
219247 {
220- RotationHandlesConfiguration config = new RotationHandlesConfiguration ( ) ;
248+ RotationHandlesConfiguration config = ScriptableObject . CreateInstance < RotationHandlesConfiguration > ( ) ;
221249 AssetDatabase . CreateAsset ( config , GenerateUniqueConfigName ( configAssetDirectory , box . gameObject , "RotationHandlesConfiguration" ) ) ;
222250
223251 config . RotationHandlePrefabColliderType = MigrateRotationHandleColliderType ( box . RotationHandlePrefabColliderType ) ;
@@ -235,7 +263,7 @@ private void MigrateRotationHandles(BoundsControl control, BoundingBox box, stri
235263
236264 private void MigrateProximityEffect ( BoundsControl control , BoundingBox box , string configAssetDirectory )
237265 {
238- ProximityEffectConfiguration config = new ProximityEffectConfiguration ( ) ;
266+ ProximityEffectConfiguration config = ScriptableObject . CreateInstance < ProximityEffectConfiguration > ( ) ;
239267 AssetDatabase . CreateAsset ( config , GenerateUniqueConfigName ( configAssetDirectory , box . gameObject , "ProximityEffectConfiguration" ) ) ;
240268
241269 config . ProximityEffectActive = box . ProximityEffectActive ;
@@ -250,5 +278,22 @@ private void MigrateProximityEffect(BoundsControl control, BoundingBox box, stri
250278
251279 control . HandleProximityEffectConfiguration = config ;
252280 }
281+
282+ #endregion Visuals Configuration Migration
283+ private void MigrateAppBar ( BoundingBox boundingBox , BoundsControl boundsControl )
284+ {
285+ // note: this might be expensive for larger scenes but we don't know where the appbar is
286+ // placed in the hierarchy so we have to search the scene for it
287+ AppBar [ ] appBars = Object . FindObjectsOfType < AppBar > ( ) ;
288+ for ( int i = 0 ; i < appBars . Length ; ++ i )
289+ {
290+ AppBar appBar = appBars [ i ] ;
291+ if ( appBar . BoundingBox == boundingBox )
292+ {
293+ Undo . RecordObject ( appBar , "BoundsControl migration: changed target of app bar." ) ;
294+ appBar . BoundingBox = boundsControl ;
295+ }
296+ }
297+ }
253298 }
254299}
0 commit comments