public static bool TryDeserializeReference(string stringData, out Object result) { // support Object ID // (non-asset reference copied from an Object field) int objectId; if (int.TryParse(stringData, out objectId)) { result = InstanceIdUtility.IdToObject(objectId, Types.UnityObject); if (result != null) { return(true); } } #if UNITY_EDITOR // support pasting by path and local file identifier // (asset reference copied from an Object field, // where target is not the main asset) object pathAndIdObject = new AssetPathAndLocalId(); if (DeserializeOverride(stringData, typeof(AssetPathAndLocalId), ref pathAndIdObject)) { var pathAndId = (AssetPathAndLocalId)pathAndIdObject; if (pathAndId.HasPath()) { result = pathAndId.Load(); return(true); } } // support pasting using asset path result = UnityEditor.AssetDatabase.LoadMainAssetAtPath(stringData); if (result != null) { return(true); } #endif // support pasting GameObject using hierarchy path result = HierarchyUtility.FindByHierarchyPath(stringData); if (result != null) { return(true); } // support pasting Component using hierarchy path result = HierarchyUtility.FindComponentByHierarchyPath(stringData, Types.Component); if (result != null) { return(result); } return(false); }
public static Object DeserializeReference(string content, bool throwErrorIfFailed) { // support Object ID // (non-asset reference copied from an Object field) int objectId; if (int.TryParse(content, out objectId)) { return(InstanceIdUtility.IdToObject(objectId, Types.UnityObject)); } #if UNITY_EDITOR // support pasting by path and local file identifier // (asset reference copied from an Object field, // where target is not the main asset) object pathAndIdObject = new AssetPathAndLocalId(); if (DeserializeOverride(content, typeof(AssetPathAndLocalId), ref pathAndIdObject)) { var pathAndId = (AssetPathAndLocalId)pathAndIdObject; if (pathAndId.HasPath()) { return(pathAndId.Load()); } } // support pasting using asset path var asset = UnityEditor.AssetDatabase.LoadMainAssetAtPath(content); if (asset != null) { return(asset); } #endif // support pasting GameObject using hierarchy path var go = HierarchyUtility.FindByHierarchyPath(content); if (go != null) { return(go); } // support pasting Component using hierarchy path var comp = HierarchyUtility.FindComponentByHierarchyPath(content, Types.Component); if (comp != null) { return(comp); } if (throwErrorIfFailed) { throw new InvalidOperationException(); } return(null); }
public static bool IsSerializedReference(string stringData) { #if UNITY_EDITOR // support reference by guid and local file identifier // (asset reference copied from an Object field) object pathAndId = new AssetPathAndLocalId(); if (DeserializeOverride(stringData, typeof(AssetPathAndLocalId), ref pathAndId)) { var guidAndId = (AssetPathAndLocalId)pathAndId; if (guidAndId.HasPath()) { return(true); } } #endif // support reference by Object ID // (non-asset reference copied from an Object field) int objectId; if (int.TryParse(stringData, out objectId)) { if (InstanceIdUtility.IdToObject(objectId, Types.UnityObject) != null) { return(true); } } #if UNITY_EDITOR // support preference by asset path if (UnityEditor.AssetDatabase.LoadMainAssetAtPath(stringData) != null) { return(true); } #endif // support GameObject reference by hierarchy path if (HierarchyUtility.FindByHierarchyPath(stringData) != null) { return(true); } // support pasting Component using hierarchy path if (HierarchyUtility.FindComponentByHierarchyPath(stringData, Types.Component) != null) { return(true); } return(false); }
public static bool TryParse(string text, out AssetPathAndLocalId result) { #if UNITY_2018_1_OR_NEWER int i = text.IndexOf('|'); if (i != -1) { string assetPath = text.Substring(0, i); if (AssetDatabase.AssetPathToGUID(assetPath).Length == 0) { result = default(AssetPathAndLocalId); return(false); } string localIdString = text.Substring(i + 1); #if UNITY_2018_2_OR_NEWER long localId; if (long.TryParse(localIdString, out localId)) #else int localId; if (int.TryParse(localIdString, out localId)) #endif { result = new AssetPathAndLocalId(assetPath, localId); return(true); } result = new AssetPathAndLocalId(assetPath); return(true); } #endif if (AssetDatabase.AssetPathToGUID(text).Length == 0) { result = default(AssetPathAndLocalId); return(false); } result = new AssetPathAndLocalId(text); return(true); }
public static string SerializeReference([CanBeNull] Object target) { if (target == null) { return("null"); } #if UNITY_EDITOR if (UnityEditor.AssetDatabase.IsMainAsset(target)) { return(UnityEditor.AssetDatabase.GetAssetPath(target)); } var pathAndId = new AssetPathAndLocalId(target); if (pathAndId.HasPath()) { return(pathAndId.Serialize()); } #endif return(StringUtils.ToString(target.GetInstanceID())); }