mirror of
https://github.com/tuyoogame/YooAsset.git
synced 2026-05-16 04:30:12 +00:00
Update samples
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1a79eedc3fa88c8429b085954a7e9093
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class EventGroup
|
||||
{
|
||||
private readonly Dictionary<System.Type, List<Action<IEventMessage>>> _cachedListener = new Dictionary<System.Type, List<Action<IEventMessage>>>();
|
||||
|
||||
/// <summary>
|
||||
/// 添加一个监听
|
||||
/// </summary>
|
||||
public void AddListener<TEvent>(System.Action<IEventMessage> listener) where TEvent : IEventMessage
|
||||
{
|
||||
System.Type eventType = typeof(TEvent);
|
||||
if (_cachedListener.ContainsKey(eventType) == false)
|
||||
_cachedListener.Add(eventType, new List<Action<IEventMessage>>());
|
||||
|
||||
if (_cachedListener[eventType].Contains(listener) == false)
|
||||
{
|
||||
_cachedListener[eventType].Add(listener);
|
||||
EventManager.AddListener(eventType, listener);
|
||||
}
|
||||
else
|
||||
{
|
||||
UnityEngine.Debug.LogWarning($"Event listener is exist : {eventType}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 移除所有缓存的监听
|
||||
/// </summary>
|
||||
public void RemoveAllListener()
|
||||
{
|
||||
foreach (var pair in _cachedListener)
|
||||
{
|
||||
System.Type eventType = pair.Key;
|
||||
for (int i = 0; i < pair.Value.Count; i++)
|
||||
{
|
||||
EventManager.RemoveListener(eventType, pair.Value[i]);
|
||||
}
|
||||
pair.Value.Clear();
|
||||
}
|
||||
_cachedListener.Clear();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1e0ee5b75a4bc4b49b08f174399b9f01
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,171 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
/// <summary>
|
||||
/// 事件管理器
|
||||
/// </summary>
|
||||
public static class EventManager
|
||||
{
|
||||
private class PostWrapper
|
||||
{
|
||||
public int PostFrame;
|
||||
public int EventID;
|
||||
public IEventMessage Message;
|
||||
|
||||
public void OnRelease()
|
||||
{
|
||||
PostFrame = 0;
|
||||
EventID = 0;
|
||||
Message = null;
|
||||
}
|
||||
}
|
||||
|
||||
private static readonly Dictionary<int, List<Action<IEventMessage>>> _listeners = new Dictionary<int, List<Action<IEventMessage>>>(1000);
|
||||
private static readonly List<PostWrapper> _postWrappers = new List<PostWrapper>(1000);
|
||||
|
||||
|
||||
public static void Update()
|
||||
{
|
||||
for (int i = _postWrappers.Count - 1; i >= 0; i--)
|
||||
{
|
||||
var wrapper = _postWrappers[i];
|
||||
if (UnityEngine.Time.frameCount > wrapper.PostFrame)
|
||||
{
|
||||
SendMessage(wrapper.EventID, wrapper.Message);
|
||||
_postWrappers.RemoveAt(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 添加监听
|
||||
/// </summary>
|
||||
public static void AddListener<TEvent>(System.Action<IEventMessage> listener) where TEvent : IEventMessage
|
||||
{
|
||||
AddListener(typeof(TEvent), listener);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加监听
|
||||
/// </summary>
|
||||
public static void AddListener(System.Type eventType, System.Action<IEventMessage> listener)
|
||||
{
|
||||
int eventId = eventType.GetHashCode();
|
||||
AddListener(eventId, listener);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加监听
|
||||
/// </summary>
|
||||
public static void AddListener(int eventId, System.Action<IEventMessage> listener)
|
||||
{
|
||||
if (_listeners.ContainsKey(eventId) == false)
|
||||
_listeners.Add(eventId, new List<Action<IEventMessage>>());
|
||||
if (_listeners[eventId].Contains(listener) == false)
|
||||
_listeners[eventId].Add(listener);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 移除监听
|
||||
/// </summary>
|
||||
public static void RemoveListener<TEvent>(System.Action<IEventMessage> listener) where TEvent : IEventMessage
|
||||
{
|
||||
RemoveListener(typeof(TEvent), listener);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 移除监听
|
||||
/// </summary>
|
||||
public static void RemoveListener(System.Type eventType, System.Action<IEventMessage> listener)
|
||||
{
|
||||
int eventId = eventType.GetHashCode();
|
||||
RemoveListener(eventId, listener);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 移除监听
|
||||
/// </summary>
|
||||
public static void RemoveListener(int eventId, System.Action<IEventMessage> listener)
|
||||
{
|
||||
if (_listeners.ContainsKey(eventId))
|
||||
{
|
||||
if (_listeners[eventId].Contains(listener))
|
||||
_listeners[eventId].Remove(listener);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 实时广播事件
|
||||
/// </summary>
|
||||
public static void SendMessage(IEventMessage message)
|
||||
{
|
||||
int eventId = message.GetType().GetHashCode();
|
||||
SendMessage(eventId, message);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 实时广播事件
|
||||
/// </summary>
|
||||
public static void SendMessage(int eventId, IEventMessage message)
|
||||
{
|
||||
if (_listeners.ContainsKey(eventId) == false)
|
||||
return;
|
||||
|
||||
List<Action<IEventMessage>> listeners = _listeners[eventId];
|
||||
for (int i = listeners.Count - 1; i >= 0; i--)
|
||||
{
|
||||
listeners[i].Invoke(message);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 延迟广播事件
|
||||
/// </summary>
|
||||
public static void PostMessage(IEventMessage message)
|
||||
{
|
||||
int eventId = message.GetType().GetHashCode();
|
||||
PostMessage(eventId, message);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 延迟广播事件
|
||||
/// </summary>
|
||||
public static void PostMessage(int eventId, IEventMessage message)
|
||||
{
|
||||
var wrapper = new PostWrapper();
|
||||
wrapper.PostFrame = UnityEngine.Time.frameCount;
|
||||
wrapper.EventID = eventId;
|
||||
wrapper.Message = message;
|
||||
_postWrappers.Add(wrapper);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 清空所有监听
|
||||
/// </summary>
|
||||
public static void ClearListeners()
|
||||
{
|
||||
foreach (int eventId in _listeners.Keys)
|
||||
{
|
||||
_listeners[eventId].Clear();
|
||||
}
|
||||
_listeners.Clear();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取监听者总数
|
||||
/// </summary>
|
||||
private static int GetAllListenerCount()
|
||||
{
|
||||
int count = 0;
|
||||
foreach (var list in _listeners)
|
||||
{
|
||||
count += list.Value.Count;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 195a0b4317373b44d9289d7b0529eb7c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,4 @@
|
||||
|
||||
public interface IEventMessage
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9f3747f3c9166b74f946fcdfde7f7827
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cb1f2313c4b23304f9257ac58c1024f8
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,121 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
/// <summary>
|
||||
/// 有限状态机
|
||||
/// </summary>
|
||||
public static class FsmManager
|
||||
{
|
||||
private static readonly List<IFsmNode> _nodes = new List<IFsmNode>();
|
||||
private static IFsmNode _curNode;
|
||||
private static IFsmNode _preNode;
|
||||
|
||||
/// <summary>
|
||||
/// 当前运行的节点名称
|
||||
/// </summary>
|
||||
public static string CurrentNodeName
|
||||
{
|
||||
get { return _curNode != null ? _curNode.Name : string.Empty; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 之前运行的节点名称
|
||||
/// </summary>
|
||||
public static string PreviousNodeName
|
||||
{
|
||||
get { return _preNode != null ? _preNode.Name : string.Empty; }
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 启动状态机
|
||||
/// </summary>
|
||||
/// <param name="entryNode">入口节点</param>
|
||||
public static void Run(string entryNode)
|
||||
{
|
||||
_curNode = GetNode(entryNode);
|
||||
_preNode = GetNode(entryNode);
|
||||
|
||||
if (_curNode != null)
|
||||
_curNode.OnEnter();
|
||||
else
|
||||
UnityEngine.Debug.LogError($"Not found entry node : {entryNode}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新状态机
|
||||
/// </summary>
|
||||
public static void Update()
|
||||
{
|
||||
if (_curNode != null)
|
||||
_curNode.OnUpdate();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 加入一个节点
|
||||
/// </summary>
|
||||
public static void AddNode(IFsmNode node)
|
||||
{
|
||||
if (node == null)
|
||||
throw new ArgumentNullException();
|
||||
|
||||
if (_nodes.Contains(node) == false)
|
||||
{
|
||||
_nodes.Add(node);
|
||||
}
|
||||
else
|
||||
{
|
||||
UnityEngine.Debug.LogWarning($"Node {node.Name} already existed");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 转换节点
|
||||
/// </summary>
|
||||
public static void Transition(string nodeName)
|
||||
{
|
||||
if (string.IsNullOrEmpty(nodeName))
|
||||
throw new ArgumentNullException();
|
||||
|
||||
IFsmNode node = GetNode(nodeName);
|
||||
if (node == null)
|
||||
{
|
||||
UnityEngine.Debug.LogError($"Can not found node {nodeName}");
|
||||
return;
|
||||
}
|
||||
|
||||
UnityEngine.Debug.Log($"FSM change {_curNode.Name} to {node.Name}");
|
||||
_preNode = _curNode;
|
||||
_curNode.OnExit();
|
||||
_curNode = node;
|
||||
_curNode.OnEnter();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 返回到之前的节点
|
||||
/// </summary>
|
||||
public static void RevertToPreviousNode()
|
||||
{
|
||||
Transition(PreviousNodeName);
|
||||
}
|
||||
|
||||
private static bool IsContains(string nodeName)
|
||||
{
|
||||
for (int i = 0; i < _nodes.Count; i++)
|
||||
{
|
||||
if (_nodes[i].Name == nodeName)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
private static IFsmNode GetNode(string nodeName)
|
||||
{
|
||||
for (int i = 0; i < _nodes.Count; i++)
|
||||
{
|
||||
if (_nodes[i].Name == nodeName)
|
||||
return _nodes[i];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bcbf86a86eb115a41b6aa61fb0945d43
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,12 @@
|
||||
|
||||
public interface IFsmNode
|
||||
{
|
||||
/// <summary>
|
||||
/// 节点名称
|
||||
/// </summary>
|
||||
string Name { get; }
|
||||
|
||||
void OnEnter();
|
||||
void OnUpdate();
|
||||
void OnExit();
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 40fe73297598be84fb8dadedd4dd17e6
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user