Example #1
0
        public void GenerateHandler(string backupCode, string baseType = null)
        {
            if (string.IsNullOrEmpty(baseType))
            {
                baseType = _selectedType.BaseType.Name;
            }

            var handlerCode = backupCode;

            if (string.IsNullOrEmpty(handlerCode))
            {
                handlerCode = UIManCodeGenerator.GenerateHandler(_selectedType.Name, baseType, _config, this.namespaceField.Text);
            }
            else
            {
                handlerCode = handlerCode.Replace($": {_selectedType.BaseType.Name}", $": {baseType}");
            }

            var saved = UIManCodeGenerator.SaveScript(_handlerScriptPath, handlerCode, false, _selectedType.BaseType.Name, baseType);

            if (saved)
            {
                AssetDatabase.Refresh(ImportAssetOptions.Default);
            }
        }
Example #2
0
        public void GenerateHandler(string scriptPath)
        {
            var handlerScriptPath = UIManCodeGenerator.GeneratPathWithSubfix(scriptPath, ".Handler.cs");
            var config            = EditorHelper.GetOrCreateScriptableObject <UIManConfig>(false);
            var handlerCode       = UIManCodeGenerator.GenerateHandler(this.typeName, this.baseType, config, this.namespaceField.Text);

            UIManCodeGenerator.SaveScript(handlerScriptPath, handlerCode, false, this.typeName, this.baseType);
        }
Example #3
0
        public void SaveCurrentType(bool warning = false, string baseType = null)
        {
            // Verify properties list
            for (var 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 (var 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;
            }

            var inheritance = baseType == nameof(ObservableModel) ? $" : {baseType}" : string.Empty;

            if (!string.IsNullOrEmpty(_currentScriptPath))
            {
                var backupCode = UIManCodeGenerator.DeleteScript(_handlerScriptPath);
                var code       = UIManCodeGenerator.GenerateType(_selectedType.Name, inheritance, _selectedTypeIsSealed, _config, this.namespaceField.Text, _selectedProperties);

                var saved = UIManCodeGenerator.SaveScript(_currentScriptPath, code, true);

                if (baseType != nameof(ObservableModel))
                {
                    GenerateHandler(backupCode, baseType);
                    saved = false;
                }

                if (saved)
                {
                    AssetDatabase.Refresh(ImportAssetOptions.Default);
                }
            }
        }
Example #4
0
 public void OnSelecType(string typeName)
 {
     _config.selectedType  = typeName;
     _selectedType         = UIManEditorReflection.GetTypeByName(typeName);
     _selectedTypeIsSealed = _selectedType.IsSealed;
     _selectedProperties   = _selectedType.GetUIManProperties(true);
     this.namespaceField   = new TextFieldHelper(_selectedType.Namespace);
     this.baseTypePopup    = new EditablePopup(_arrSupportType, _selectedType.BaseType.Name, OnChangeBaseType);
     _currentScriptPath    = UIManCodeGenerator.GetScriptPathByType(_selectedType);
     _handlerScriptPath    = UIManCodeGenerator.GeneratPathWithSubfix(_currentScriptPath, ".Handler.cs");
     CachePropertiesDrawer();
 }
Example #5
0
        private void DrawHeaderButtons()
        {
            GUILayout.BeginHorizontal();

            if (!File.Exists(_handlerScriptPath))
            {
                if (ColorButton.Draw("Generate Type Handler", CommonColor.LightGreen, GUILayout.Height(30)))
                {
                    var backupCode = UIManCodeGenerator.DeleteScript(_handlerScriptPath);
                    GenerateHandler(backupCode, _selectedType.BaseType.Name);
                }
            }
            else
            {
                if (ColorButton.Draw("Edit Type Handler", CommonColor.LightBlue, GUILayout.Height(30)))
                {
                    var handler = UIManCodeGenerator.GetScriptPathByType(_selectedType);
                    handler = handler.Replace(".cs", ".Handler.cs");
                    UnityEditorInternal.InternalEditorUtility.OpenFileAtLineExternal(handler, 1);
                }
            }

            if (ColorButton.Draw("Edit Type View", CommonColor.LightBlue, GUILayout.Height(30)))
            {
                GameObject         prefabInstance;
                UnityEngine.Object obj = FindObjectOfType(_selectedType);
                if (obj != null)
                {
                    prefabInstance = ((MonoBehaviour)obj).gameObject;
                }
                else
                {
                    var        isDialog     = _selectedType.BaseType == typeof(UIManDialog);
                    var        prefabFolder = GetUIPrefabPath(_selectedType, isDialog);
                    var        prefabFile   = _selectedType.Name + PREFAB_EXT;
                    var        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)))
            {
                var cs      = UIManCodeGenerator.GetScriptPathByType(_selectedType);
                var handler = cs.Replace(".cs", ".Handler.cs");
                AssetDatabase.DeleteAsset(cs);
                AssetDatabase.DeleteAsset(handler);

                var isDialog     = _selectedType.BaseType == typeof(UIManDialog);
                var prefabFolder = GetUIPrefabPath(_selectedType, isDialog);
                var prefabFile   = _selectedType.Name + PREFAB_EXT;
                var prefabPath   = UIManDefine.ASSETS_FOLDER + prefabFolder + prefabFile;
                AssetDatabase.DeleteAsset(prefabPath);
                AssetDatabase.Refresh();
            }

            GUILayout.EndHorizontal();
            LineHelper.Draw(Color.gray);
        }
