Beispiel #1
0
        public override string ToString()
        {
            string strDefaultValue = "";

            if (PropertyType == typeof(string))
            {
                strDefaultValue = DefaltValue == null ? "\"null\"" : "\"" + DefaltValue + "\"";
            }
            else if (PropertyType.BaseType == typeof(ObservableModel))
            {
                strDefaultValue = "new " + PropertyType.Name + "()";
            }
            else
            {
                strDefaultValue = DefaltValue.ToString();
            }
            if (PropertyType == typeof(bool))
            {
                strDefaultValue = strDefaultValue.ToLower();
            }
            string fieldName    = CodeGenerationHelper.NormalizeFieldName(Name);
            string propertyName = CodeGenerationHelper.NormalizePropertyName(Name);
            string field        = string.Format("\t{0} _{1} = {2};", PropertyType.GetAllias(), fieldName, strDefaultValue);
            string attribute    = "\t[UIManProperty]";
            string property     = string.Format("\tpublic {0} {1} {{", PropertyType.GetAllias(), propertyName);
            string getter       = string.Format("\t\tget {{ return _{0}; }}", fieldName);
            string setter       = string.Format("\t\tset {{ _{0} = value; OnPropertyChanged(); }}", fieldName);
            string code         = NewLine() + field + NewLine() + attribute + NewLine() + property + NewLine() + getter + NewLine() + setter + NewLine() + "\t}";

            return(code);
        }
Beispiel #2
0
        public void GenerateViewModel()
        {
            if (typeName.Contains(" "))
            {
                EditorUtility.DisplayDialog("Error", "View model name cannot constain special character", "OK");
                return;
            }

            bool warn = false;

            if (typeName.Length <= 1 || (!typeName.Substring(0, 2).Equals("UI") && !baseTypePopup.SelectedItem.Equals(CodeGenerator.GetSupportTypeName(0))))
            {
                typeName = "UI" + typeName;
                warn     = true;
            }

            baseType = baseTypePopup.SelectedItem;

            UIManConfig config = Resources.Load <UIManConfig> ("UIManConfig");

            string savePath = "";

            if (baseType.Equals(CodeGenerator.GetSupportTypeName(1)))
            {
                savePath = config.screenScriptFolder;
            }
            else if (baseType.Equals(CodeGenerator.GetSupportTypeName(2)))
            {
                savePath = config.dialogScriptFolder;
            }

            savePath = Application.dataPath + "/" + savePath + "/" + typeName + ".cs";
            if (File.Exists(savePath) || CodeGenerator.IsViewModelExisted(typeName))
            {
                EditorUtility.DisplayDialog("Error", "View model name is already exist, please input other name!", "OK");
                return;
            }

            string[] paths      = Regex.Split(savePath, "/");
            string   scriptName = paths [paths.Length - 1];

            scriptName = scriptName.Replace(".cs", "");

            config.generatingType = typeName;
            string code = CodeGenerationHelper.GenerateScript(typeName, baseType);

            CodeGenerationHelper.SaveScript(savePath, code, true);
            GenerateViewModelHandler(savePath);
            AssetDatabase.Refresh(ImportAssetOptions.Default);

            if (warn)
            {
                Debug.LogWarning("Code generation warning: Invalid name detected, auto generate is activated!");
            }

            Close();
        }
Beispiel #3
0
 public void OnSelecType(string typeName)
 {
     config.selectedType = typeName;
     selectedType        = ReflectUtils.GetTypeByName(typeName);
     selectedProperties  = selectedType.GetUIManProperties();
     baseTypePopup       = new EditablePopup(arrSupportType, selectedType.BaseType.Name, OnChangeBaseType);
     currentScriptPath   = CodeGenerationHelper.GetScriptPathByType(selectedType);
     handlerScriptPath   = CodeGenerationHelper.GeneratPathWithSubfix(currentScriptPath, ".Handler.cs");
     CachePropertiesDrawer();
 }
