Beispiel #1
0
        /// <summary>
        /// Convert a serialized string reference to an actual object reference.
        /// </summary>

        static public UnityEngine.Object StringToReference(this GameObject go, string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                return(null);
            }
            string[] split = path.Split(new char[] { '|' }, 3);

            if (split.Length == 3)
            {
                System.Type myType = UnityTools.FindType(split[1]);

                if (myType != null)
                {
                    if (myType == typeof(Shader))
                    {
                        return(Shader.Find(split[2]));
                    }
                    else if (split[0] == "asset")
                    {
                        return(Resources.Load(split[2], myType));
                    }
                    else if (split[0] == "ref")
                    {
                        Transform t = go.transform;

                        string[] splitPath = split[2].Split('/');

                        for (int i = 0; i < splitPath.Length; ++i)
                        {
                            string s = splitPath[i];

                            if (s == "..")
                            {
                                t = t.parent;
                                if (t == null)
                                {
                                    break;
                                }
                            }
                            else if (!string.IsNullOrEmpty(s))
                            {
                                t = t.FindChild(s);
                                if (t == null)
                                {
                                    break;
                                }
                            }
                        }

                        if (t != null)
                        {
                            if (myType == typeof(GameObject))
                            {
                                return(t.gameObject);
                            }
                            return(t.GetComponent(myType));
                        }
                        else
                        {
                            Debug.LogWarning("Hierarchy path not found: " + split[2], go);
                        }
                    }
                }
            }
            return(null);
        }