示例#1
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(UIGenerator.GetSupportTypeName(0))))
            {
                typeName = "UI" + typeName;
                warn     = true;
            }

            baseType = baseTypePopup.SelectedItem;


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

            string savePath = "";

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

            savePath = Application.dataPath + "/" + savePath + "/" + typeName + ".cs";
            if (File.Exists(savePath) || UIGenerator.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", "");

            if (baseType != arrSupportType[0])
            {
                config.generatingType = typeName;
            }

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

            CodeGenerationHelper.SaveScript(savePath, code, true);

            if (baseType != arrSupportType[0])
            {
                GenerateViewModelHandler(savePath);
            }

            AssetDatabase.Refresh(ImportAssetOptions.Default);

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

            Close();
        }
示例#2
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();
        }