public class SwarmItem : MonoBehaviour
{
protected STATE _state; //用来处理是不是活动的状态
protected SwarmItemManager _swarmItemManager; //父对象的物体引用
protected Transform _thisTransform;
protected int _prefabIndex;
protected float _lifeSpanLeft;
protected bool _debugEvents;
public enum STATE
{
Inactive,
Active
}
public float maximumLifeSpan = 0;
public STATE State
{
get
{
return _state;
}
set
{
// save whether the state was changed
bool _stateChanged = (_state != value);
_state = value;
switch (_state)
{
case STATE.Inactive:
// turn off the item
gameObject.SetActiveRecursively(false);
break;
case STATE.Active:
// turn on the item and reset its life span
gameObject.SetActiveRecursively(true);
_lifeSpanLeft = UnityEngine.Random.Range(minimumLifeSpan, maximumLifeSpan);
break;
}
// if the state changed, call the OnStateChange method
if (_stateChanged)
OnStateChange();
}
}
protected virtual void OnStateChange()
public int PrefabIndex //预设的标签数?
{
get
{
return _prefabIndex;
}
}
//重要 设置SwarmItemManager 和 预设的标签 是否debug等
public virtual void Initialize(SwarmItemManager swarmItemManager, int prefabIndex, bool debugEvents)
{
_swarmItemManager = swarmItemManager;
_prefabIndex = prefabIndex;
_debugEvents = debugEvents;
_thisTransform = this.transform;
State = STATE.Inactive;
}
/// <summary>
/// This method is called by the manager after the item is moved to either the active or inactive list.
/// It is often useful to modify an item's position, rotation, or scale here since these values will
/// be set to Vector3.zero, Quaternion.Identity, and Vector3.one, respectively.
/// </summary> 相当于回复之后的初始化,给子物体使用
public virtual void OnSetParentTransform()
{
}
public virtual void Kill() //放回垃圾回收池
{
State = STATE.Inactive;
_swarmItemManager.DeactiveItem(this);
}
/// <summary>
/// This is called before an object is destroyed by a prune event from the manager.
/// You should call Destroy on any objects in your inherited class that need their memory freed
/// using an overload of this method.
/// </summary>
public virtual void PreDestroy() //物体预先被销毁时调用
{
}
public virtual void FrameUpdate()
{
if (_lifeSpanLeft > 0)
{
_lifeSpanLeft -= Time.deltaTime;
if (_lifeSpanLeft <= 0)
{
Kill();
if (_debugEvents)
Debug.Log("Lifespan expired for item " + name + " at frame: " + Time.frameCount);
}
}
}
{
protected STATE _state; //用来处理是不是活动的状态
protected SwarmItemManager _swarmItemManager; //父对象的物体引用
protected Transform _thisTransform;
protected int _prefabIndex;
protected float _lifeSpanLeft;
protected bool _debugEvents;
public enum STATE
{
Inactive,
Active
}
//生命值的时间 即留在场上的时间长短
public float maximumLifeSpan = 0;
public STATE State
{
get
{
return _state;
}
set
{
// save whether the state was changed
bool _stateChanged = (_state != value);
_state = value;
switch (_state)
{
case STATE.Inactive:
// turn off the item
gameObject.SetActiveRecursively(false);
break;
case STATE.Active:
// turn on the item and reset its life span
gameObject.SetActiveRecursively(true);
_lifeSpanLeft = UnityEngine.Random.Range(minimumLifeSpan, maximumLifeSpan);
break;
}
// if the state changed, call the OnStateChange method
if (_stateChanged)
OnStateChange();
}
}
protected virtual void OnStateChange()
{
public int PrefabIndex //预设的标签数?
{
get
{
return _prefabIndex;
}
}
//重要 设置SwarmItemManager 和 预设的标签 是否debug等
public virtual void Initialize(SwarmItemManager swarmItemManager, int prefabIndex, bool debugEvents)
{
_swarmItemManager = swarmItemManager;
_prefabIndex = prefabIndex;
_debugEvents = debugEvents;
_thisTransform = this.transform;
State = STATE.Inactive;
}
/// <summary>
/// This method is called by the manager after the item is moved to either the active or inactive list.
/// It is often useful to modify an item's position, rotation, or scale here since these values will
/// be set to Vector3.zero, Quaternion.Identity, and Vector3.one, respectively.
/// </summary> 相当于回复之后的初始化,给子物体使用
public virtual void OnSetParentTransform()
{
}
public virtual void Kill() //放回垃圾回收池
{
State = STATE.Inactive;
_swarmItemManager.DeactiveItem(this);
}
/// <summary>
/// This is called before an object is destroyed by a prune event from the manager.
/// You should call Destroy on any objects in your inherited class that need their memory freed
/// using an overload of this method.
/// </summary>
public virtual void PreDestroy() //物体预先被销毁时调用
{
}
public virtual void FrameUpdate()
{
if (_lifeSpanLeft > 0)
{
_lifeSpanLeft -= Time.deltaTime;
if (_lifeSpanLeft <= 0)
{
Kill();
if (_debugEvents)
Debug.Log("Lifespan expired for item " + name + " at frame: " + Time.frameCount);
}
}
}
}
这就是这个垃圾回收的基类的item 感觉还是十分有用的...,但是貌似如果是大boss的话貌似没什么效率...