@@ -217,7 +217,7 @@ public static GameObject[] CreateInstantiatedModelPrefab(
217
217
/// <param name="origObj"></param>
218
218
/// <param name="newObj"></param>
219
219
/// <param name="toConvertRoot"></param>
220
- private static void FixSceneReferences ( Object origObj , Object newObj , GameObject toConvertRoot )
220
+ internal static void FixSceneReferences ( Object origObj , Object newObj , GameObject toConvertRoot )
221
221
{
222
222
var sceneObjs = GetSceneReferencesToObject ( origObj ) ;
223
223
@@ -260,12 +260,25 @@ private static void FixSceneReferences(Object origObj, Object newObj, GameObject
260
260
}
261
261
}
262
262
263
+ /// <summary>
264
+ /// Helper for getting a property from an instance with reflection.
265
+ /// </summary>
266
+ /// <param name="instance"></param>
267
+ /// <param name="propertyName"></param>
268
+ /// <param name="isPublic"></param>
269
+ /// <returns></returns>
270
+ private static object GetPropertyReflection ( object instance , string propertyName , bool isPublic )
271
+ {
272
+ return instance . GetType ( ) . GetProperty ( propertyName , ( isPublic ? System . Reflection . BindingFlags . Public : System . Reflection . BindingFlags . NonPublic ) |
273
+ System . Reflection . BindingFlags . Instance ) . GetValue ( instance , null ) ;
274
+ }
275
+
263
276
/// <summary>
264
277
/// Returns a list of GameObjects in the scene that contain references to the given object.
265
278
/// </summary>
266
279
/// <param name="obj"></param>
267
280
/// <returns></returns>
268
- private static List < GameObject > GetSceneReferencesToObject ( Object obj )
281
+ internal static List < GameObject > GetSceneReferencesToObject ( Object obj )
269
282
{
270
283
var sceneHierarchyWindowType = typeof ( UnityEditor . SearchableEditorWindow ) . Assembly . GetType ( "UnityEditor.SceneHierarchyWindow" ) ;
271
284
var sceneHierarchyWindow = EditorWindow . GetWindow ( sceneHierarchyWindowType ) ;
@@ -277,16 +290,16 @@ private static List<GameObject> GetSceneReferencesToObject(Object obj)
277
290
setSearchFilterMethod . Invoke ( sceneHierarchyWindow , new object [ ] { string . Format ( idFormat , instanceID ) , SearchableEditorWindow . SearchMode . All , true , false } ) ;
278
291
279
292
// Get objects from list of instance IDs of currently visible objects
280
- var sceneHierarchy = sceneHierarchyWindowType . GetProperty ( "sceneHierarchy" , System . Reflection . BindingFlags . Public | System . Reflection . BindingFlags . Instance ) . GetValue ( sceneHierarchyWindow , null ) ;
281
- var treeView = sceneHierarchy . GetType ( ) . GetProperty ( "treeView" , System . Reflection . BindingFlags . NonPublic | System . Reflection . BindingFlags . Instance ) . GetValue ( sceneHierarchy , null ) ;
282
- var data = treeView . GetType ( ) . GetProperty ( "data" ) . GetValue ( treeView , null ) ;
293
+ var sceneHierarchy = GetPropertyReflection ( sceneHierarchyWindow , "sceneHierarchy" , isPublic : true ) ;
294
+ var treeView = GetPropertyReflection ( sceneHierarchy , "treeView" , isPublic : false ) ;
295
+ var data = GetPropertyReflection ( treeView , "data" , isPublic : true ) ;
283
296
var getRows = data . GetType ( ) . GetMethod ( "GetRows" ) ;
284
297
var rows = getRows . Invoke ( data , null ) as IEnumerable ;
285
298
286
299
var sceneObjects = new List < GameObject > ( ) ;
287
300
foreach ( var row in rows )
288
301
{
289
- var id = ( int ) row . GetType ( ) . GetProperty ( "id" ) . GetValue ( row , null ) ;
302
+ var id = ( int ) GetPropertyReflection ( row , "id" , isPublic : true ) ;
290
303
var gameObject = EditorUtility . InstanceIDToObject ( id ) as GameObject ;
291
304
if ( gameObject )
292
305
{
0 commit comments