51 lines
1.5 KiB
C#
51 lines
1.5 KiB
C#
|
|
using System.Collections;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Linq;
|
|||
|
|
using UnityEngine;
|
|||
|
|
using UnityEngine.SceneManagement;
|
|||
|
|
|
|||
|
|
|
|||
|
|
namespace Stary.Evo.FiniteStateMachine
|
|||
|
|
{
|
|||
|
|
public static class FSMInitialize
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// 状态机初始化
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="targetScene">指定场景</param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static FSMManager Init(Scene targetScene)
|
|||
|
|
{
|
|||
|
|
GameObject targetObject = targetScene.GetRootGameObjects().FirstOrDefault(go => go.name == "FSMManger");
|
|||
|
|
FSMManager fSMManager;
|
|||
|
|
|
|||
|
|
if (targetObject == null)
|
|||
|
|
{
|
|||
|
|
targetObject = new GameObject("FSMManger");
|
|||
|
|
SceneManager.MoveGameObjectToScene(targetObject, targetScene);
|
|||
|
|
|
|||
|
|
fSMManager = new FSMManager();
|
|||
|
|
fSMManager.FSMMangerObject = targetObject;
|
|||
|
|
|
|||
|
|
void OnSceneUnloaded(Scene scene)
|
|||
|
|
{
|
|||
|
|
// 当场景卸载时释放缓存
|
|||
|
|
if (scene.name == targetScene.name)
|
|||
|
|
{
|
|||
|
|
GameObject.Destroy(targetObject);
|
|||
|
|
fSMManager = null;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
SceneManager.sceneUnloaded += OnSceneUnloaded;
|
|||
|
|
|
|||
|
|
return fSMManager;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
Debug.LogError("场景: " + targetScene.name + "已经初始化过FSM状态机!!!");
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|