Exemple #1
0
        /// <summary>
        /// Locate the specified object in the Resources folder.
        /// This function will only work in the Unity Editor.
        /// </summary>

        static public string LocateResource(Object obj, bool allowPrefabInstances = false)
        {
#if UNITY_EDITOR
            Object prefab = UnityEditor.PrefabUtility.GetPrefabParent(obj) ?? obj;

            if (prefab != null)
            {
                if (!allowPrefabInstances && prefab != obj)
                {
                    return(null);
                }

                string childPrefabPath = UnityEditor.AssetDatabase.GetAssetPath(prefab);

                if (!string.IsNullOrEmpty(childPrefabPath) && childPrefabPath.Contains("/Resources/"))
                {
                    int index = childPrefabPath.IndexOf("/Resources/");

                    if (index != -1)
                    {
                        childPrefabPath = childPrefabPath.Substring(index + "/Resources/".Length);
                        childPrefabPath = Tools.GetFilePathWithoutExtension(childPrefabPath).Replace("\\", "/");

                        Object loaded = Resources.Load(childPrefabPath);
                        if (loaded != null && loaded.GetType() == obj.GetType())
                        {
                            return(childPrefabPath);
                        }
                    }
                }
            }
#endif
            return(null);
        }
        /// <summary>
        /// Locate the specified object in the Resources folder.
        /// This function will only work in the Unity Editor.
        /// </summary>

        static public string LocateResource(Object obj, bool allowPrefabInstances = false)
        {
#if UNITY_EDITOR
            var prefab = UnityEditor.PrefabUtility.GetPrefabObject(obj);
            if (obj is GameObject && prefab != null && obj != prefab)
            {
                return(null);
            }
            if (prefab == null)
            {
                prefab = obj;
            }

            if (prefab != null)
            {
                if (!allowPrefabInstances && prefab != obj)
                {
                    return(null);
                }

                // Selected objects should not count as referenced prefabs, as they're being exported
                var objects = UnityEditor.Selection.objects;
                if (objects != null)
                {
                    foreach (var o in objects)
                    {
                        if (prefab == o)
                        {
                            return(null);
                        }
                    }
                }

                var childPrefabPath = UnityEditor.AssetDatabase.GetAssetPath(prefab);

                if (!string.IsNullOrEmpty(childPrefabPath) && childPrefabPath.Contains("/Resources/"))
                {
                    var index = childPrefabPath.IndexOf("/Resources/");

                    if (index != -1)
                    {
                        childPrefabPath = childPrefabPath.Substring(index + "/Resources/".Length);
                        childPrefabPath = Tools.GetFilePathWithoutExtension(childPrefabPath).Replace("\\", "/");

                        var loaded = Resources.Load(childPrefabPath, obj.GetType());
                        if (loaded != null)
                        {
                            return(childPrefabPath);
                        }
                    }
                }
            }
#endif
            return(null);
        }
        /// <summary>
        /// Convert an object reference to a serializable string format.
        /// </summary>

        static public string ReferenceToString(this GameObject go, UnityEngine.Object obj)
        {
            if (obj == null)
            {
                return(null);
            }

            var type = obj.GetType();

            if (obj is Shader)
            {
                return("asset|" + type.ToString().Replace("UnityEngine.", "") + "|" + obj.name);
            }
            else
            {
                //var gobj = obj as GameObject;
                //var comp = obj as Component;
                //var src = (gobj != null ? gobj : (comp != null ? comp.gameObject : null));

                //if (src != null)
                //{
                //	if (src == go) return "ref|" + type.ToString().Replace("UnityEngine.", "");
                //	string path = src.transform.GetHierarchy(go.transform);
                //	if (!string.IsNullOrEmpty(path)) return "ref|" + type.ToString().Replace("UnityEngine.", "") + "|" + path;
                //}
#if UNITY_EDITOR
                string assetPath = UnityEditor.AssetDatabase.GetAssetPath(obj);

                if (!string.IsNullOrEmpty(assetPath))
                {
                    int index = assetPath.IndexOf("Resources/");
                    if (index == -1)
                    {
                        return(null);
                    }
                    assetPath = assetPath.Substring(index + "Resources/".Length);
                    assetPath = Tools.GetFilePathWithoutExtension(assetPath).Replace('\\', '/');
                    return("asset|" + type.ToString().Replace("UnityEngine.", "") + "|" + assetPath);
                }
#endif
            }
            return(null);
        }
Exemple #4
0
        /// <summary>
        /// Convert an object reference to a serializable string format.
        /// </summary>

        static public string ReferenceToString(this GameObject go, UnityEngine.Object obj)
        {
            if (obj == null)
            {
                return(null);
            }

            if (obj is Shader)
            {
                return("asset|" + obj.GetType().ToString().Replace("UnityEngine.", "") + "|" + (obj as Shader).name);
            }
            else
            {
#if UNITY_EDITOR
                string assetPath = UnityEditor.AssetDatabase.GetAssetPath(obj);

                if (!string.IsNullOrEmpty(assetPath))
                {
                    int index = assetPath.IndexOf("Resources/");
                    if (index != -1)
                    {
                        assetPath = assetPath.Substring(index + "Resources/".Length);
                    }
                    assetPath = Tools.GetFilePathWithoutExtension(assetPath).Replace('\\', '/');
                    return("asset|" + obj.GetType().ToString().Replace("UnityEngine.", "") + "|" + assetPath);
                }
#endif
                UnityEngine.GameObject gobj = obj as UnityEngine.GameObject;
                UnityEngine.Component  comp = obj as UnityEngine.Component;

                if (comp != null || gobj != null)
                {
                    string path = (gobj ?? comp.gameObject).transform.GetHierarchy(go.transform);
                    if (!string.IsNullOrEmpty(path))
                    {
                        return("ref|" + obj.GetType().ToString().Replace("UnityEngine.", "") + "|" + path);
                    }
                }
            }
            return(null);
        }