Esempio n. 1
0
        private static void CreatePersonType(string gendir, string className, string parentName)
        {
            FileUtil.CreateFolder(gendir);

            var fullpath = gendir + "/" + className + ".cs";
            if (File.Exists(fullpath))
                return;

            var stream = File.CreateText(fullpath);

            stream.Write("using ReadyGamerOne.Data;\n" +
                         "using ReadyGamerOne.Rougelike.Person;\n" +
                         "using " + rootNs + ".Const;\n" +
                         "namespace " + rootNs + ".Model.Person\n" +
                         "{\n");
            if(createControllers)
                stream.Write("\t[UsePersonController(typeof("+className+"Controller))]\n"); 
            stream.Write("\tpublic partial class "+className+" :\n" +
                         "\t\t"+parentName+"<"+className+">\n" +
                         "\t{\n" +
                         "\t\tpublic override string ResKey => PersonName."+className+";\n" +
                         "\t\tpublic override void LoadData(CsvMgr data)\n" +
                         "\t\t{\n" +
                         "\t\t\t//    如果父类没有此方法,删掉就行\n"+
                         "\t\t\tbase.LoadData(data);\n"+
                         "\t\t\t//    在这里需要使用数据对类内变量进行初始化赋值(比如_hp,_maxhp,_attack,这三项自动生成在基类中了)\n" +
                         "\t\t\t//    这个函数人物每次激活时都会调用\n" +
                         "\t\t}\n" +
                         "\t}\n" +
                         "}\n");
            
            stream.Dispose();
            stream.Close();
        }
        private static void CreateControllerType(string gendir, string className, string parentName)
        {
            FileUtil.CreateFolder(gendir);

            var fullpath = gendir + "/" + className + ".cs";

            if (File.Exists(fullpath))
            {
                return;
            }


            var stream = File.CreateText(fullpath);

            stream.Write("namespace " + rootNs + ".Model.Person\n" +
                         "{\n" +
                         "\tpublic class " + className + " :\n" +
                         "\t\t" + parentName + "\n" +
                         "\t{\n" +
                         "\t\tpublic override float MoveSpeed { get; set; }\n" +
                         "\t\tpublic override void SetMoveable(bool state)\n" +
                         "\t\t{\n" +
                         "\t\t\tthrow new System.NotImplementedException();\n" +
                         "\t\t}\n" +
                         "\t}\n" +
                         "}\n");


            stream.Dispose();
            stream.Close();
        }
        private static void CreateAbstractPersonType(string gendir, DirectoryInfo dirInfo, string curName, string parName)
        {
            var className = rootNs + curName;

            var isRootPerson = new DirectoryInfo(dirInfo.FullName).FullName == new DirectoryInfo(sourDir).FullName;

            FileUtil.CreateFolder(gendir);

            var fileFullPath = gendir + "/" + className + ".cs";

            if (File.Exists(fileFullPath))
            {
                return;
            }

            var stream = File.CreateText(fileFullPath);

            stream.Write("using ReadyGamerOne.Rougelike.Person;\n");


            if (dirInfo.HasValuableFiles())
            {
                stream.Write("using System;\n" +
                             "using " + rootNs + ".Data;\n");
            }


            stream.Write("using UnityEngine;\n\n" +
                         "namespace " + rootNs + ".Model.Person\n" +
                         "{\n" +
                         "\tpublic interface I" + className);

            if (!isRootPerson)
            {
                stream.Write(" : \n" +
                             "\t\tI" + parName);
            }
            stream.Write("\n" +
                         "\t{\n" +
                         "\t}\n\n" +
                         "\tpublic abstract class " + className + "<T>:\n" +
                         "\t\t" + parName + "<T>,\n" +
                         "\t\tI" + className + "\n" +
                         "\t\twhere T : " + className + "<T>,new()\n" +
                         "\t{\n");

            if (isRootPerson)
            {
                stream.Write("\t\t#region Fields\n\n" +
                             "\t\tprotected int _hp;\n" +
                             "\t\tprotected int _maxHp;\n" +
                             "\t\tprotected int _attack;\n\n" +
                             "\t\t#endregion\n\n" +
                             "\t\t#region ITakeDamageablePerson<T>\n\n" +
                             "\t\tpublic override int Hp => _hp;\n" +
                             "\t\tpublic override int MaxHp => _maxHp;\n" +
                             "\t\tpublic virtual void OnTakeDamage(AbstractPerson takeDamageFrom, int damage)\n" +
                             "\t\t{\n" +
                             "\t\t\tDebug.Log($\"{CharacterName}收到来自{takeDamageFrom.CharacterName}的{damage}伤害\");\n\n" +
                             "\t\t\t_hp -= damage;\n" +
                             "\t\t\tif(_hp<0)\n" +
                             "\t\t\t\tKill();\n" +
                             "\t\t}\n\n" +
                             "\t\t public void OnCauseDamage(AbstractPerson causeDamageTo, int damage)\n" +
                             "\t\t{\n" +
                             "\t\t\tDebug.Log($\"{CharacterName}对{causeDamageTo.CharacterName}造成{damage}伤害\");\n" +
                             "\t\t}\n\n" +
                             "\t\t#endregion\n");
            }

            if (dirInfo.HasValuableFiles())
            {
                stream.Write("\t\tpublic override Type DataType => typeof(" + curName + "Data);\n");
            }

            stream.Write("\t}\n" +
                         "}\n");
            stream.Dispose();
            stream.Close();
        }
        static void OnToolsGUI(string rootNs, string viewNs, string constNs, string dataNs, string autoDir,
                               string scriptNs)
        {
            EditorGUILayout.Space();
            EditorGUILayout.Space();

            createPanelClasses = EditorGUILayout.Toggle("是否自动生成Panel类型", createPanelClasses);
            if (createPanelClasses)
            {
                EditorGUILayout.LabelField("自动生成Panel文件路径", Application.dataPath + "/" + rootNs + "/" + viewNs);
            }
            EditorGUILayout.Space();


            createGameMgr = EditorGUILayout.Toggle("是否生成GameMgr", createGameMgr);
            if (createGameMgr)
            {
                _resourceManagerType = (ResourceManagerType)EditorGUILayout.EnumPopup("资源管理类型", _resourceManagerType);
                EditorGUILayout.LabelField("自动生成" + rootNs + "Mgr文件路径",
                                           Application.dataPath + "/" + rootNs + "/" + scriptNs);
            }
            EditorGUILayout.Space();


            createMessage = EditorGUILayout.Toggle("是否生成空Message常量类", createMessage);
            if (createMessage)
            {
                EditorGUILayout.LabelField("生成Message.cs路径",
                                           Application.dataPath + "/" + rootNs + "/" + constNs + "/Message.cs");
            }
            EditorGUILayout.Space();
            createGlobalVar = EditorGUILayout.Toggle("是否重写GlobalVar类", createGlobalVar);
            if (createGlobalVar)
            {
                EditorGUILayout.LabelField("生成GlobalVar.cs路径",
                                           Application.dataPath + "/" + rootNs + "/Global/GlobalVar.cs");
            }
            EditorGUILayout.Space();
            createSceneNameClass = EditorGUILayout.Toggle("是否生成SceneName类", createSceneNameClass);
            if (createSceneNameClass)
            {
                EditorGUILayout.LabelField("生成SceneName.cs路径",
                                           Application.dataPath + "/" + rootNs + "/" + constNs + "/" + autoDir + "/SceneName.cs");
            }
            EditorGUILayout.Space();



            if (GUILayout.Button("开启自动生成", GUILayout.Height(2 * EditorGUIUtility.singleLineHeight)))
            {
                #region 文件夹的创建

                var resourceDir = Application.dataPath + "/Resources";
                var rootDir     = Application.dataPath + "/" + rootNs;
                if (!Directory.Exists(resourceDir))
                {
                    Directory.CreateDirectory(resourceDir);
                    return;
                }

                FileUtil.CreateFolder(rootDir);
                FileUtil.CreateFolder(rootDir + "/" + constNs);
                FileUtil.CreateFolder(rootDir + "/" + constNs + "/" + autoDir);

                #endregion

                autoClassName.Clear();
                otherResPathDic.Clear();
                otherResFileNameDic.Clear();
                allResPathDic.Clear();
                allResFileNameDic.Clear();


                foreach (var fullName in Directory.GetFileSystemEntries(resourceDir))
                {
                    if (Directory.Exists(fullName))
                    {
                        //如果是文件夹
                        OprateDir(new DirectoryInfo(fullName));
                    }
                    else
                    {
                        //是文件
                        OprateFile(new FileInfo(fullName));
                    }
                }


                #region 特殊类的生成

                if (createPanelClasses && autoClassName.Contains("Panel"))
                {
                    CreatePanelFile(Application.dataPath + "/Resources/ClassPanel", viewNs, constNs, rootNs, autoDir);
                }

                #endregion


                #region 定向生成特殊小文件

                if (createGameMgr)
                {
                    CreateMgr(rootNs, constNs, scriptNs, autoDir);
                }

                if (createGlobalVar)
                {
                    FileUtil.CreateClassFile("GlobalVar", rootNs + ".Global", rootDir + "/Global",
                                             "ReadyGamerOne.Global.GlobalVar",
                                             "这里写当前项目需要的全局变量,调用GlobalVar时,最好调用当前这个类");
                }

                if (createMessage)
                {
                    FileUtil.CreateClassFile("Message", rootNs + "." + constNs, rootDir + "/" + constNs,
                                             helpTips: "自定义的消息写到这里");
                }

                if (createSceneNameClass)
                {
                    CreateSceneNameClass(rootNs, constNs, autoDir);
                }

                #endregion


                AssetDatabase.Refresh();
                Debug.Log("生成结束");
            }
        }
        /// <summary>
        /// 创建Panel类
        /// </summary>
        /// <param name="panelPrefabDir"></param>
        /// <param name="viewNs"></param>
        /// <param name="constNs"></param>
        /// <param name="rootNs"></param>
        /// <param name="autoDirName"></param>
        /// <returns></returns>
        private static bool CreatePanelFile(string panelPrefabDir, string viewNs, string constNs, string rootNs,
                                            string autoDirName)
        {
            if (!Directory.Exists(panelPrefabDir))
            {
                Debug.LogWarning("panelPrefabDir is not exist ! ");
                return(false);
            }

            foreach (var fullPath in Directory.GetFiles(panelPrefabDir))
            {
                if (fullPath.EndsWith(".meta"))
                {
                    continue;
                }
                if (!fullPath.EndsWith(".prefab"))
                {
                    continue;
                }

                var prefabName  = Path.GetFileNameWithoutExtension(fullPath);
                var fileName    = prefabName;
                var className   = prefabName;
                var safePartDir = Application.dataPath + "/" + rootNs + "/" + viewNs;
                var autoPartDir = safePartDir + "/" + autoDirName;
//                Debug.Log(safePartDir);
                FileUtil.CreateFolder(safePartDir);
                FileUtil.CreateFolder(autoPartDir);
                var safePartPath = safePartDir + "/" + fileName + ".cs";
                var autoPartPath = autoPartDir + "/" + fileName + ".cs";

                #region AutoPart

                if (File.Exists(autoPartPath))
                {
                    File.Delete(autoPartPath);
                }

                var stream = File.CreateText(autoPartPath);

                stream.Write("using ReadyGamerOne.View;\n" +
                             "using " + rootNs + "." + constNs + ";\n" +
                             "namespace " + rootNs + "." + viewNs + "\n" +
                             "{\n" +
                             "\tpublic partial class " + className + " : AbstractPanel\n" +
                             "\t{\n" +
                             "\t\tpartial void OnLoad();\n" +
                             "\n" +
                             "\t\tprotected override void Load()\n" +
                             "\t\t{\n" +
                             "\t\t\tCreate(PanelName." + className + ");\n" +
                             "\t\t\tOnLoad();\n" +
                             "\t\t}\n" +
                             "\t}\n" +
                             "}\n");


                stream.Flush();
                stream.Dispose();
                stream.Close();

                #endregion

                #region SafePart

                if (File.Exists(safePartPath))
                {
                    continue;
                }

                stream = new StreamWriter(safePartPath);

                stream.Write("namespace " + rootNs + "." + viewNs + "\n" +
                             "{\n" +
                             "\tpublic partial class " + fileName + "\n" +
                             "\t{\n" +
                             "\t\tpartial void OnLoad()\n" +
                             "\t\t{\n" +
                             "\t\t\t//do any thing you want\n" +
                             "\t\t}\n" +
                             "\t}\n" +
                             "}\n");

                stream.Dispose();
                stream.Close();

                #endregion
            }

            return(true);
        }
        /// <summary>
        /// 创建GameMgr
        /// </summary>
        /// <param name="rootNs"></param>
        /// <param name="constNs"></param>
        /// <param name="scriptNs"></param>
        /// <param name="autoDirName"></param>
        private static void CreateMgr(string rootNs, string constNs, string scriptNs, string autoDirName)
        {
            var safePartDir = Application.dataPath + "/" + rootNs + "/" + scriptNs;
            var autoPartDir = safePartDir + "/" + autoDirName;

            FileUtil.CreateFolder(safePartDir);
            FileUtil.CreateFolder(autoPartDir);
            var fileName = rootNs + "Mgr";

            var safePartPath = safePartDir + "/" + fileName + ".cs";
            var autoPartPath = autoPartDir + "/" + fileName + ".cs";

            #region AutoPart

            if (File.Exists(autoPartPath))
            {
                File.Delete(autoPartPath);
            }

            var stream = new StreamWriter(autoPartPath);

            stream.Write("using ReadyGamerOne.Script;\n");
            if (_resourceManagerType == ResourceManagerType.AssetBundle)
            {
                stream.Write("using " + rootNs + "." + constNs + ";\n");
            }

            stream.Write("using ReadyGamerOne.MemorySystem;\n");


            stream.Write("namespace " + rootNs + "." + scriptNs + "\n" +
                         "{\n" +
                         "\tpublic partial class " + fileName + " : AbstractGameMgr<" + fileName + ">\n" +
                         "\t{\n");

            switch (_resourceManagerType)
            {
            case ResourceManagerType.Resource:
                stream.Write("\t\tprotected override IResourceLoader ResourceLoader => ResourcesResourceLoader.Instance;\n" +
                             "\t\tprotected override IAssetConstUtil AssetConstUtil => Utility.AssetConstUtil.Instance;\n");
                break;

            case ResourceManagerType.AssetBundle:
                stream.Write("\t\tprotected override IResourceLoader ResourceLoader => AssetBundleResourceLoader.Instance;\n" +
                             "\t\tprotected override IHotUpdatePath PathData => HotUpdatePathData.Instance;\n" +
                             "\t\tprotected override IOriginAssetBundleUtil OriginBundleData => OriginBundleUtil.Instance;\n");
                break;
            }



            stream.Write("\t\tpartial void OnSafeAwake();\n");

            stream.Write("\t\tprotected override void Awake()\n" +
                         "\t\t{\n" +
                         "\t\t\tbase.Awake();\n" +
                         "\t\t\tOnSafeAwake();\n");

            stream.Write("\t\t}\n" +
                         "\t}\n" +
                         "}\n");

            stream.Dispose();
            stream.Close();

            #endregion

            #region SafePart

            if (File.Exists(safePartPath))
            {
                return;
            }

            stream = new StreamWriter(safePartPath);

            var usePanel = autoClassName.Contains("Panel");
            var useAudio = autoClassName.Contains("Audio");
            if (useAudio || usePanel)
            {
                stream.Write("using ReadyGamerOne.EditorExtension;\n" +
                             "using " + rootNs + "." + constNs + ";\n" +
                             "using ReadyGamerOne.Script;\n");
            }

            if (usePanel)
            {
                stream.Write("using ReadyGamerOne.View;\n");
            }

            stream.Write("namespace " + rootNs + "." + scriptNs + "\n" +
                         "{\n" +
                         "\tpublic partial class " + fileName + "\n" +
                         "\t{\n");
            if (usePanel)
            {
                stream.Write("\t\tpublic StringChooser startPanel = new StringChooser(typeof(PanelName));\n");
            }

            if (useAudio)
            {
                stream.Write("\t\tpublic StringChooser startBgm = new StringChooser(typeof(AudioName));\n");
            }

            stream.Write("\t\tpartial void OnSafeAwake()\n" +
                         "\t\t{\n");

            if (usePanel)
            {
                stream.Write("\t\t\tPanelMgr.PushPanel(startPanel.StringValue);\n");
            }
            if (useAudio)
            {
                stream.Write("\t\t\tAudioMgr.Instance.PlayBgm(startBgm.StringValue);\n");
            }

            stream.Write("\t\t\t//do any thing you want\n" +
                         "\t\t}\n" +
                         "\t}\n" +
                         "}\n");


            stream.Dispose();
            stream.Close();

            #endregion
        }
