自己写了一套流程控制的框架,根据不同的状态执行不同的命令,每个状态判断和命令都是一个场景中的物体
不想每次重复同样的操作,创建脚本再创建预制体再绑定脚本,所以尝试写了一个自动创建脚本与预制体的工具
StateMachineEditorUtil用来创建脚本
public class StateMachineEditorUtil
{
public static string dirPath=Application.dataPath+"/Game/Scripts/StateMachine";
public static string logicPath=dirPath +"/ObjectLogic";
public static string templatePath=logicPath+"/ObjectLogicTemplate.txt";
public static string triggerPath = dirPath + "/Trigger";
public static string actionPath = dirPath + "/Action";
[MenuItem("GameObject/状态机/添加状态机", priority = 0)]
public static void CreatScript()
{
var select = Selection.activeGameObject;
var name = select.name;
name="StateMachine_"+name.Replace(" ", "");
var createPath=string.Format("{0}/{1}.cs", logicPath, name);
using (StreamWriter writer = new StreamWriter(createPath))
{
writer.WriteLine(GetScriptTemplateSetClassName(templatePath, name));
}
AssetDatabase.Refresh();
var go = new GameObject {name = @select.name};
go.transform.parent = select.transform.parent;
select.transform.SetParent(go.transform);
var trigger = new GameObject {name = "Triggers"};
trigger.transform.SetParent(go.transform);
}
public static void CreateTrigger(string name)
{
var createPath=string.Format("{0}/{1}.cs", triggerPath, name);
using (StreamWriter writer = new StreamWriter(createPath))
{
writer.WriteLine(GetScriptTemplateSetClassName(triggerPath+"/TriggerTemplate.txt", name));
}
AssetDatabase.Refresh();
}
public static void CreateAction(string name)
{
var createPath=string.Format("{0}/{1}.cs", actionPath, name);
using (StreamWriter writer = new StreamWriter(createPath))
{
writer.WriteLine(GetScriptTemplateSetClassName(actionPath+"/ActionTemplate.txt", name));
}
AssetDatabase.Refresh();
}
private static string GetScriptTemplateSetClassName(string FilePath, string DesiredClassName)
{
return File.ReadAllText(FilePath).Replace("#SCRIPTNAME#", DesiredClassName).TrimEnd();
}
public void Expand(GameObject go)
{
SceneHierarchyUtility.SetExpanded(go,true);
}
}
脚本模板,如xxxAction
StateTool.cs
public class StateTool : MonoBehaviour
{
#if UNITY_EDITOR
private string pfbPath;
[LabelText("新建state的名字"),TextArea]
public string stateName;
[Button("新建一个Trigger预制体与脚本")]
void CreateTrigger()
{
if (stateName.IsNullOrWhitespace())
{
Debug.Log("请输入类名");
return;
}
if (!stateName.Contains("Trigger"))
{
Debug.Log("名称不规范!请以Trigger为后缀");
return;
}
StateMachineEditorUtil.CreateTrigger(stateName);
var go = new GameObject {name = stateName};
UnityEditor.PrefabUtility.SaveAsPrefabAsset(go, "Assets/Game/Prefabs/Game/StateMachine/Trigger/"+stateName+".prefab");
DestroyImmediate(go);
AssetDatabase.Refresh();
}
[Button("新建一个Action预制体与脚本")]
void CreateAction()
{
if (stateName.IsNullOrWhitespace())
{
Debug.Log("请输入类名");
return;
}
if (!stateName.Contains("Action"))
{
Debug.Log("名称不规范!请以Action为后缀");
return;
}
StateMachineEditorUtil.CreateAction(stateName);
var go = new GameObject {name = stateName};
UnityEditor.PrefabUtility.SaveAsPrefabAsset(go, "Assets/Game/Prefabs/Game/StateMachine/Action/"+stateName+".prefab");
DestroyImmediate(go);
AssetDatabase.Refresh();
}
void SetPfbPath()
{
pfbPath = "";
if (stateName.Contains("Action"))
{
pfbPath = "Assets/Game/Prefabs/Game/StateMachine/Action/" + stateName + ".prefab";
}else if (stateName.Contains("Trigger"))
{
pfbPath = "Assets/Game/Prefabs/Game/StateMachine/Trigger/" + stateName + ".prefab";
}
}
string GetNameSpace()
{
if (stateName.Contains("Action"))
{
return "Game.Scripts.StateMachine.Action.";
}
if (stateName.Contains("Trigger"))
{
return "Game.Scripts.StateMachine.Trigger.";
}
return "";
}
[Button("绑定脚本")]
private void BindScript()
{
SetPfbPath();
if (!pfbPath.IsNullOrWhitespace())
{
if (!EditorApplication.isCompiling)
{
var go=PrefabUtility.InstantiatePrefab( AssetDatabase.LoadAssetAtPath<GameObject>(pfbPath)) as GameObject;
var type = Type.GetType(GetNameSpace()+stateName);
if (!go.TryGetComponent(type,out Component component))
{
go.AddComponent(type);
PrefabUtility.ApplyAddedComponent(go.GetComponent(type),pfbPath,InteractionMode.UserAction);
AssetDatabase.Refresh();
}
DestroyImmediate(go);
}
}
}
private void OnValidate()
{
if (!stateName.IsNullOrWhitespace())
{
var s = stateName[0];
var S=s.ToString().ToUpper();
stateName = S + stateName.Substring(1);
}
}
#endif
}
使用的话注意修改对应路径,然后挂到物体上,然后先新建,等待脚本编译完成,再绑定脚本
|