Beispiel #4
0
        public void SaveCurrentType(bool warning = false, string baseType = null)
        {
            // Verify properties list
            for (int i = 0; i < selectedProperties.Length; i++)
            {
                CustomPropertyInfo property = selectedProperties [i];
                if (string.IsNullOrEmpty(property.Name) || Char.IsNumber(property.Name [0]))
                {
                    property.Name = "";
                    if (warning)
                    {
                        EditorUtility.DisplayDialog("Save script error", "Property name cannot be a digit, null or empty!", "OK");
                    }
                    return;
                }

                for (int j = 0; j < selectedProperties.Length; j++)
                {
                    if (j != i && selectedProperties [i].Name.ToLower() == selectedProperties [j].Name.ToLower())
                    {
                        selectedProperties [j].Name = "";
                        if (warning)
                        {
                            EditorUtility.DisplayDialog("Save script error", "There are one or more properties are have the same name!", "OK");
                        }
                        return;
                    }
                }
            }

            if (baseType == null)
            {
                baseType = selectedType.BaseType.Name;
            }

            if (!string.IsNullOrEmpty(currentScriptPath))
            {
                string backupCode = CodeGenerationHelper.DeleteScript(handlerScriptPath);
                string code       = CodeGenerationHelper.GenerateScript(selectedType.Name, baseType, selectedProperties);

                bool saved = CodeGenerationHelper.SaveScript(currentScriptPath, code, true);

                if (baseType != "ObservableModel")
                {
                    GenerateViewModelHandler(backupCode, baseType);
                    saved = false;
                }

                if (saved)
                {
                    AssetDatabase.Refresh(ImportAssetOptions.Default);
                }
            }
        }
Beispiel #5
0
        public void GenerateViewModelHandler(string scriptPath)
        {
            string handlerScriptPath = CodeGenerationHelper.GeneratPathWithSubfix(scriptPath, ".Handler.cs");
            string handlerCode       = "";

            if (string.IsNullOrEmpty(handlerCode))
            {
                handlerCode = CodeGenerationHelper.GenerateViewModelHandler(typeName, baseType);
            }
            else
            {
                handlerCode = handlerCode.Replace(": " + typeName, ": " + baseType);
            }
            CodeGenerationHelper.SaveScript(handlerScriptPath, handlerCode, false, typeName, baseType);
        }
Beispiel #6
0
        public override void OnInspectorGUI()
        {
            DrawDefaultInspector();

            UIManBase uiManBase = (UIManBase)target;

            if (uiManBase is UIManDialog)
            {
                UIManDialog dialog = (UIManDialog)uiManBase;
                dialog.useCover = EditorGUILayout.Toggle(cover, dialog.useCover);
                EditorUtility.SetDirty(target);
            }

            if (uiManBase.motionShow == UIMotion.CUSTOM_MECANIM_ANIMATION || uiManBase.motionHide == UIMotion.CUSTOM_MECANIM_ANIMATION)
            {
                if (uiManBase.gameObject != null)
                {
                    uiManBase.animRoot = uiManBase.gameObject.GetComponent <Animator> ();
                }

                uiManBase.animRoot = EditorGUILayout.ObjectField(animator, uiManBase.animRoot, typeof(Animator), true) as Animator;

                if (uiManBase.animRoot == null || uiManBase.animRoot.runtimeAnimatorController == null)
                {
                    if (GUILayout.Button("Generate Animator"))
                    {
                        AnimationEditorUtils.GenerateAnimator(uiManBase.gameObject, UIManDefine.ANIM_SHOW, UIManDefine.ANIM_HIDE, UIManDefine.ANIM_IDLE);
                    }
                }
            }

            uiManBase.motionShow   = (UIMotion)EditorGUILayout.EnumPopup(show, uiManBase.motionShow);
            uiManBase.motionHide   = (UIMotion)EditorGUILayout.EnumPopup(hide, uiManBase.motionHide);
            uiManBase.motionIdle   = (UIMotion)EditorGUILayout.EnumPopup(idle, uiManBase.motionIdle);
            uiManBase.animTime     = EditorGUILayout.FloatField(time, uiManBase.animTime);
            uiManBase.showPosition = EditorGUILayout.Vector3Field(position, uiManBase.showPosition);

            if (GUILayout.Button("Edit Logic"))
            {
                string handler = CodeGenerationHelper.GetScriptPathByType(target.GetType());
                handler = handler.Replace(".cs", ".Handler.cs");
                UnityEditorInternal.InternalEditorUtility.OpenFileAtLineExternal(handler, 1);
            }
        }
Beispiel #7
0
        public void GenerateViewModelHandler(string backupCode, string baseType = null)
        {
            if (string.IsNullOrEmpty(baseType))
            {
                baseType = selectedType.BaseType.Name;
            }

            string handlerCode = backupCode;

            if (string.IsNullOrEmpty(handlerCode))
            {
                handlerCode = CodeGenerationHelper.GenerateViewModelHandler(selectedType.Name, baseType);
            }
            else
            {
                handlerCode = handlerCode.Replace(": " + selectedType.BaseType.Name, ": " + baseType);
            }
            bool saved = CodeGenerationHelper.SaveScript(handlerScriptPath, handlerCode, false, selectedType.BaseType.Name, baseType);

            if (saved)
            {
                AssetDatabase.Refresh(ImportAssetOptions.Default);
            }
        }
