浏览代码

Merge branch 'sm-improvements' of https://github.com/DeivSky/open-project-1 into pr-bash

/main
Ciro Continisio 4 年前
当前提交
13449978
共有 33 个文件被更改,包括 277 次插入33 次删除
  1. 1
      UOP1_Project/Assets/Scripts/StateMachine/Core/StateAction.cs
  2. 29
      UOP1_Project/Assets/Scripts/StateMachine/Core/StateCondition.cs
  3. 6
      UOP1_Project/Assets/Scripts/StateMachine/Core/StateMachine.cs
  4. 16
      UOP1_Project/Assets/Scripts/StateMachine/ScriptableObjects/StateConditionSO.cs
  5. 14
      UOP1_Project/Assets/Scripts/StateMachine/Debugging/StateMachineDebugger.cs
  6. 8
      UOP1_Project/Assets/Scripts/StateMachine/Debugging.meta
  7. 8
      UOP1_Project/Assets/Scripts/StateMachine/Editor/Templates.meta
  8. 8
      UOP1_Project/Assets/Scripts/StateMachine/Editor/Utilities.meta
  9. 8
      UOP1_Project/Assets/Scripts/StateMachine/Utilities.meta
  10. 11
      UOP1_Project/Assets/Scripts/StateMachine/Editor/Templates/ScriptTemplates.cs.meta
  11. 28
      UOP1_Project/Assets/Scripts/StateMachine/Editor/Templates/StateAction.txt
  12. 7
      UOP1_Project/Assets/Scripts/StateMachine/Editor/Templates/StateAction.txt.meta
  13. 29
      UOP1_Project/Assets/Scripts/StateMachine/Editor/Templates/StateCondition.txt
  14. 7
      UOP1_Project/Assets/Scripts/StateMachine/Editor/Templates/StateCondition.txt.meta
  15. 62
      UOP1_Project/Assets/Scripts/StateMachine/Editor/Templates/ScriptTemplates.cs
  16. 38
      UOP1_Project/Assets/Scripts/StateMachine/Editor/Utilities/InitOnlyAttributeDrawer.cs
  17. 11
      UOP1_Project/Assets/Scripts/StateMachine/Editor/Utilities/InitOnlyAttributeDrawer.cs.meta
  18. 8
      UOP1_Project/Assets/Scripts/StateMachine/Utilities/InitOnlyAttribute.cs
  19. 11
      UOP1_Project/Assets/Scripts/StateMachine/Utilities/InitOnlyAttribute.cs.meta
  20. 0
      /UOP1_Project/Assets/Scripts/StateMachine/Debugging/StateMachineDebugger.cs
  21. 0
      /UOP1_Project/Assets/Scripts/StateMachine/Debugging/StateMachineDebugger.cs.meta
  22. 0
      /UOP1_Project/Assets/Scripts/StateMachine/Editor/Utilities/AddTransitionHelper.cs
  23. 0
      /UOP1_Project/Assets/Scripts/StateMachine/Editor/Utilities/AddTransitionHelper.cs.meta
  24. 0
      /UOP1_Project/Assets/Scripts/StateMachine/Editor/Utilities/ContentStyle.cs
  25. 0
      /UOP1_Project/Assets/Scripts/StateMachine/Editor/Utilities/ContentStyle.cs.meta
  26. 0
      /UOP1_Project/Assets/Scripts/StateMachine/Editor/Utilities/SerializedTransition.cs
  27. 0
      /UOP1_Project/Assets/Scripts/StateMachine/Editor/Utilities/SerializedTransition.cs.meta
  28. 0
      /UOP1_Project/Assets/Scripts/StateMachine/Editor/Utilities/TransitionDisplayHelper.cs.meta
  29. 0
      /UOP1_Project/Assets/Scripts/StateMachine/Editor/Utilities/TransitionDisplayHelper.cs

1
UOP1_Project/Assets/Scripts/StateMachine/Core/StateAction.cs


