浏览代码

Added templates for StateAction and StateCondition scripts

Added menu items for creating new StateAction and StateCondition scripts, to reduce some of the boilerplate they require.
To create a new script, go to Assets (or right click on project view) > Create > State Machines > Action/Condition Script.
/main
DeivSky 4 年前
当前提交
c6c566d3
共有 8 个文件被更改,包括 152 次插入0 次删除
  1. 8
      UOP1_Project/Assets/Scripts/StateMachine/Editor/Templates.meta
  2. 62
      UOP1_Project/Assets/Scripts/StateMachine/Editor/Templates/ScriptTemplates.cs
  3. 11
      UOP1_Project/Assets/Scripts/StateMachine/Editor/Templates/ScriptTemplates.cs.meta
  4. 28
      UOP1_Project/Assets/Scripts/StateMachine/Editor/Templates/StateAction.txt
  5. 7
      UOP1_Project/Assets/Scripts/StateMachine/Editor/Templates/StateAction.txt.meta
  6. 29
      UOP1_Project/Assets/Scripts/StateMachine/Editor/Templates/StateCondition.txt
  7. 7
      UOP1_Project/Assets/Scripts/StateMachine/Editor/Templates/StateCondition.txt.meta

8
UOP1_Project/Assets/Scripts/StateMachine/Editor/Templates.meta


fileFormatVersion: 2
guid: 5230aeaf5ff8c204baca93f3d4433c14
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

62
UOP1_Project/Assets/Scripts/StateMachine/Editor/Templates/ScriptTemplates.cs


using System.IO;
using System.Text;
using UnityEngine;
using UnityEditor;
using UnityEditor.ProjectWindowCallback;
internal class ScriptTemplates
{
private static readonly string _path = "Assets/Scripts/StateMachine/Editor/Templates";
[MenuItem("Assets/Create/State Machines/Action Script", priority = 20)]
public static void CreateActionScript() =>
ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0,
ScriptableObject.CreateInstance<DoCreateStateMachineScriptAsset>(),
"NewActionSO.cs",
(Texture2D)EditorGUIUtility.IconContent("cs Script Icon").image,
$"{_path}/StateAction.txt");
[MenuItem("Assets/Create/State Machines/Condition Script", priority = 20)]
public static void CreateConditionScript() =>
ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0,
ScriptableObject.CreateInstance<DoCreateStateMachineScriptAsset>(),
"NewConditionSO.cs",
(Texture2D)EditorGUIUtility.IconContent("cs Script Icon").image,
$"{_path}/StateCondition.txt");
private class DoCreateStateMachineScriptAsset : EndNameEditAction
{
public override void Action(int instanceId, string pathName, string resourceFile)
{
string text = File.ReadAllText(resourceFile);
string fileName = Path.GetFileName(pathName);
{
string newName = fileName.Replace(" ", "");
if (!newName.Contains("SO"))
newName = newName.Insert(fileName.Length - 3, "SO");
pathName = pathName.Replace(fileName, newName);
fileName = newName;
}
string fileNameWithoutExtension = fileName.Substring(0, fileName.Length - 3);
text = text.Replace("#SCRIPTNAME#", fileNameWithoutExtension);
string runtimeName = fileNameWithoutExtension.Replace("SO", "");
text = text.Replace("#RUNTIMENAME#", runtimeName);
for (int i = runtimeName.Length - 1; i > 0; i--)
if (char.IsUpper(runtimeName[i]) && char.IsLower(runtimeName[i - 1]))
runtimeName = runtimeName.Insert(i, " ");
text = text.Replace("#RUNTIMENAME_WITH_SPACES#", runtimeName);
string fullPath = Path.GetFullPath(pathName);
var encoding = new UTF8Encoding(true);
File.WriteAllText(fullPath, text, encoding);
AssetDatabase.ImportAsset(pathName);
ProjectWindowUtil.ShowCreatedAsset(AssetDatabase.LoadAssetAtPath(pathName, typeof(UnityEngine.Object)));
}
}
}

11
UOP1_Project/Assets/Scripts/StateMachine/Editor/Templates/ScriptTemplates.cs.meta


fileFormatVersion: 2
guid: 9fd3c8720e796cd4e804095392ce509e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

28
UOP1_Project/Assets/Scripts/StateMachine/Editor/Templates/StateAction.txt


using UnityEngine;
using UOP1.StateMachine;
using UOP1.StateMachine.ScriptableObjects;
[CreateAssetMenu(fileName = "#RUNTIMENAME#", menuName = "State Machines/Actions/#RUNTIMENAME_WITH_SPACES#")]
public class #SCRIPTNAME# : StateActionSO
{
protected override StateAction CreateAction() => new #RUNTIMENAME#();
}
public class #RUNTIMENAME# : StateAction
{
public override void Awake(StateMachine stateMachine)
{
}
public override void OnUpdate()
{
}
// public override void OnStateEnter()
// {
// }
// public override void OnStateExit()
// {
// }
}

7
UOP1_Project/Assets/Scripts/StateMachine/Editor/Templates/StateAction.txt.meta


fileFormatVersion: 2
guid: 615301afafc99e24590709430b12dfd5
TextScriptImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

29
UOP1_Project/Assets/Scripts/StateMachine/Editor/Templates/StateCondition.txt


using UnityEngine;
using UOP1.StateMachine;
using UOP1.StateMachine.ScriptableObjects;
[CreateAssetMenu(fileName = "#RUNTIMENAME#", menuName = "State Machines/Conditions/#RUNTIMENAME_WITH_SPACES#")]
public class #SCRIPTNAME# : StateConditionSO
{
protected override Condition CreateCondition() => new #RUNTIMENAME#();
}
public class #RUNTIMENAME# : Condition
{
public override void Awake(StateMachine stateMachine)
{
}
protected override bool Statement()
{
return true;
}
// public override void OnStateEnter()
// {
// }
// public override void OnStateExit()
// {
// }
}

7
UOP1_Project/Assets/Scripts/StateMachine/Editor/Templates/StateCondition.txt.meta


fileFormatVersion: 2
guid: 0db232a5ab5b5694f95fed9846d48f81
TextScriptImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存