Beispiel #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(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 #2
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);
                }
            }
        }