Inheritance: UnityEngine.MonoBehaviour
Esempio n. 1
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 (!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);
        }