Esempio n. 7
0
        static void OnToolsGUI(string rootNs, string viewNs, string constNs, string dataNs, string autoDir, string scriptNs)
        {
            var resources = Application.dataPath + "/Resources";

            EditorGUILayout.Space();
            EditorGUILayout.Space();

            EditorGUILayout.LabelField("自动常量文件生成目录", Application.dataPath + "/" + rootNs + "/" + constNs + "/" + autoDir);
            EditorGUILayout.Space();


            createPanelClasses = EditorGUILayout.Toggle("是否自动生成Panel类型", createPanelClasses);
            if (createPanelClasses)
            {
                EditorGUILayout.LabelField("自动生成Panel文件路径", Application.dataPath + "/" + rootNs + "/" + viewNs);
            }
            EditorGUILayout.Space();


            createGameMgr = EditorGUILayout.Toggle("是否生成GameMgr", createGameMgr);
            if (createGameMgr)
            {
                EditorGUILayout.LabelField("自动生成" + rootNs + "Mgr文件路径", Application.dataPath + "/" + rootNs + "/" + scriptNs);
            }
            EditorGUILayout.Space();


            if (GUILayout.Button("开启自动生成", GUILayout.Height(2 * EditorGUIUtility.singleLineHeight)))
            {
                autoClassName.Clear();
                var rootDir = Application.dataPath + "/" + rootNs;
                FileUtil.CreateFolder(rootDir);
                FileUtil.CreateFolder(rootDir + "/" + constNs);
                FileUtil.CreateFolder(rootDir + "/" + constNs + "/" + autoDir);
                nameToPath.Clear();
                if (!Directory.Exists(resources))
                {
                    Directory.CreateDirectory(resources);
                    return;
                }
                FileUtil.SearchDirectory(resources,
                                         OprateFile, false,
                                         filPath => OprateDir(filPath, rootNs, constNs, autoDir)
                                         );

                if (nameToPath.Count > 0)
                {
                    FileUtil.CreateConstClassByDictionary("OtherResPath", rootDir + "/" + constNs + "/" + autoDir,
                                                          rootNs + "." + constNs, nameToPath);
                    FileUtil.CreateConstClassByDictionary("OtherResName", rootDir + "/" + constNs + "/" + autoDir,
                                                          rootNs + "." + constNs, nameToPath.Select(pair => pair.Key).ToDictionary(name => name));
                    autoClassName.Add("OtherRes");
                }


                if (autoClassName.Contains("Panel"))
                {
                    CreatePanelFile(Application.dataPath + "/Resources/ClassPanel", viewNs, constNs, rootNs, autoDir);
                }

                if (createGameMgr)
                {
                    CreateMgr(rootNs, constNs, scriptNs, autoDir);
                }

                AssetDatabase.Refresh();
                Debug.Log("生成结束");
            }

            EditorGUILayout.Space();

            if (autoClassName.Count > 0)
            {
                var str = "生成的类型有:";
                foreach (var name in autoClassName)
                {
                    str += "\n" + name + "Name" + "\t\t" + name + "Path";
                }
                EditorGUILayout.HelpBox(str, MessageType.Info);
            }
        }