Example #6
0
        public override void OnInspectorGUI()
        {
            var uiManBase = (UIManBase)this.target;

            this.orgBgColor = GUI.backgroundColor;

            GUI.backgroundColor = CommonColor.LightOrange;
            GUILayout.BeginHorizontal("Box");
            LabelHelper.HeaderLabel(string.Format("UIMan View Model ({0})", uiManBase.GetUIBaseType()));
            GUILayout.EndHorizontal();

            GUI.backgroundColor = this.orgBgColor;

            LineHelper.Draw(Color.gray);

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

            EditorGUI.BeginChangeCheck();

            if (uiManBase is UIManDialog dialog)
            {
                dialog.useCover = EditorGUILayout.Toggle(this.cover, dialog.useCover);
            }
            else if (uiManBase is UIManScreen screen)
            {
                screen.useBackground = EditorGUILayout.Toggle(this.background, screen.useBackground);
                if (screen.useBackground)
                {
                    screen.background = EditorGUILayout.TextField(screen.background);
                }
            }

            if (EditorGUI.EndChangeCheck())
            {
                EditorUtility.SetDirty(this.target);
            }

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

                uiManBase.animRoot = EditorGUILayout.ObjectField(this.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(this.show, uiManBase.motionShow);
            uiManBase.motionHide = (UIMotion)EditorGUILayout.EnumPopup(this.hide, uiManBase.motionHide);
            uiManBase.motionIdle = (UIMotion)EditorGUILayout.EnumPopup(this.idle, uiManBase.motionIdle);

            var motions = new UIMotion[3] {
                uiManBase.motionShow, uiManBase.motionHide, uiManBase.motionIdle
            };
            var haveMecanimAnim = false;
            var 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.CustomMecanimAnimation && 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(this.time, uiManBase.animTime);
            uiManBase.showPosition = EditorGUILayout.Vector3Field(this.position, uiManBase.showPosition);

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

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

            EditorGUILayout.Space();
            if (ColorButton.Draw("Edit View (UI)", CommonColor.LightGreen, GUILayout.Height(25)))
            {
                GameObject prefabInstance;
                Object     obj = FindObjectOfType(uiManBase.UIType);
                if (obj != null)
                {
                    prefabInstance = ((MonoBehaviour)obj).gameObject;
                }
                else
                {
                    var isDialog = uiManBase.GetUIBaseType() == UIBaseType.Dialog;
                    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 (ColorButton.Draw("Edit View Logic (Handler)", CommonColor.LightGreen, GUILayout.Height(25)))
            {
                var handler = UIManCodeGenerator.GetScriptPathByType(this.target.GetType());
                handler = handler.Replace(".cs", ".Handler.cs");
                UnityEditorInternal.InternalEditorUtility.OpenFileAtLineExternal(handler, 1);
            }
        }
Example #7
0
        public void GenerateType()
        {
            if (this.typeName.Contains(" "))
            {
                EditorUtility.DisplayDialog("Error", "Class name cannot constain special characters", "OK");
                return;
            }

            var warn = false;

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

            this.baseType = this.baseTypePopup.SelectedItem;

            var config   = EditorHelper.GetOrCreateScriptableObject <UIManConfig>(false);
            var savePath = "";

            if (this.baseType.Equals(UIGenerator.GetSupportTypeName(0)))
            {
                savePath = config.modelScriptFolder;
                config.generatingTypeIsDialog = false;
            }
            else if (this.baseType.Equals(UIGenerator.GetSupportTypeName(1)))
            {
                savePath = config.screenScriptFolder;
                config.generatingTypeIsDialog = false;
            }
            else if (this.baseType.Equals(UIGenerator.GetSupportTypeName(2)))
            {
                savePath = config.dialogScriptFolder;
                config.generatingTypeIsDialog = true;
            }

            savePath = Application.dataPath + "/" + savePath + "/" + this.typeName + ".cs";

            if (File.Exists(savePath) || UIGenerator.IsViewModelExisted(this.typeName))
            {
                EditorUtility.DisplayDialog("Error", "A class of the same name has already existed", "OK");
                return;
            }

            var paths       = Regex.Split(savePath, "/");
            var inheritance = string.Empty;

            if (this.baseType != this.arrSupportType[0])
            {
                config.generatingType = this.typeName;
            }
            else
            {
                inheritance = $" : {this.baseType}";
            }

            var code = UIManCodeGenerator.GenerateType(this.typeName, inheritance, false, config, this.namespaceField.Text);

            UIManCodeGenerator.SaveScript(savePath, code, true);

            if (this.baseType != this.arrSupportType[0])
            {
                GenerateHandler(savePath);
            }

            AssetDatabase.Refresh(ImportAssetOptions.Default);

            if (warn)
            {
                UnuLogger.LogWarning("Code generation warning: Class name is invalid. New name is generated.");
            }

            Close();
        }