You've already forked taptap2024_GJ_chidouren
init
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
|
||||
namespace XFFSM
|
||||
{
|
||||
|
||||
public class FSMConditionFactory
|
||||
{
|
||||
|
||||
public static FSMConditionData CreateCondition(RuntimeFSMController controller) {
|
||||
FSMConditionData condition = new FSMConditionData();
|
||||
|
||||
string parameterName = string.Empty;
|
||||
|
||||
FSMParameterData parameter = null;
|
||||
|
||||
if (controller.parameters.Count > 0)
|
||||
{
|
||||
parameter = controller.parameters[0];
|
||||
parameterName = parameter.name;
|
||||
}
|
||||
|
||||
if (parameter != null)
|
||||
{
|
||||
switch (parameter.parameterType)
|
||||
{
|
||||
case ParameterType.Float:
|
||||
condition.compareType = CompareType.Greater;
|
||||
break;
|
||||
case ParameterType.Int:
|
||||
condition.compareType = CompareType.Greater;
|
||||
break;
|
||||
case ParameterType.Bool:
|
||||
condition.compareType = CompareType.Equal;
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
condition.compareType = CompareType.Greater;
|
||||
}
|
||||
|
||||
|
||||
condition.targetValue = 0;
|
||||
condition.parameterName = parameterName;
|
||||
return condition;
|
||||
}
|
||||
|
||||
// 创建条件
|
||||
public static void CreateCondition(RuntimeFSMController controller,FSMTransitionData transition) {
|
||||
FSMConditionData condition = CreateCondition(controller);
|
||||
transition.conditions.Add(condition);
|
||||
UnityEditor.EditorUtility.SetDirty(controller);
|
||||
UnityEditor.AssetDatabase.SaveAssets();
|
||||
}
|
||||
|
||||
public static void DeleteCondition(RuntimeFSMController controller, FSMTransitionData transition,int index) {
|
||||
if ( index < 0 || index >= transition.conditions.Count ) {
|
||||
return;
|
||||
}
|
||||
|
||||
transition.conditions.RemoveAt(index);
|
||||
|
||||
|
||||
UnityEditor.EditorUtility.SetDirty(controller);
|
||||
UnityEditor.AssetDatabase.SaveAssets();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f55b27a4a6f3fcd428fbb8944fa14f2a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,149 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using UnityEngine;
|
||||
|
||||
namespace XFFSM
|
||||
{
|
||||
public class FSMParamterFactory
|
||||
{
|
||||
|
||||
// 创建参数
|
||||
public static void CreateParamter(RuntimeFSMController controller,ParameterType type) {
|
||||
|
||||
FSMParameterData parameter = new FSMParameterData();
|
||||
parameter.name = GetDefualtName(controller, type);
|
||||
parameter.parameterType = type;
|
||||
parameter.value = 0;
|
||||
|
||||
controller.AddParameters(parameter);
|
||||
}
|
||||
|
||||
private static string GetDefualtName(RuntimeFSMController controller,ParameterType type) {
|
||||
string name = string.Format("New {0}", type.ToString());
|
||||
string tempName = name;
|
||||
|
||||
int i = 1;
|
||||
while (controller.GetParameterData(tempName) != null)
|
||||
{
|
||||
tempName = string.Format("{0}{1}", name, i);
|
||||
i++;
|
||||
}
|
||||
|
||||
return tempName;
|
||||
}
|
||||
|
||||
// 删除参数
|
||||
public static void RemoveParamter(RuntimeFSMController controller,int index) {
|
||||
|
||||
if (Application.isPlaying) return;
|
||||
|
||||
if (controller == null) return;
|
||||
|
||||
FSMParameterData parameter = controller.parameters[index];
|
||||
|
||||
List<FSMTransitionData> transitions = new List<FSMTransitionData>();
|
||||
|
||||
// 查询引用了这个参数的过渡
|
||||
foreach (var item in controller.transitions)
|
||||
{
|
||||
foreach (var condition in item.conditions)
|
||||
{
|
||||
if (condition.parameterName!=null && condition.parameterName.Equals(parameter.name)) {
|
||||
transitions.Add(item);
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if (transitions.Count == 0)
|
||||
{
|
||||
controller.RemoveParameters(parameter);
|
||||
}
|
||||
else {
|
||||
|
||||
StringBuilder content = new StringBuilder();
|
||||
content.Append("确定删除参数:").Append(parameter.name).Append("吗?").Append("\n");
|
||||
content.Append("有以下过渡引用此参数!\n");
|
||||
|
||||
foreach (var item in transitions)
|
||||
{
|
||||
content.Append(item.fromStateName).Append(" -> ").Append(item.toStateName);
|
||||
}
|
||||
|
||||
if (UnityEditor.EditorUtility.DisplayDialog("删除参数",content.ToString(),"确定","取消")) {
|
||||
controller.RemoveParameters(parameter);
|
||||
|
||||
foreach (var item in transitions)
|
||||
{
|
||||
for (int i = item.conditions.Count - 1; i >=0; i--)
|
||||
{
|
||||
FSMConditionData condition = item.conditions[i];
|
||||
if (condition.parameterName != null && condition.parameterName.Equals(parameter.name))
|
||||
{
|
||||
item.conditions.RemoveAt(i);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
// 重命名参数
|
||||
public static void RenameParamter(RuntimeFSMController controller,FSMParameterData parameter,string newName) {
|
||||
|
||||
// 判断名称是否为空
|
||||
if (string.IsNullOrEmpty(newName))
|
||||
{
|
||||
FSMEditorWindow.ShowNotification("参数名称不能为空!");
|
||||
Debug.LogError("参数名称不能为空!");
|
||||
return;
|
||||
}
|
||||
// 判断新的名称是否已经存在
|
||||
if (controller.GetParameterData(newName) != null) {
|
||||
FSMEditorWindow.ShowNotification("参数名称已经存在!");
|
||||
Debug.LogError("参数名称已经存在!");
|
||||
return;
|
||||
}
|
||||
|
||||
// 查找到所有引用此参数的过渡 修改名称
|
||||
foreach (var item in controller.transitions)
|
||||
{
|
||||
// 遍历所有的条件
|
||||
foreach (var condition in item.conditions)
|
||||
{
|
||||
if (condition.parameterName!=null && condition.parameterName.Equals(parameter.name)) {
|
||||
condition.parameterName = newName;
|
||||
}
|
||||
}
|
||||
|
||||
// 遍历其他组的条件
|
||||
foreach (var group in item.group_conditions)
|
||||
{
|
||||
foreach (var condition in group.conditions)
|
||||
{
|
||||
if (condition.parameterName != null && condition.parameterName.Equals(parameter.name))
|
||||
{
|
||||
condition.parameterName = newName;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 修改参数名称
|
||||
parameter.name = newName;
|
||||
controller.ClearCache();
|
||||
controller.Save();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 897bc9b25a26839409ec026d49a9b6ad
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,191 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace XFFSM
|
||||
{
|
||||
public class FSMStateNodeFactory
|
||||
{
|
||||
public static FSMStateNodeData CreateStateNode(RuntimeFSMController controller, string name, Rect rect, bool defaultState,bool isSubStateMachine = false,List<string> parent = null,bool isBuildInState = false,string buildInStateName = "")
|
||||
{
|
||||
|
||||
// 判断名称是否重复
|
||||
if (controller.GetStateNodeData(name) != null)
|
||||
{
|
||||
string message = string.Format("创建状态节点失败,名称:{0}重复!", name);
|
||||
Debug.LogError(message);
|
||||
FSMEditorWindow.ShowNotification(message);
|
||||
return null;
|
||||
}
|
||||
|
||||
FSMStateNodeData node = new FSMStateNodeData();
|
||||
node.name = name;
|
||||
node.rect = rect;
|
||||
node.isSubStateMachine = isSubStateMachine;
|
||||
node.parents = parent;
|
||||
node.isBuildInState = isBuildInState;
|
||||
node.buildInStateName = buildInStateName;
|
||||
if (defaultState)
|
||||
{
|
||||
foreach (var item in controller.states)
|
||||
{
|
||||
// 把相同层级的默认状态设置成false
|
||||
if (!item.CompareParent(node)) continue;
|
||||
item.defaultState = false;
|
||||
}
|
||||
}
|
||||
|
||||
node.defaultState = defaultState;
|
||||
controller.AddState(node);
|
||||
AssetDatabase.SaveAssets();
|
||||
return node;
|
||||
}
|
||||
|
||||
public static FSMStateNodeData CreateStateNode(RuntimeFSMController controller, Rect rect, bool defaultState, bool isSubStateMachine = false, List<string> parent = null,bool isBuildInState = false, string buildInStateName = "") {
|
||||
|
||||
return CreateStateNode(controller,GetStateNodeName(controller),rect,defaultState,isSubStateMachine,parent, isBuildInState, buildInStateName);
|
||||
}
|
||||
|
||||
private static string GetStateNodeName(RuntimeFSMController controller) {
|
||||
|
||||
string name = null;
|
||||
int i = 1;
|
||||
do
|
||||
{
|
||||
name = string.Format("New State{0}", i);
|
||||
i++;
|
||||
} while (controller.GetStateNodeData(name) != null);
|
||||
|
||||
|
||||
|
||||
return name;
|
||||
}
|
||||
|
||||
public static void DeleteState(RuntimeFSMController controller,FSMStateNodeData state) {
|
||||
|
||||
// 判断 删除的是不是 entry 或者 any
|
||||
if (state.IsAnyState || state.IsEntryState || state.IsUpState)
|
||||
{
|
||||
if (state.BaseLayer())
|
||||
{
|
||||
string message = string.Format("状态:{0}不能删除!", state.DisplayName);
|
||||
Debug.LogWarning(message);
|
||||
FSMEditorWindow.ShowNotification(message);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 删除相关的过渡
|
||||
for (int i = 0; i < controller.transitions.Count; i++)
|
||||
{
|
||||
FSMTransitionData transitionData = controller.transitions[i];
|
||||
if (transitionData.fromStateName.Equals(state.name) || transitionData.toStateName.Equals(state.name))
|
||||
{
|
||||
controller.RemoveTransition(transitionData);
|
||||
i--;
|
||||
}
|
||||
}
|
||||
|
||||
controller.RemoveState(state);
|
||||
// 判断是不是默认状态
|
||||
if (state.defaultState) {
|
||||
foreach (var item in controller.states)
|
||||
{
|
||||
if (item.IsAnyState || item.IsEntryState || item.IsUpState)
|
||||
continue;
|
||||
|
||||
if (!item.CompareParent(state)) continue;
|
||||
|
||||
item.defaultState = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 判断是不是子状态机
|
||||
if (state.isSubStateMachine) {
|
||||
|
||||
List<FSMStateNodeData> subStates = new List<FSMStateNodeData>();
|
||||
foreach (var item in controller.states)
|
||||
{
|
||||
if (item.BelongToParent(state.name))
|
||||
subStates.Add(item);
|
||||
}
|
||||
|
||||
foreach (var item in subStates)
|
||||
{
|
||||
// 删除子状态
|
||||
DeleteState(controller, item);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static bool Rename(RuntimeFSMController controller,FSMStateNodeData node,string newName) {
|
||||
|
||||
if ( node.name.Equals(FSMConst.entryState) || node.name.Equals(FSMConst.anyState) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(newName))
|
||||
{
|
||||
FSMEditorWindow.ShowNotification("名称不能为空!");
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( controller.GetStateNodeData(newName) !=null )
|
||||
{
|
||||
string message = string.Format("状态重命名失败,名称:{0}已经存在,请使用其他的名称!", newName);
|
||||
FSMEditorWindow.ShowNotification(message);
|
||||
return false;
|
||||
}
|
||||
|
||||
// 找到 相关的过渡 进行修改
|
||||
foreach (var item in controller.transitions)
|
||||
{
|
||||
if (item.fromStateName.Equals(node.name)) {
|
||||
item.fromStateName = newName;
|
||||
}
|
||||
|
||||
if (item.toStateName.Equals(node.name)) {
|
||||
item.toStateName = newName;
|
||||
}
|
||||
}
|
||||
|
||||
string oldName = node.name;
|
||||
|
||||
node.name = newName;
|
||||
|
||||
// 修改子状态层级
|
||||
if (node.isSubStateMachine) {
|
||||
// 查到所有包含该状态的状态
|
||||
List<FSMStateNodeData> subStates = new List<FSMStateNodeData>();
|
||||
foreach (var item in controller.states)
|
||||
{
|
||||
if (item.ContainsParent(oldName)) subStates.Add(item);
|
||||
}
|
||||
|
||||
foreach (var item in subStates)
|
||||
{
|
||||
// 重命名
|
||||
item.RenameParent(oldName, newName);
|
||||
|
||||
if (item.isBuildInState)
|
||||
{
|
||||
string itemNewName = string.Format("{0}/{1}",item.ParentPath,item.buildInStateName);
|
||||
Rename(controller,item, itemNewName);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
controller.ClearCache();
|
||||
EditorUtility.SetDirty(controller);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f4b272a7b6b47be4b9f4cbc29ee45bc9
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,54 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace XFFSM
|
||||
{
|
||||
public class FSMTransitionFactory
|
||||
{
|
||||
|
||||
public static void CreateTransition(RuntimeFSMController controller,string fromStateName,string toStateName) {
|
||||
|
||||
FSMStateNodeData toNode = controller.GetStateNodeData(toStateName);
|
||||
|
||||
if (toNode.IsAnyState || toNode.IsEntryState || toNode.IsUpState )
|
||||
{
|
||||
string message = string.Format("状态:{0}不能添加过渡!", toNode.DisplayName);
|
||||
Debug.LogWarning(message);
|
||||
FSMEditorWindow.ShowNotification(message);
|
||||
return;
|
||||
}
|
||||
|
||||
if (fromStateName.Equals(toStateName)) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var item in controller.transitions)
|
||||
{
|
||||
if (item.fromStateName.Equals(fromStateName) && item.toStateName.Equals(toStateName) )
|
||||
{
|
||||
string message = string.Format("过渡 {0} -> {1} 已经存在,请勿重复添加!", fromStateName, toStateName);
|
||||
|
||||
Debug.LogError(message);
|
||||
FSMEditorWindow.ShowNotification(message);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
FSMTransitionData transition = new FSMTransitionData();
|
||||
transition.fromStateName = fromStateName;
|
||||
transition.toStateName = toStateName;
|
||||
controller.AddTransition(transition);
|
||||
UnityEditor.EditorUtility.SetDirty(controller);
|
||||
UnityEditor.AssetDatabase.SaveAssets();
|
||||
|
||||
}
|
||||
|
||||
public static void DeleteTransition(RuntimeFSMController controller,FSMTransitionData transition) {
|
||||
controller.RemoveTransition(transition);
|
||||
UnityEditor.EditorUtility.SetDirty(controller);
|
||||
UnityEditor.AssetDatabase.SaveAssets();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c40b3be288bcec747ab41b63ee243e59
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user