CourseEditor.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEditor;
  5. using UnityEngine;
  6. using WS;
  7. public class CourseEditor : EditorWindow
  8. {
  9. [MenuItem("Tools/课程编辑器")]
  10. public static void ShowWindow()
  11. {
  12. CourseEditor window = EditorWindow.GetWindow<CourseEditor>();
  13. window.Show();
  14. }
  15. ///<summary>课程</summary>
  16. private List<CourseData> Config;
  17. ///<summary>文件路径</summary>
  18. private string path;
  19. ///<summary>课程名称引用</summary>
  20. private string CourseName;
  21. private List<string> textAll = new List<string>() { };
  22. private void OnEnable()
  23. {
  24. path = $"{Application.dataPath}/Resources/Config/Framework/Course.txt";
  25. if (ConfigHelper.ConfigExists(path))
  26. {
  27. Config = ConfigHelper.FileLoadConfig<List<CourseData>>(path);
  28. }
  29. }
  30. private void OnGUI()
  31. {
  32. EditorGUILayout.BeginHorizontal();
  33. CourseName = EditorGUILayout.TextField(CourseName);
  34. if (GUILayout.Button("添加", GUILayout.Width(100)))
  35. {
  36. if (string.IsNullOrEmpty(CourseName))
  37. {
  38. Debug.LogError("名称不能为空!");
  39. }
  40. else
  41. {
  42. CourseData data = Config.Find(c => c.Name == CourseName);
  43. if (data != null)
  44. {
  45. Debug.LogError($"课程 {CourseName} 已存在!");
  46. }
  47. else
  48. {
  49. Config.Add(new CourseData { Name = CourseName });
  50. CourseName = null;
  51. }
  52. }
  53. }
  54. EditorGUILayout.EndHorizontal();
  55. for (int i = 0; i < Config.Count; i++)
  56. {
  57. EditorGUILayout.BeginHorizontal();
  58. Config[i].Name = EditorGUILayout.TextField(Config[i].Name);
  59. if (GUILayout.Button("删除", GUILayout.Width(100)))
  60. {
  61. Config.RemoveAt(i);
  62. }
  63. if (GUILayout.Button("编辑", GUILayout.Width(100)))
  64. {
  65. ProcessEditor window = EditorWindow.GetWindow<ProcessEditor>();
  66. window.Init(Config[i].Name);
  67. window.Show();
  68. //Close();
  69. }
  70. EditorGUILayout.EndHorizontal();
  71. }
  72. EditorGUILayout.BeginHorizontal();
  73. GUILayout.Label("");
  74. if (GUILayout.Button("保存", GUILayout.Width(100)))
  75. {
  76. ConfigHelper.SaveConfig(Config, path);
  77. }
  78. EditorGUILayout.EndHorizontal();
  79. EditorGUILayout.BeginHorizontal();
  80. if (GUILayout.Button("合并", GUILayout.Width(100)))
  81. {
  82. string text = null;
  83. List<Module> modules = new List<Module>();
  84. modules.Add(new Module());
  85. modules[0].Id = 1;
  86. modules[0].Name = "AllProcess";
  87. modules[0].NoOrder = false;
  88. modules[0].NoOrder = false;
  89. for (int i = 0; i < 6; i++)
  90. {
  91. text = Resources.Load<TextAsset>("Config/Process/Process" + (i + 1)).text;
  92. var m = LitJson.JsonMapper.ToObject<List<Module>>(text);
  93. modules[0].WSTasks.AddRange(m[0].WSTasks);
  94. }
  95. for (int i = 0; i < modules[0].WSTasks.Count; i++)
  96. {
  97. modules[0].WSTasks[i].Id = i + 1;
  98. }
  99. byte[] bytes = System.Text.Encoding.UTF8.GetBytes(LitJson.JsonMapper.ToJson(modules)); ;
  100. FileStream fs = new FileStream(Application.dataPath + "/Resources/Config/Process/AllProcess.txt", FileMode.Create);
  101. fs.Write(bytes, 0, bytes.Length);
  102. fs.Close();
  103. Debug.Log("合并成功");
  104. }
  105. EditorGUILayout.EndHorizontal();
  106. }
  107. }