public abstract class StateAction : IStateComponent
{
internal StateActionSO _originSO;
protected StateActionSO OriginSO => _originSO;
/// <summary>
/// Called every frame the <see cref="StateMachine"/> is in a <see cref="State"/> with this <see cref="StateAction"/>.

29
UOP1_Project/Assets/Scripts/StateMachine/Core/StateCondition.cs


using UnityEngine;
using UOP1.StateMachine.ScriptableObjects;
using UOP1.StateMachine.ScriptableObjects;
namespace UOP1.StateMachine
{

private bool _isCached = false;
private bool _cachedStatement = default;
internal StateConditionSO _originSO;
protected StateConditionSO OriginSO => _originSO;
/// <summary>
/// Specify the statement to evaluate.

/// </summary>
internal bool GetStatement()
{
bool returnValue;
if (!_originSO.cacheResult)
return Statement();
if (_originSO.cacheResult && _isCached)
returnValue = _cachedStatement;
else
if (!_isCached)
returnValue = Statement();
if (_originSO.cacheResult)
{
_isCached = true;
_cachedStatement = returnValue;
}
_isCached = true;
_cachedStatement = Statement();
return returnValue;
return _cachedStatement;
}
internal void ClearStatementCache()

/// </summary>
public readonly struct StateCondition
{
internal readonly StateConditionSO _originSO;
public StateCondition(StateConditionSO originSO, StateMachine stateMachine, Condition condition, bool expectedResult)
public StateCondition(StateMachine stateMachine, Condition condition, bool expectedResult)
_originSO = originSO;
_condition._originSO = originSO;
_expectedResult = expectedResult;
}

bool isMet = statement == _expectedResult;
#if UNITY_EDITOR
_stateMachine._debugger.TransitionConditionResult(_originSO.name, statement, isMet);
_stateMachine._debugger.TransitionConditionResult(_condition._originSO.name, statement, isMet);
#endif
return isMet;
}

6
UOP1_Project/Assets/Scripts/StateMachine/Core/StateMachine.cs


#if UNITY_EDITOR
[Space]
[SerializeField]
internal StateMachineDebugger _debugger = default;
internal Debugging.StateMachineDebugger _debugger = default;
private State _currentState;
internal State _currentState;
private void Awake()
{

_debugger.Awake(this, _currentState._originSO.name);
_debugger.Awake(this);
#endif
}

16
UOP1_Project/Assets/Scripts/StateMachine/ScriptableObjects/StateConditionSO.cs


/// </summary>
internal StateCondition GetCondition(StateMachine stateMachine, bool expectedResult, Dictionary<ScriptableObject, object> createdInstances)
{
if (createdInstances.TryGetValue(this, out var cond))
return new StateCondition(this, stateMachine, (Condition)cond, expectedResult);
if (!createdInstances.TryGetValue(this, out var obj))
{
var condition = CreateCondition();
condition._originSO = this;
createdInstances.Add(this, condition);
condition.Awake(stateMachine);
var condition = new StateCondition(this, stateMachine, CreateCondition(), expectedResult);
createdInstances.Add(this, condition._condition);
condition._condition.Awake(stateMachine);
return condition;
obj = condition;
}
return new StateCondition(stateMachine, (Condition)obj, expectedResult);
}
protected abstract Condition CreateCondition();
}

14
UOP1_Project/Assets/Scripts/StateMachine/Debugging/StateMachineDebugger.cs


using System;
#if UNITY_EDITOR
using System;
namespace UOP1.StateMachine
namespace UOP1.StateMachine.Debugging
{
/// <summary>
/// Class specialized in debugging the state transitions, should only be used while in editor mode.

/// <summary>
/// Must be called together with <c>StateMachine.Awake()</c>
/// </summary>
internal void Awake(StateMachine stateMachine, string initialState)
internal void Awake(StateMachine stateMachine)
currentState = initialState;
currentState = stateMachine._currentState._originSO.name;
}
internal void TransitionEvaluationBegin(string targetState)

return;
_logBuilder.Clear();
_logBuilder.AppendLine($"{_stateMachine.gameObject.name} state changed");
_logBuilder.AppendLine($"{_stateMachine.name} state changed");
_logBuilder.AppendLine($"{currentState} {SHARP_ARROW} {_targetState}");
if (appendConditionsInfo)

}
}
}
#endif

8
UOP1_Project/Assets/Scripts/StateMachine/Debugging.meta


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

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


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

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


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

8
UOP1_Project/Assets/Scripts/StateMachine/Utilities.meta


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

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:

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)));
}
}
}

