Exemplo n.º 1
0
        private void DrawVariables()
        {
            scroll = GUILayout.BeginScrollView(scroll);
            Dictionary <string, List <FsmVariable> > groupParameters = GetGroupVariables();

            EditorGUIUtility.labelWidth = 110;
            foreach (var kvp in groupParameters)
            {
                bool foldout = EditorPrefs.GetBool(kvp.Key, false);
                bool state   = EditorGUILayout.Foldout(foldout, kvp.Key);
                if (state != foldout)
                {
                    EditorPrefs.SetBool(kvp.Key, state);
                }
                if (foldout)
                {
                    for (int i = 0; i < groupParameters[kvp.Key].Count; i++)
                    {
                        FsmVariable parameter = groupParameters[kvp.Key][i];
                        if (parameter != null)
                        {
                            SerializedObject   paramObject = new SerializedObject(parameter);
                            SerializedProperty prop        = paramObject.FindProperty("value");
                            GUILayout.BeginHorizontal();
                            GUILayout.Space(16f);
                            string name = paramObject.FindProperty("name").stringValue;
                            if (parameter is FsmGameObject)
                            {
                                GUI.changed = false;
                                FsmGameObject mParam = parameter as FsmGameObject;
                                if (string.IsNullOrEmpty(mParam.ScenePath))
                                {
                                    mParam.Value = (GameObject)EditorGUILayout.ObjectField(name, mParam.Value, typeof(UnityEngine.GameObject), true);
                                }
                                else
                                {
                                    GUILayout.Label(name, GUILayout.Width(106));
                                    GUILayout.Label(mParam.ScenePath, FsmEditorStyles.wrappedLabelLeft);
                                    GUILayout.FlexibleSpace();
                                }


                                if (GUI.changed)
                                {
                                    if (!EditorUtility.IsPersistent(mParam.Value) && mParam.Value is GameObject)
                                    {
                                        string currentOpenScene = string.Empty;
                                                                                #if UNITY_4_6 || UNITY_4_7 || UNITY_5_0 || UNITY_5_1 || UNITY_5_2
                                        currentOpenScene = EditorApplication.currentScene;
                                                                                #else
                                        currentOpenScene = UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene().name;
                                                                                #endif

                                        if (string.IsNullOrEmpty(currentOpenScene))
                                        {
                                            EditorUtility.DisplayDialog("Save Scene!",
                                                                        "You need to save the scene before setting the GameObject.", "Ok");
                                            mParam.Value = null;
                                        }
                                        else
                                        {
                                                                                        #if UNITY_4_6 || UNITY_4_7 || UNITY_5_0 || UNITY_5_1 || UNITY_5_2
                                            mParam.ScenePath = mParam.Value.name + "(" + EditorApplication.currentScene + ")";
                                                                                        #else
                                            mParam.ScenePath = mParam.Value.name + "(" + UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene().path + ")";
                                                                                        #endif
                                            SetGlobalGameObject mSet = mParam.Value.GetComponent <SetGlobalGameObject>();
                                            if (mSet == null)
                                            {
                                                mSet = mParam.Value.AddComponent <SetGlobalGameObject>();
                                            }
                                            mSet.variableName = mParam.Name;
                                        }
                                    }
                                    EditorUtility.SetDirty(mParam);
                                }
                            }
                            else
                            {
                                paramObject.Update();
                                if (prop != null)
                                {
                                    EditorGUILayout.PropertyField(prop, new GUIContent(name), true);
                                }
                                paramObject.ApplyModifiedProperties();
                            }

                            if (GUILayout.Button("down", EditorStyles.toolbarButton, GUILayout.Width(35)))
                            {
                                if (i < groupParameters[kvp.Key].Count)
                                {
                                    int indexToMove = Array.FindIndex(globalVariables.Variables, x => x.Name == parameter.Name);
                                    globalVariables.Variables.Move(indexToMove, 0);
                                    EditorUtility.SetDirty(globalVariables);
                                }
                            }
                            if (GUILayout.Button("up", EditorStyles.toolbarButton, GUILayout.Width(20)))
                            {
                                if (i > 0)
                                {
                                    int indexToMove = Array.FindIndex(globalVariables.Variables, x => x.Name == parameter.Name);
                                    globalVariables.Variables.Move(indexToMove, 1);
                                    EditorUtility.SetDirty(globalVariables);
                                }
                            }

                            if (GUILayout.Button(parameter.Group, EditorStyles.toolbarDropDown, GUILayout.Width(54)))
                            {
                                GenericMenu menu = new GenericMenu();
                                foreach (FsmVariable p in globalVariables.Variables)
                                {
                                    string      group  = p.Group;
                                    FsmVariable mParam = parameter;
                                    menu.AddItem(new GUIContent(group), mParam.Group == group, delegate() {
                                        mParam.Group = group;
                                    });
                                }
                                menu.ShowAsContext();
                            }

                            if (GUILayout.Button(EditorGUIUtility.FindTexture("Toolbar Minus"), "label", GUILayout.Width(20)))
                            {
                                if (parameter is FsmGameObject)
                                {
                                    string scenePath = (parameter as FsmGameObject).ScenePath;

                                    scenePath = scenePath.Substring(scenePath.IndexOf("(") + 1);
                                    scenePath = scenePath.Substring(0, scenePath.IndexOf(")"));

                                                                        #if UNITY_4_6 || UNITY_4_7 || UNITY_5_0 || UNITY_5_1 || UNITY_5_2
                                    if (EditorApplication.currentScene != scenePath.Split('/').Last())
                                    {
                                        EditorApplication.SaveScene();
                                        EditorApplication.OpenScene(scenePath);
                                    }
                                                                        #else
                                    UnityEditor.SceneManagement.EditorSceneManager.SaveOpenScenes();
                                    UnityEngine.SceneManagement.Scene scene = UnityEditor.SceneManagement.EditorSceneManager.OpenScene(scenePath);
                                    UnityEditor.SceneManagement.EditorSceneManager.SetActiveScene(scene);
                                                                        #endif

                                    List <SetGlobalGameObject> gos = FindObjectsOfType <SetGlobalGameObject>().ToList();
                                    SetGlobalGameObject        go  = gos.Find(x => x.variableName == parameter.Name);
                                    if (go != null)
                                    {
                                        DestroyImmediate(go);
                                    }
                                }
                                globalVariables.Variables = ArrayUtility.Remove(globalVariables.Variables, parameter);
                                FsmEditorUtility.DestroyImmediate(parameter);;
                                EditorUtility.SetDirty(globalVariables);
                                                                #if UNITY_4_6 || UNITY_4_7 || UNITY_5_0 || UNITY_5_1 || UNITY_5_2
                                EditorApplication.SaveScene();
                                                                #else
                                UnityEditor.SceneManagement.EditorSceneManager.SaveOpenScenes();
                                                                #endif
                            }

                            GUILayout.EndHorizontal();
                        }
                    }
                }
            }
            GUILayout.EndScrollView();
        }
        private void DrawVariables()
        {
            scroll = GUILayout.BeginScrollView(scroll);
            Dictionary <string, List <FsmVariable> > groupParameters = GetGroupVariables();

            EditorGUIUtility.labelWidth = 110;
            foreach (var kvp in groupParameters)
            {
                bool foldout = EditorPrefs.GetBool(kvp.Key, false);
                bool state   = EditorGUILayout.Foldout(foldout, kvp.Key);
                if (state != foldout)
                {
                    EditorPrefs.SetBool(kvp.Key, state);
                }
                if (foldout)
                {
                    for (int i = 0; i < groupParameters[kvp.Key].Count; i++)
                    {
                        FsmVariable parameter = groupParameters[kvp.Key][i];
                        if (parameter != null)
                        {
                            SerializedObject   paramObject = new SerializedObject(parameter);
                            SerializedProperty prop        = paramObject.FindProperty("value");
                            GUILayout.BeginHorizontal();
                            GUILayout.Space(16f);
                            string name = paramObject.FindProperty("name").stringValue;
                            if (parameter is FsmGameObject)
                            {
                                GUI.changed = false;
                                FsmGameObject mParam = parameter as FsmGameObject;
                                if (string.IsNullOrEmpty(mParam.ScenePath))
                                {
                                    mParam.Value = (GameObject)EditorGUILayout.ObjectField(name, mParam.Value, typeof(UnityEngine.GameObject), true);
                                }
                                else
                                {
                                    GUILayout.Label(name, GUILayout.Width(106));
                                    GUILayout.Label(mParam.ScenePath, FsmEditorStyles.wrappedLabelLeft);
                                    GUILayout.FlexibleSpace();
                                }

                                if (GUI.changed)
                                {
                                    if (!EditorUtility.IsPersistent(mParam.Value) && mParam.Value is GameObject)
                                    {
                                        mParam.ScenePath = mParam.Value.name + "(" + EditorApplication.currentScene + ")";
                                        SetGlobalGameObject mSet = mParam.Value.GetComponent <SetGlobalGameObject>();
                                        if (mSet == null)
                                        {
                                            mSet = mParam.Value.AddComponent <SetGlobalGameObject>();
                                        }
                                        mSet.variableName = mParam.Name;
                                    }
                                    EditorUtility.SetDirty(mParam);
                                }
                            }
                            else
                            {
                                paramObject.Update();
                                if (prop != null)
                                {
                                    EditorGUILayout.PropertyField(prop, new GUIContent(name), true);
                                }
                                paramObject.ApplyModifiedProperties();
                            }

                            if (GUILayout.Button("down", EditorStyles.toolbarButton, GUILayout.Width(35)))
                            {
                                if (i < groupParameters[kvp.Key].Count)
                                {
                                    int indexToMove = Array.FindIndex(globalVariables.Variables, x => x.Name == parameter.Name);
                                    globalVariables.Variables.Move(indexToMove, 0);
                                    EditorUtility.SetDirty(globalVariables);
                                }
                            }
                            if (GUILayout.Button("up", EditorStyles.toolbarButton, GUILayout.Width(20)))
                            {
                                if (i > 0)
                                {
                                    int indexToMove = Array.FindIndex(globalVariables.Variables, x => x.Name == parameter.Name);
                                    globalVariables.Variables.Move(indexToMove, 1);
                                    EditorUtility.SetDirty(globalVariables);
                                }
                            }

                            if (GUILayout.Button(parameter.Group, EditorStyles.toolbarDropDown, GUILayout.Width(54)))
                            {
                                GenericMenu menu = new GenericMenu();
                                foreach (FsmVariable p in globalVariables.Variables)
                                {
                                    string      group  = p.Group;
                                    FsmVariable mParam = parameter;
                                    menu.AddItem(new GUIContent(group), mParam.Group == group, delegate() {
                                        mParam.Group = group;
                                    });
                                }
                                menu.ShowAsContext();
                            }

                            if (GUILayout.Button(EditorGUIUtility.FindTexture("Toolbar Minus"), "label", GUILayout.Width(20)))
                            {
                                globalVariables.Variables = ArrayUtility.Remove(globalVariables.Variables, parameter);
                                FsmEditorUtility.DestroyImmediate(parameter);;
                                EditorUtility.SetDirty(globalVariables);
                            }

                            GUILayout.EndHorizontal();
                        }
                    }
                }
            }
            GUILayout.EndScrollView();
        }