Skip to content

Commit ce5fda5

Browse files
committed
clean up reflection so it is easier to read
1 parent 0b3f940 commit ce5fda5

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

com.unity.formats.fbx/Editor/ConvertToNestedPrefab.cs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ public static GameObject[] CreateInstantiatedModelPrefab(
217217
/// <param name="origObj"></param>
218218
/// <param name="newObj"></param>
219219
/// <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)
221221
{
222222
var sceneObjs = GetSceneReferencesToObject(origObj);
223223

@@ -260,12 +260,25 @@ private static void FixSceneReferences(Object origObj, Object newObj, GameObject
260260
}
261261
}
262262

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+
263276
/// <summary>
264277
/// Returns a list of GameObjects in the scene that contain references to the given object.
265278
/// </summary>
266279
/// <param name="obj"></param>
267280
/// <returns></returns>
268-
private static List<GameObject> GetSceneReferencesToObject(Object obj)
281+
internal static List<GameObject> GetSceneReferencesToObject(Object obj)
269282
{
270283
var sceneHierarchyWindowType = typeof(UnityEditor.SearchableEditorWindow).Assembly.GetType("UnityEditor.SceneHierarchyWindow");
271284
var sceneHierarchyWindow = EditorWindow.GetWindow(sceneHierarchyWindowType);
@@ -277,16 +290,16 @@ private static List<GameObject> GetSceneReferencesToObject(Object obj)
277290
setSearchFilterMethod.Invoke(sceneHierarchyWindow, new object[] { string.Format(idFormat, instanceID), SearchableEditorWindow.SearchMode.All, true, false });
278291

279292
// 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);
283296
var getRows = data.GetType().GetMethod("GetRows");
284297
var rows = getRows.Invoke(data, null) as IEnumerable;
285298

286299
var sceneObjects = new List<GameObject>();
287300
foreach (var row in rows)
288301
{
289-
var id = (int)row.GetType().GetProperty("id").GetValue(row, null);
302+
var id = (int)GetPropertyReflection(row, "id", isPublic: true);
290303
var gameObject = EditorUtility.InstanceIDToObject(id) as GameObject;
291304
if (gameObject)
292305
{

0 commit comments

Comments
 (0)