Created
December 31, 2015 06:58
-
-
Save DmitriyYukhanov/feb182871a14e355ac38 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define UNITY_5_PLUS | |
#if UNITY_4_5 || UNITY_4_6 || UNITY_4_7 || UNITY_4_8 || UNITY_4_9 | |
#undef UNITY_5_PLUS | |
#endif | |
using UnityEngine; | |
using System.Reflection; | |
using UnityEditor; | |
namespace CodeStage.Maintainer.Tools | |
{ | |
public class ObjectTools | |
{ | |
private static PropertyInfo cachedInspectorModeInfo; | |
internal static long GetLocalIdentifierInFileForObject(Object unityObject) | |
{ | |
long id = -1; | |
if (unityObject == null) return id; | |
if (cachedInspectorModeInfo == null) | |
{ | |
cachedInspectorModeInfo = typeof(SerializedObject).GetProperty("inspectorMode", BindingFlags.NonPublic | BindingFlags.Instance); | |
} | |
SerializedObject serializedObject = new SerializedObject(unityObject); | |
cachedInspectorModeInfo.SetValue(serializedObject, InspectorMode.Debug, null); | |
SerializedProperty serializedProperty = serializedObject.FindProperty("m_LocalIdentfierInFile"); | |
#if UNITY_5_PLUS | |
id = serializedProperty.longValue; | |
#else | |
id = serializedProperty.intValue; | |
#endif | |
if (id <= 0) | |
{ | |
PrefabType prefabType = PrefabUtility.GetPrefabType(unityObject); | |
if (prefabType != PrefabType.None) | |
{ | |
id = GetLocalIdentifierInFileForObject(PrefabUtility.GetPrefabObject(unityObject)); | |
} | |
else | |
{ | |
// this will work for the new objects in scene which weren't saved yet | |
id = unityObject.GetInstanceID(); | |
} | |
} | |
return id; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Update: https://gist.github.com/DmitriyYukhanov/5d93a0a204c32db1dd45d7a86e1df87e