Beispiel #8
0
        void PropertiesWindow(int id = 2)
        {
            GUILayout.BeginVertical();

            if (listTypes != null && !string.IsNullOrEmpty(listTypes.SelectedItem))
            {
                if (selectedType != null)
                {
                    // Title
                    GUILayout.Space(2);
                    LabelHelper.TitleLabel(selectedType.Name);
                    LineHelper.Draw(Color.gray);

                    // Common
                    GUILayout.Space(2);
                    if (selectedType.BaseType != typeof(ObservableModel))
                    {
                        GUILayout.BeginHorizontal();
                        if (ColorButton.Draw("Edit View Logic (Handler)", CommonColor.LightBlue, GUILayout.Height(30)))
                        {
                            string handler = CodeGenerationHelper.GetScriptPathByType(selectedType);
                            handler = handler.Replace(".cs", ".Handler.cs");
                            UnityEditorInternal.InternalEditorUtility.OpenFileAtLineExternal(handler, 1);
                        }

                        if (ColorButton.Draw("Edit View (UI)", CommonColor.LightBlue, GUILayout.Height(30)))
                        {
                            GameObject         prefabInstance;
                            UnityEngine.Object obj = FindObjectOfType(selectedType);
                            if (obj != null)
                            {
                                prefabInstance = ((MonoBehaviour)obj).gameObject;
                            }
                            else
                            {
                                bool       isDialog     = selectedType.BaseType == typeof(UIManDialog);
                                string     prefabFolder = GetUIPrefabPath(selectedType, isDialog);
                                string     prefabFile   = selectedType.Name + PREFAB_EXT;
                                string     prefabPath   = Path.Combine(prefabFolder, prefabFile);
                                GameObject prefab       = AssetDatabase.LoadAssetAtPath <GameObject> (prefabPath);
                                if (prefab == null)
                                {
                                    prefab = FindAssetObject <GameObject> (selectedType.Name, PREFAB_EXT);
                                }

                                prefabInstance = PrefabUtility.InstantiatePrefab(prefab) as GameObject;
                                if (isDialog)
                                {
                                    prefabInstance.transform.SetParent(UIMan.Instance.dialogRoot, false);
                                }
                                else
                                {
                                    prefabInstance.transform.SetParent(UIMan.Instance.screenRoot, false);
                                }
                            }
                            Selection.activeGameObject = prefabInstance;
                        }

                        if (ColorButton.Draw("Delete", CommonColor.LightRed, GUILayout.Height(30)))
                        {
                            string cs      = CodeGenerationHelper.GetScriptPathByType(selectedType);
                            string handler = cs.Replace(".cs", ".Handler.cs");
                            AssetDatabase.DeleteAsset(cs);
                            AssetDatabase.DeleteAsset(handler);

                            bool   isDialog     = selectedType.BaseType == typeof(UIManDialog);
                            string prefabFolder = GetUIPrefabPath(selectedType, isDialog);
                            string prefabFile   = selectedType.Name + PREFAB_EXT;
                            string prefabPath   = UIManDefine.ASSETS_FOLDER + prefabFolder + prefabFile;
                            AssetDatabase.DeleteAsset(prefabPath);
                            AssetDatabase.Refresh();
                        }
                        GUILayout.EndHorizontal();
                        LineHelper.Draw(Color.gray);
                    }

                    // Base type
                    GUILayout.Space(10);
                    LabelHelper.HeaderLabel("Type");
                    LineHelper.Draw(Color.gray);
                    baseTypePopup.Draw();

                    if (baseTypePopup.SelectedItem != "ObservableModel")
                    {
                        if (!System.IO.File.Exists(handlerScriptPath))
                        {
                            if (GUILayout.Button("Generate Handler"))
                            {
                                string backupCode = CodeGenerationHelper.DeleteScript(handlerScriptPath);
                                GenerateViewModelHandler(backupCode, selectedType.BaseType.Name);
                            }
                        }
                    }

                    // Properties
                    GUILayout.Space(10);
                    LabelHelper.HeaderLabel("Properties");
                    LineHelper.Draw(Color.gray);

                    propertiesScrollPos = EditorGUILayout.BeginScrollView(propertiesScrollPos, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));
                    if (propertiesDrawerCache.ContainsKey(selectedType))
                    {
                        EditablePropertyDrawer[] props = propertiesDrawerCache [selectedType];
                        for (int i = 0; i < props.Length; i++)
                        {
                            props [i].Draw(propertiesAreaWidth);
                        }
                    }
                    EditorGUILayout.EndScrollView();
                }

                GUILayout.Space(10);

                // Add property
                if (ColorButton.Draw("Add New Property", CommonColor.LightGreen, GUILayout.Height(30)))
                {
                    int    newIndex    = 0;
                    string strNewIndex = "";
                    for (int i = 0; i < selectedProperties.Length; i++)
                    {
                        if (selectedProperties [i].LastName.Contains("NewProperty"))
                        {
                            newIndex++;
                        }
                    }
                    if (newIndex > 0)
                    {
                        strNewIndex = newIndex.ToString();
                    }
                    CustomPropertyInfo newProperty = new CustomPropertyInfo("", typeof(string));
                    newProperty.LastName = "NewProperty" + strNewIndex;
                    ArrayUtility.Add <CustomPropertyInfo> (ref selectedProperties, newProperty);
                    CachePropertiesDrawer();
                }

                //Save all change
                CustomPropertyInfo[] changeList   = new CustomPropertyInfo[0];
                CustomPropertyInfo[] selectedList = new CustomPropertyInfo[0];
                for (int i = 0; i < selectedProperties.Length; i++)
                {
                    if (selectedProperties [i].HasChange)
                    {
                        ArrayUtility.Add(ref changeList, selectedProperties [i]);
                    }
                    if (selectedProperties [i].IsSelected)
                    {
                        ArrayUtility.Add(ref selectedList, selectedProperties [i]);
                    }
                }

                GUILayout.Space(10);
                LineHelper.Draw(Color.gray);
                GUILayout.Space(5);

                if (changeList.Length > 0)
                {
                    if (ColorButton.Draw("Save All Changes", CommonColor.LightGreen, GUILayout.Height(30)))
                    {
                        for (int i = 0; i < changeList.Length; i++)
                        {
                            changeList [i].CommitChange();
                        }
                        SaveCurrentType(true, baseTypePopup.SelectedItem);
                    }
                }

                if (selectedList.Length > 0)
                {
                    if (ColorButton.Draw("Delete Selected Properties", CommonColor.LightRed, GUILayout.Height(30)))
                    {
                        for (int i = 0; i < selectedList.Length; i++)
                        {
                            ArrayUtility.Remove(ref selectedProperties, selectedList [i]);
                        }
                        SaveCurrentType(true, baseTypePopup.SelectedItem);
                        CachePropertiesDrawer(true);
                    }
                }

                if (selectedProperties.Length > 0)
                {
                    if (ColorButton.Draw("Delete All Properties", CommonColor.LightRed, GUILayout.Height(30)))
                    {
                        while (selectedProperties.Length > 0)
                        {
                            ArrayUtility.Clear(ref selectedProperties);
                            SaveCurrentType();
                            CachePropertiesDrawer(true);
                        }
                    }
                }
            }
            else
            {
                GUILayout.Label("NO DATA FOR PREVIEW!");
            }


            GUILayout.EndVertical();
        }