38
UOP1_Project/Assets/Scripts/StateMachine/Editor/Utilities/InitOnlyAttributeDrawer.cs


using UnityEditor;
using UnityEngine;
namespace UOP1.StateMachine.Editor
{
[CustomPropertyDrawer(typeof(InitOnlyAttribute))]
public class InitOnlyAttributeDrawer : PropertyDrawer
{
private static readonly string _text = "Changes to this parameter during play mode will only take effect on new state machine instances, or the next time you enter play mode.";
private static readonly GUIStyle _style = new GUIStyle(GUI.skin.GetStyle("helpbox")) { padding = new RectOffset(5, 5, 5, 5) };
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
if (EditorApplication.isPlaying)
{
position.height = _style.CalcHeight(new GUIContent(_text), EditorGUIUtility.currentViewWidth);
EditorGUI.HelpBox(position, _text, MessageType.Info);
position.y += position.height + EditorGUIUtility.standardVerticalSpacing;
position.height = EditorGUI.GetPropertyHeight(property, label);
}
EditorGUI.PropertyField(position, property, label);
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
float height = EditorGUI.GetPropertyHeight(property, label);
if (EditorApplication.isPlaying)
{
height += _style.CalcHeight(new GUIContent(_text), EditorGUIUtility.currentViewWidth)
+ EditorGUIUtility.standardVerticalSpacing * 4;
}
return height;
}
}
}

11
UOP1_Project/Assets/Scripts/StateMachine/Editor/Utilities/InitOnlyAttributeDrawer.cs.meta


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

8
UOP1_Project/Assets/Scripts/StateMachine/Utilities/InitOnlyAttribute.cs


using System;
using UnityEngine;
namespace UOP1.StateMachine
{
[AttributeUsage(AttributeTargets.Field)]
public class InitOnlyAttribute : PropertyAttribute { }
}

11
UOP1_Project/Assets/Scripts/StateMachine/Utilities/InitOnlyAttribute.cs.meta


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

/UOP1_Project/Assets/Scripts/StateMachine/Core/StateMachineDebugger.cs → /UOP1_Project/Assets/Scripts/StateMachine/Debugging/StateMachineDebugger.cs

/UOP1_Project/Assets/Scripts/StateMachine/Core/StateMachineDebugger.cs.meta → /UOP1_Project/Assets/Scripts/StateMachine/Debugging/StateMachineDebugger.cs.meta

/UOP1_Project/Assets/Scripts/StateMachine/Editor/AddTransitionHelper.cs → /UOP1_Project/Assets/Scripts/StateMachine/Editor/Utilities/AddTransitionHelper.cs

/UOP1_Project/Assets/Scripts/StateMachine/Editor/AddTransitionHelper.cs.meta → /UOP1_Project/Assets/Scripts/StateMachine/Editor/Utilities/AddTransitionHelper.cs.meta

/UOP1_Project/Assets/Scripts/StateMachine/Editor/ContentStyle.cs → /UOP1_Project/Assets/Scripts/StateMachine/Editor/Utilities/ContentStyle.cs

/UOP1_Project/Assets/Scripts/StateMachine/Editor/ContentStyle.cs.meta → /UOP1_Project/Assets/Scripts/StateMachine/Editor/Utilities/ContentStyle.cs.meta

/UOP1_Project/Assets/Scripts/StateMachine/Editor/SerializedTransition.cs → /UOP1_Project/Assets/Scripts/StateMachine/Editor/Utilities/SerializedTransition.cs

/UOP1_Project/Assets/Scripts/StateMachine/Editor/SerializedTransition.cs.meta → /UOP1_Project/Assets/Scripts/StateMachine/Editor/Utilities/SerializedTransition.cs.meta

/UOP1_Project/Assets/Scripts/StateMachine/Editor/TransitionDisplayHelper.cs.meta → /UOP1_Project/Assets/Scripts/StateMachine/Editor/Utilities/TransitionDisplayHelper.cs.meta

/UOP1_Project/Assets/Scripts/StateMachine/Editor/TransitionDisplayHelper.cs → /UOP1_Project/Assets/Scripts/StateMachine/Editor/Utilities/TransitionDisplayHelper.cs

正在加载...
取消
保存