Exemplo n.º 1
0
        public override Dictionary <string, object> WriteJson(Type type, object value)
        {
            UnityEngine.Object obj = (UnityEngine.Object)value;


            Dictionary <string, object> dict = new Dictionary <string, object>();

            dict.Add("InstanceID", obj.GetInstanceID());
            dict.Add("Name", obj.name);
            dict.Add("Type", obj.GetType().AssemblyQualifiedName);

            //Write scene path if the object is a Component or GameObject
            Component  component = value as Component;
            GameObject go        = value as GameObject;

            if (component != null || go != null)
            {
                if (component != null && go == null)
                {
                    go = component.gameObject;
                }

                UnityReferenceHelper helper = go.GetComponent <UnityReferenceHelper>();

                if (helper == null)
                {
                    Debug.Log("Adding UnityReferenceHelper to Unity Reference '" + obj.name + "'");
                    helper = go.AddComponent <UnityReferenceHelper>();
                }

                //Make sure it has a unique GUID
                helper.Reset();

                dict.Add("GUID", helper.GetGUID());
            }
            return(dict);
        }
        private UnityEngine.Object DeserializeUnityObjectInner()
        {
            string a = this.EatField();

            if (a == "InstanceID")
            {
                this.EatField();
                a = this.EatField();
            }
            if (a != "Name")
            {
                throw new Exception("Expected 'Name' field");
            }
            string text = this.EatField();

            if (text == null)
            {
                return(null);
            }
            if (this.EatField() != "Type")
            {
                throw new Exception("Expected 'Type' field");
            }
            string text2 = this.EatField();

            if (text2.IndexOf(',') != -1)
            {
                text2 = text2.Substring(0, text2.IndexOf(','));
            }
            Type type = WindowsStoreCompatibility.GetTypeInfo(typeof(AstarPath)).Assembly.GetType(text2);

            type = (type ?? WindowsStoreCompatibility.GetTypeInfo(typeof(Transform)).Assembly.GetType(text2));
            if (object.Equals(type, null))
            {
                Debug.LogError("Could not find type '" + text2 + "'. Cannot deserialize Unity reference");
                return(null);
            }
            this.EatWhitespace();
            if ((ushort)this.reader.Peek() == 34)
            {
                if (this.EatField() != "GUID")
                {
                    throw new Exception("Expected 'GUID' field");
                }
                string b = this.EatField();
                UnityReferenceHelper[] array = UnityEngine.Object.FindObjectsOfType <UnityReferenceHelper>();
                int i = 0;
                while (i < array.Length)
                {
                    UnityReferenceHelper unityReferenceHelper = array[i];
                    if (unityReferenceHelper.GetGUID() == b)
                    {
                        if (object.Equals(type, typeof(GameObject)))
                        {
                            return(unityReferenceHelper.gameObject);
                        }
                        return(unityReferenceHelper.GetComponent(type));
                    }
                    else
                    {
                        i++;
                    }
                }
            }
            UnityEngine.Object[] array2 = Resources.LoadAll(text, type);
            for (int j = 0; j < array2.Length; j++)
            {
                if (array2[j].name == text || array2.Length == 1)
                {
                    return(array2[j]);
                }
            }
            return(null);
        }
Exemplo n.º 3
0
    public Object ObjectField(GUIContent label, Object obj, System.Type objType, bool allowSceneObjects)
    {
#if UNITY_3_3
        allowSceneObjects = true;
#endif

#if UNITY_3_3
        obj = EditorGUILayout.ObjectField(label, obj, objType);
#else
        obj = EditorGUILayout.ObjectField(label, obj, objType, allowSceneObjects);
#endif

        if (obj != null)
        {
            if (allowSceneObjects && !EditorUtility.IsPersistent(obj))
            {
                //Object is in the scene
                Component  com = obj as Component;
                GameObject go  = obj as GameObject;
                if (com != null)
                {
                    go = com.gameObject;
                }
                if (go != null)
                {
                    UnityReferenceHelper urh = go.GetComponent <UnityReferenceHelper> ();
                    if (urh == null)
                    {
                        if (FixLabel("Object's GameObject must have a UnityReferenceHelper component attached"))
                        {
                            go.AddComponent <UnityReferenceHelper>();
                        }
                    }
                }
            }
            else if (EditorUtility.IsPersistent(obj))
            {
                string path = AssetDatabase.GetAssetPath(obj);

                System.Text.RegularExpressions.Regex rg = new System.Text.RegularExpressions.Regex(@"Resources[/|\\][^/]*$");

                //if (!path.Contains ("Resources/")) {
                Debug.Log(path + "  " + rg.Match(path).Value);

                if (!rg.IsMatch(path))
                {
                    if (FixLabel("Object must be in the 'Resources' folder, top level"))
                    {
                        if (!System.IO.Directory.Exists(Application.dataPath + "/Resources"))
                        {
                            System.IO.Directory.CreateDirectory(Application.dataPath + "/Resources");
                            AssetDatabase.Refresh();
                        }
                        string ext = System.IO.Path.GetExtension(path);

                        string error = AssetDatabase.MoveAsset(path, "Assets/Resources/" + obj.name + ext);

                        if (error == "")
                        {
                            //Debug.Log ("Successful move");
                            path = AssetDatabase.GetAssetPath(obj);
                        }
                        else
                        {
                            Debug.LogError("Couldn't move asset - " + error);
                        }
                    }
                }

                if (!AssetDatabase.IsMainAsset(obj) && obj.name != AssetDatabase.LoadMainAssetAtPath(path).name)
                {
                    if (FixLabel("Due to technical reasons, the main asset must\nhave the same name as the referenced asset"))
                    {
                        string error = AssetDatabase.RenameAsset(path, obj.name);
                        if (error == "")
                        {
                            //Debug.Log ("Successful");
                        }
                        else
                        {
                            Debug.LogError("Couldn't rename asset - " + error);
                        }
                    }
                }
            }
        }

        return(obj);
    }