实例追踪类
实例追踪类
InstanceTracker.cs
/// <summary>
/// Monobehavioids which inherit this class will be tracked in the static
/// Instances property.
/// </summary>
/// <typeparam name="T"></typeparam>
public class InstanceTracker<T> : MonoBehaviour where T : MonoBehaviour
{
public static List<T> Instances { get; private set; } = new List<T>();
int instanceIndex = 0;
protected virtual void OnEnable()
{
instanceIndex = Instances.Count;
Instances.Add(this as T);
}
protected virtual void OnDisable()
{
if (instanceIndex < Instances.Count)
{
var end = Instances.Count - 1;
Instances[instanceIndex] = Instances[end];
Instances.RemoveAt(end);
}
}
}作用
代码解释
用例
FadingSprite.cs
FadingSpriteSystem.cs
代码解释
最后更新于