Esempio n. 8
0
        /// <summary>
        /// 创建GameMgr
        /// </summary>
        /// <param name="rootNs"></param>
        /// <param name="constNs"></param>
        /// <param name="scriptNs"></param>
        /// <param name="autoDirName"></param>
        private static void CreateMgr(string rootNs, string constNs, string scriptNs, string autoDirName)
        {
            var safePartDir = Application.dataPath + "/" + rootNs + "/" + scriptNs;
            var autoPartDir = safePartDir + "/" + autoDirName;

            FileUtil.CreateFolder(safePartDir);
            FileUtil.CreateFolder(autoPartDir);
            var fileName = rootNs + "Mgr";

            var safePartPath = safePartDir + "/" + fileName + ".cs";
            var autoPartPath = autoPartDir + "/" + fileName + ".cs";

            #region AutoPart

            if (File.Exists(autoPartPath))
            {
                File.Delete(autoPartPath);
            }

            var stream = new StreamWriter(autoPartPath);

            stream.Write("using ReadyGamerOne.Script;\n");

            stream.Write("namespace " + rootNs + "." + scriptNs + "\n" +
                         "{\n" +
                         "\tpublic partial class " + fileName + " : AbstractGameMgr<" + fileName + ">\n" +
                         "\t{\n");


            stream.Write("\t\tpartial void OnSafeAwake();\n");

            stream.Write("\t\tprotected override void Awake()\n" +
                         "\t\t{\n" +
                         "\t\t\tbase.Awake();\n" +
                         "\t\t\tOnSafeAwake();\n");

            stream.Write("\t\t}\n" +
                         "\t}\n" +
                         "}\n");

            stream.Dispose();
            stream.Close();


            #endregion

            #region SafePart

            if (File.Exists(safePartPath))
            {
                return;
            }

            stream = new StreamWriter(safePartPath);

            var usePanel = autoClassName.Contains("Panel");
            var useAudio = autoClassName.Contains("Audio");
            if (useAudio || usePanel)
            {
                stream.Write("using ReadyGamerOne.EditorExtension;\n");
                stream.Write("using " + rootNs + "." + constNs + ";\n");
            }

            if (usePanel)
            {
                stream.Write("using ReadyGamerOne.View;\n" +
                             "using ReadyGamerOne.Script;\n");
            }

            stream.Write("namespace " + rootNs + "." + scriptNs + "\n" +
                         "{\n" +
                         "\tpublic partial class " + fileName + "\n" +
                         "\t{\n");
            if (usePanel)
            {
                stream.Write("\t\tpublic StringChooser startPanel = new StringChooser(typeof(PanelName));\n");
            }

            if (useAudio)
            {
                stream.Write("\t\tpublic StringChooser startBgm = new StringChooser(typeof(AudioPath));\n");
            }

            stream.Write("\t\tpartial void OnSafeAwake()\n" +
                         "\t\t{\n");

            if (usePanel)
            {
                stream.Write("\t\t\tPanelMgr.PushPanel(startPanel.StringValue);\n");
            }
            if (useAudio)
            {
                stream.Write("\t\t\tAudioMgr.Instance.PlayBgm(startBgm.StringValue);\n");
            }

            stream.Write("\t\t\t//do any thing you want\n" +
                         "\t\t}\n" +
                         "\t}\n" +
                         "}\n");


            stream.Dispose();
            stream.Close();

            #endregion
        }