FSMManager
This manager check config files to found a fsm and a config for the current loaded scene (GameManager calls LoadFSM). If not found, use the default fsm.
using System.Collections;
using System.Collections.Generic;
using EODE.Wonderland;
using UnityEngine;
namespace MyProject {
public class FSMManager : MonoSingleton<FSMManager> {
protected override bool NeverReplace => true;
GameObject _defaultFSM;
FSMConfig _defaultConfig;
FSMAutoLoad[] _list;
public IConfigurableStateMachine CurrentMachine { get; protected set; }
public void Init(GameObject defaultFSM, FSMConfig defaultConfig, FSMAutoLoad[] list) {
_defaultFSM = defaultFSM;
_defaultConfig = defaultConfig;
_list = list;
}
public void LoadFSM(UnityEngine.SceneManagement.Scene target) {
foreach (var fsmal in _list) {
foreach (var al in fsmal.AutoLoads) {
if (al.Scene.Path == target.path) {
LoadFSM(target, fsmal.FSM, al.Config);
return;
}
}
}
Debug.LogWarning("FSM not found; Use default for map : " + target.path);
LoadFSM(target, _defaultFSM, _defaultConfig);
}
public void LoadFSM(UnityEngine.SceneManagement.Scene target, GameObject fsmgo, FSMConfig conf) {
var go = GameObject.Instantiate(fsmgo);
UnityEngine.SceneManagement.SceneManager.MoveGameObjectToScene(go, target);
CurrentMachine = go.GetComponent<IConfigurableStateMachine>();
CurrentMachine.SetConfig(conf);
}
}
}
namespace MyProject {
[CreateAssetMenu(menuName = "MyProject/FSMAutoLoad", fileName = "FSMAutoLoad")]
public class FSMAutoLoad : ScriptableObject {
public GameObject FSM;
public AutoLoad[] AutoLoads;
[System.Serializable]
public class AutoLoad {
public FSMConfig Config;
public SceneField Scene;
}
}
}