Beispiel #9
0
        public override void OnInspectorGUI()
        {
            GUILayout.BeginHorizontal("Box");
            LabelHelper.HeaderLabel("UIMan View Model");
            GUILayout.EndHorizontal();
            LineHelper.Draw(Color.gray);

            UIManBase uiManBase = (UIManBase)target;

            EditorGUILayout.Space();
            LabelHelper.HeaderLabel("General");
            GUILayout.BeginVertical("Box");

            if (uiManBase is UIManDialog)
            {
                UIManDialog dialog = (UIManDialog)uiManBase;
                dialog.useCover = EditorGUILayout.Toggle(cover, dialog.useCover);
                EditorUtility.SetDirty(target);
            }
            else if (uiManBase is UIManBase)
            {
                UIManScreen screen = (UIManScreen)uiManBase;
                screen.useBackground = EditorGUILayout.Toggle(background, screen.useBackground);
                if (screen.useBackground)
                {
                    screen.backgroundType = EditorGUILayout.TextField(screen.backgroundType);
                }
                EditorUtility.SetDirty(target);
            }

            if (uiManBase.motionShow == UIMotion.CUSTOM_MECANIM_ANIMATION || uiManBase.motionHide == UIMotion.CUSTOM_MECANIM_ANIMATION)
            {
                if (uiManBase.gameObject != null)
                {
                    uiManBase.animRoot = uiManBase.gameObject.GetComponent <Animator> ();
                }

                uiManBase.animRoot = EditorGUILayout.ObjectField(animator, uiManBase.animRoot, typeof(Animator), true) as Animator;

                if (uiManBase.animRoot == null || uiManBase.animRoot.runtimeAnimatorController == null)
                {
                    if (GUILayout.Button("Generate Animator"))
                    {
                        AnimationEditorUtils.GenerateAnimator(uiManBase.gameObject, UIManDefine.ANIM_SHOW, UIManDefine.ANIM_HIDE, UIManDefine.ANIM_IDLE);
                    }
                }
            }

            uiManBase.motionShow = (UIMotion)EditorGUILayout.EnumPopup(show, uiManBase.motionShow);
            uiManBase.motionHide = (UIMotion)EditorGUILayout.EnumPopup(hide, uiManBase.motionHide);
            uiManBase.motionIdle = (UIMotion)EditorGUILayout.EnumPopup(idle, uiManBase.motionIdle);

            UIMotion[] motions = new UIMotion[3] {
                uiManBase.motionShow, uiManBase.motionHide, uiManBase.motionIdle
            };
            bool haveMecanimAnim = false;
            bool haveTweenAnim   = false;

            foreach (UIMotion m in motions)
            {
                if ((int)m == 7)
                {
                    haveMecanimAnim = true;
                }
                else
                {
                    haveTweenAnim = true;
                }
            }
            if (haveTweenAnim && haveMecanimAnim)
            {
                GUILayout.BeginHorizontal("Box");
                EditorGUILayout.LabelField("<color=red><b>Warning: </b>Your motion type is not match with each others so it maybe cause unexpected error!\nPlease select all motion type as Mecanim if you want to make you animation manually with Unity animation editor!</color>", EditorGUIHelper.RichText(true));
                GUILayout.EndHorizontal();
            }

            if (uiManBase.motionIdle != UIMotion.CUSTOM_MECANIM_ANIMATION && uiManBase.motionIdle != UIMotion.NONE)
            {
                GUILayout.BeginHorizontal("Box");
                EditorGUILayout.LabelField("<color=red><b>Warning: </b>Idle motion is now only support Mecanim animation!</color>", EditorGUIHelper.RichText(true));
                GUILayout.EndHorizontal();
            }

            uiManBase.animTime     = EditorGUILayout.FloatField(time, uiManBase.animTime);
            uiManBase.showPosition = EditorGUILayout.Vector3Field(position, uiManBase.showPosition);

            GUILayout.EndVertical();
            LineHelper.Draw(Color.gray);

            EditorGUILayout.Space();
            LabelHelper.HeaderLabel("Custom fields");
            GUILayout.BeginVertical("Box");
            DrawDefaultInspector();
            GUILayout.EndVertical();

            EditorGUILayout.Space();
            if (GUILayout.Button("Edit View (UI)", GUILayout.Height(25)))
            {
                GameObject         prefabInstance;
                UnityEngine.Object obj = FindObjectOfType(uiManBase.UIType);
                if (obj != null)
                {
                    prefabInstance = ((MonoBehaviour)obj).gameObject;
                }
                else
                {
                    bool isDialog = uiManBase.GetUIBaseType() == UIBaseType.DIALOG;
//					string prefabFolder = GetUIPrefabPath (selectedType, isDialog);
//					string prefabFile = selectedType.Name + PREFAB_EXT;
//					string prefabPath = Path.Combine (prefabFolder, prefabFile);
//					GameObject prefab = AssetDatabase.LoadAssetAtPath<GameObject> (prefabPath);
//					if (prefab == null) {
//						prefab = FindAssetObject<GameObject> (selectedType.Name, PREFAB_EXT);
//					}

                    prefabInstance = PrefabUtility.InstantiatePrefab(uiManBase.gameObject) as GameObject;
                    if (isDialog)
                    {
                        prefabInstance.transform.SetParent(UIMan.Instance.dialogRoot, false);
                    }
                    else
                    {
                        prefabInstance.transform.SetParent(UIMan.Instance.screenRoot, false);
                    }
                }
                Selection.activeGameObject = prefabInstance;
            }
            if (GUILayout.Button("Edit View Logic (Handler)", GUILayout.Height(25)))
            {
                string handler = CodeGenerationHelper.GetScriptPathByType(target.GetType());
                handler = handler.Replace(".cs", ".Handler.cs");
                UnityEditorInternal.InternalEditorUtility.OpenFileAtLineExternal(handler, 1);
            }
        }