274 lines
8.6 KiB
C#
274 lines
8.6 KiB
C#
using LitJson;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using System.Text;
|
||
using UnityEditor;
|
||
using UnityEngine;
|
||
using UnityEngine.SceneManagement;
|
||
using ZXKFrameworkEditor;
|
||
|
||
namespace FSM
|
||
{
|
||
public class GenerateStateClass : EditorWindow
|
||
{
|
||
string fsmConfigPath;
|
||
string fsmNameSpace;
|
||
string fsmControlBase;
|
||
string fsmClassPath;
|
||
string fsmFolder;
|
||
private int selectedIndex = 0;
|
||
readonly FsmConfigReader reader = new();
|
||
[MenuItem("FSM/GenerateStateClass")] //定义菜单栏位置
|
||
public static void OpenWindow() //打开窗口函数,必须是static
|
||
{
|
||
GetWindow<GenerateStateClass>(false, "生成Class", true).Show(); //实例化窗口
|
||
}
|
||
|
||
/// <summary>
|
||
/// 窗口内显示的GUI面板
|
||
/// </summary>
|
||
void OnGUI()
|
||
{
|
||
selectedIndex = EditorGUILayout.Popup("场景:", selectedIndex, getAllSceneName());
|
||
if (GUILayout.Button("生成Class"))
|
||
{
|
||
List<JsonData> data = JsonMapper.ToObject<List<JsonData>>(Resources.Load<TextAsset>("Main/ExcelData/ExcelToJson/MainData").text);
|
||
fsmClassPath = null;
|
||
fsmNameSpace = null;
|
||
fsmConfigPath = null;
|
||
fsmControlBase = null;
|
||
fsmFolder = null;
|
||
for (int i = 0; i < data.Count; i++)
|
||
{
|
||
if (getAllSceneName()[selectedIndex] == data[i]["scene"].ToString())
|
||
{
|
||
fsmClassPath = data[i]["fsmClassPath"].ToString();
|
||
fsmNameSpace = data[i]["nameSpace"].ToString();
|
||
fsmConfigPath = data[i]["folder"].ToString() + "/ExcelData/ExcelToJson/StateData";
|
||
fsmControlBase = data[i]["fsmManager"].ToString();
|
||
fsmFolder = data[i]["folder"].ToString();
|
||
}
|
||
}
|
||
Debug.Log(fsmClassPath + "=" + fsmNameSpace + "=" + fsmConfigPath + "=" + fsmControlBase);
|
||
if (string.IsNullOrEmpty(fsmConfigPath))
|
||
{
|
||
Debug.LogError("FSM配置文件不能为空");
|
||
return;
|
||
}
|
||
if (string.IsNullOrEmpty(fsmControlBase))
|
||
{
|
||
Debug.LogError("FSM管理类不能为空");
|
||
return;
|
||
}
|
||
if (string.IsNullOrEmpty(fsmClassPath))
|
||
{
|
||
Debug.LogError("FSM生成路径不能为空");
|
||
return;
|
||
}
|
||
if (string.IsNullOrEmpty(fsmNameSpace))
|
||
{
|
||
Debug.LogError("FSM命名空间不能为空");
|
||
return;
|
||
}
|
||
ExcelToJson json = new();
|
||
json.GetAllExcelPath(fsmFolder, fsmNameSpace);
|
||
GenerateClass(fsmConfigPath);
|
||
}
|
||
}
|
||
|
||
|
||
void GenerateClass(string fsmConfigPath)
|
||
{
|
||
string directoryPath = Application.dataPath + "/" + fsmClassPath + fsmControlBase;
|
||
if (!Directory.Exists(directoryPath))
|
||
{
|
||
EditorTools.CreateDirectory(directoryPath);
|
||
}
|
||
if (!Directory.Exists(directoryPath + "/State"))
|
||
{
|
||
EditorTools.CreateDirectory(directoryPath + "/State");
|
||
}
|
||
if (!Directory.Exists(directoryPath + "/Trigger"))
|
||
{
|
||
EditorTools.CreateDirectory(directoryPath + "/Trigger");
|
||
}
|
||
reader.EditorReadConfigFile(fsmConfigPath);
|
||
//reader.ReadConfigFile(fsmConfigPath, null);
|
||
string filePath = directoryPath + "/" + fsmControlBase + ".cs";
|
||
{
|
||
if (File.Exists(filePath))
|
||
{
|
||
}
|
||
else
|
||
{
|
||
FileStream fileStream = File.Create(filePath);
|
||
string content = "";
|
||
if (string.IsNullOrEmpty(fsmNameSpace) || fsmNameSpace.Trim() == "FSM")
|
||
{
|
||
content = @"using FSM;
|
||
public class " + fsmControlBase + @" : FsmBase<" + fsmControlBase + @">
|
||
{
|
||
public void Init()
|
||
{
|
||
|
||
}
|
||
protected override void Update()
|
||
{
|
||
base.Update();
|
||
}
|
||
}
|
||
";
|
||
}
|
||
else
|
||
{
|
||
content = @"using FSM;
|
||
namespace " + fsmNameSpace + @"
|
||
{
|
||
public class " + fsmControlBase + @" : FsmBase<" + fsmControlBase + @">
|
||
{
|
||
public void Init()
|
||
{
|
||
|
||
}
|
||
protected override void Update()
|
||
{
|
||
base.Update();
|
||
}
|
||
}
|
||
}
|
||
";
|
||
}
|
||
|
||
byte[] byteArray = Encoding.UTF8.GetBytes(content);
|
||
fileStream.Write(byteArray, 0, byteArray.Length);
|
||
fileStream.Close();
|
||
}
|
||
}
|
||
foreach (var mainKey in reader.map.Keys)
|
||
{
|
||
filePath = directoryPath + "/State/" + mainKey + ".cs";
|
||
if (File.Exists(filePath))
|
||
{
|
||
//Debug.LogError(mainKey + "State.cs 已存在");
|
||
continue;
|
||
}
|
||
FileStream fileStream = File.Create(filePath);
|
||
string content = "";
|
||
if (string.IsNullOrEmpty(fsmNameSpace) || fsmNameSpace.Trim() == "FSM")
|
||
{
|
||
content = @"namespace FSM
|
||
{
|
||
public class " + mainKey + @" : FsmState<" + fsmControlBase + @">
|
||
{
|
||
public override void OnStateEnter()
|
||
{
|
||
base.OnStateEnter();
|
||
}
|
||
public override void OnStateStay()
|
||
{
|
||
base.OnStateStay();
|
||
}
|
||
public override void OnStateExit()
|
||
{
|
||
base.OnStateExit();
|
||
}
|
||
}
|
||
}
|
||
";
|
||
}
|
||
else
|
||
{
|
||
content = @"using FSM;
|
||
namespace " + fsmNameSpace + @"
|
||
{
|
||
public class " + mainKey + @" : FsmState<" + fsmControlBase + @">
|
||
{
|
||
public override void OnStateEnter()
|
||
{
|
||
base.OnStateEnter();
|
||
}
|
||
public override void OnStateStay()
|
||
{
|
||
base.OnStateStay();
|
||
}
|
||
public override void OnStateExit()
|
||
{
|
||
base.OnStateExit();
|
||
}
|
||
}
|
||
}
|
||
";
|
||
}
|
||
|
||
byte[] byteArray = Encoding.UTF8.GetBytes(content);
|
||
fileStream.Write(byteArray, 0, byteArray.Length);
|
||
|
||
fileStream.Close();
|
||
}
|
||
foreach (var mainKey in reader.map.Keys)
|
||
{
|
||
foreach (var key in reader.map[mainKey].Keys)
|
||
{
|
||
filePath = directoryPath + "/Trigger/" + key + ".cs";
|
||
if (File.Exists(filePath))
|
||
{
|
||
//Debug.LogError(key + "Trigger.cs 已存在");
|
||
continue;
|
||
}
|
||
FileStream fileStream = File.Create(filePath);
|
||
string content = "";
|
||
if (string.IsNullOrEmpty(fsmNameSpace) || fsmNameSpace.Trim() == "FSM")
|
||
{
|
||
content = @"namespace FSM
|
||
{
|
||
public class " + key + @" : FsmTrigger<" + fsmControlBase + @">
|
||
{
|
||
public override bool OnTriggerHandler()
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
}
|
||
";
|
||
}
|
||
else
|
||
{
|
||
content = @"using FSM;
|
||
namespace " + fsmNameSpace + @"
|
||
{
|
||
public class " + key + @" : FsmTrigger<" + fsmControlBase + @">
|
||
{
|
||
public override bool OnTriggerHandler()
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
}
|
||
";
|
||
}
|
||
byte[] byteArray = Encoding.UTF8.GetBytes(content);
|
||
fileStream.Write(byteArray, 0, byteArray.Length);
|
||
fileStream.Close();
|
||
}
|
||
}
|
||
AssetDatabase.Refresh();
|
||
Debug.Log("类生成完毕");
|
||
}
|
||
|
||
/// <summary>获取所有场景名字</summary>
|
||
private string[] getAllSceneName()
|
||
{
|
||
List<JsonData> data = JsonMapper.ToObject<List<JsonData>>(Resources.Load<TextAsset>("Main/ExcelData/ExcelToJson/MainData").text);
|
||
List<string> temp = new();
|
||
for (int i = 0; i < data.Count; i++)
|
||
{
|
||
if (!string.IsNullOrEmpty(data[i]["scene"].ToString()) && data[i]["scene"].ToString() != "Main")
|
||
{
|
||
temp.Add(data[i]["scene"].ToString());
|
||
}
|
||
}
|
||
return temp.ToArray();
|
||
}
|
||
}
|
||
} |