Unity ET study notes - a simple implementation of a message mechanism

I have always wondered why this message can be realized by adding features and implementing the inheritance rewriting method in ET. Let me record the simple version of the implementation process.

The meaning of the characteristic: scan all classes with characteristics in the assembly

Call the function of obtaining the generic class in the parent class, and the obtained result is temporarily called the component type

Bind the component type to this class and add it to the dictionary

Find the corresponding class through the component type, call the interface function of the corresponding class, in which the interface function implements our rewriting method

The more convoluted part is the above part.

When we find the corresponding function of the corresponding type, it will be easy to say

System represents our logic layer, and component represents our data layer. No matter how you write it, since we can get data, we can write our own logic normally.

code show as below:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public interface ISystem
{
	Type Type();
	void Run(object o);
}
public abstract class MoveSystem<E> : ISystem
{
	public void Run(object o)
	{
		this.Start((E)o);
	}
	public Type Type()
	{
		return typeof(E);
	}
	public abstract void Start(E self);
}
[ObjectSystem]
public class HeroStartSystem : MoveSystem<MoveComponent>
{
	public override void Start(MoveComponent self)
	{
		self.Start();
	}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public abstract class Component
{
	public int SoleID { get; set; }
}

public class MoveComponent : Component
{
	public string moveName = "";
	public int moveSpeed { get; set; }
	public int veerSpeed { get; set; }
}

public static class ComponentHelper
{
	public static void Start(this MoveComponent moveComponent)
	{
		moveComponent.Swiftly();
		moveComponent.Slowly();
		Debug.Log(moveComponent.moveName);
	}
	public static void Swiftly(this MoveComponent moveComponent)
	{
		Debug.Log("-------S------");
	}
	public static void Slowly(this MoveComponent moveComponent)
	{
		Debug.Log("-------L------");
	}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Reflection;


[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public class ObjectSystemAttribute : Attribute
{

}

public class EventSystem
{
	public static EventSystem instance;
	public static EventSystem Instance
	{
		get
		{
			return instance ?? (instance = new EventSystem());
		}
	}
	private readonly UnOrderMultiMap<Type, ISystem> startSystems = new UnOrderMultiMap<Type, ISystem>();

	// 扫描添加的程序集中的ObjectSystemAttribute对象
	public void Add(Assembly assembly)
	{

		foreach (Type type in assembly.GetTypes())
		{
			object[] objects = type.GetCustomAttributes(typeof(ObjectSystemAttribute), false);
			if (objects.Length == 0)
			{
				continue;
			}

			object obj = Activator.CreateInstance(type);

			switch (obj)
			{
				case ISystem startSystem:
					this.startSystems.Add(startSystem.Type(), startSystem);
					break;
			}
		}

	}

	// 调用组件的Start方法
	public void Start(Component component)
	{
		if (startSystems.Count == 0)
		{
			return;
		}
		List<ISystem> iStartSystems = this.startSystems[component.GetType()];
		if (iStartSystems == null)
		{
			return;
		}

		foreach (ISystem iStartSystem in iStartSystems)
		{
			try
			{
				iStartSystem.Run(component);
				// startSystems.Remove(component.GetType());
			}
			catch (Exception e)
			{
				Console.WriteLine(e);
			}
		}

		startSystems.Remove(component.GetType());

	}
}




public class UnOrderMultiMap<T, K>
{
	// 重用list
	private readonly Queue<List<K>> queue = new Queue<List<K>>();
	private readonly Dictionary<T, List<K>> dictionary = new Dictionary<T, List<K>>();
	public void Add(T t, K k)
	{
		List<K> list;
		this.dictionary.TryGetValue(t, out list);
		if (list == null)
		{
			list = this.FetchList();
			this.dictionary[t] = list;
		}
		list.Add(k);
	}
	public bool Remove(T t)
	{
		List<K> list = null;
		this.dictionary.TryGetValue(t, out list);
		if (list != null)
		{
			this.RecycleList(list);
		}
		return this.dictionary.Remove(t);
	}
	public List<K> this[T t]
	{
		get
		{
			List<K> list;
			this.dictionary.TryGetValue(t, out list);
			return list;
		}
	}
	public int Count
	{
		get
		{
			return this.dictionary.Count;
		}
	}


	private List<K> FetchList()
	{
		if (this.queue.Count > 0)
		{
			List<K> list = this.queue.Dequeue();
			list.Clear();
			return list;
		}
		return new List<K>();
	}

	private void RecycleList(List<K> list)
	{
		// 防止暴涨
		if (this.queue.Count > 100)
		{
			return;
		}
		list.Clear();
		this.queue.Enqueue(list);
	}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Reflection;
public class AppStart : MonoBehaviour
{
	private void Start()
	{
		MoveComponent moveComponent = new MoveComponent();
		EventSystem.Instance.Add(typeof(AppStart).Assembly);
		EventSystem.Instance.Start(moveComponent);
	}
}

Guess you like

Origin blog.csdn.net/qq_55042292/article/details/125636177