public void StepCollect(Action <object, int> collectListener) { ++collectedIndex; for (int i = 0; i < collectStep; ++i) { collectedIndex += i; if (collectedIndex >= count) { collectedIndex = -1; return; } PoolNode node = list[collectedIndex]; object o = node.obj; if (o != null && o.Equals(null)) { node.obj = null; if (collectListener != null) { collectListener(o, collectedIndex); } } } }
public T TakeFromActiveObjs() { var first = m_FirstNode; var last = m_LastNode; if (first == null) { return(null); } var node = first; if (node.item == null) { throw new NullReferenceException(); } OnDeactivation(node.item); if (first == last) { OnActivation(node.item); return(node.item); } node.next.prev = null; first = node.next; node.next = null; last.next = node; node.prev = last; last = node; m_FirstNode = first; m_LastNode = last; OnActivation(node.item); return(node.item); }
public void ReturnNode(PoolNode node) { if (count < poolParams.Size || poolParams.Size < 0) { Push(node); } }
public LuaObjectPool() { list = new List <PoolNode>(1024); head = new PoolNode(0, null); list.Add(head); count = 1; }
public T TakeFromInactiveObjs() { if (m_InactiveObjs.Count < 1) { return(null); } var node = m_InactiveObjs.Pop(); if (node.item == null) { throw new NullReferenceException(); } var first = m_FirstNode; var last = m_LastNode; m_InactiveObjsSet.Remove(node.item); m_ActiveObjsDict.Add(node.item, node); if (first == null) { first = node; } if (last != null) { last.next = node; node.prev = last; } last = node; m_FirstNode = first; m_LastNode = last; OnActivation(node.item); return(node.item); }
public int Add(GameObject obj, int parent) { int pos = -1; if (head.index != 0) { pos = head.index; PoolNode node = list[pos]; node.obj = obj; head.index = node.index; if (parent > 0 && parent < count) { node.parent = parent; list[parent].AddChild(node.index); } } else { pos = list.Count; list.Add(new PoolNode(pos, obj)); list[pos].parent = parent; count = pos + 1; } return(pos); }
public void Clear() { list.Clear(); head = null; count = 0; _instance = null; }
private PoolNode Create() { PoolNode node = emptyNodesPool.GetNode(); node.Value = new T(); return(node); }
public LuaObjectPool() { list = new List<PoolNode>(1024); head = new PoolNode(0, null); list.Add(head); list.Add(new PoolNode(1, null)); count = list.Count; }
public JSObjectPool() { list = new List <PoolNode>(1024); head = new PoolNode(0, null); list.Add(head); list.Add(new PoolNode(1, null)); count = list.Count; }
public RubyObjectPool() { list = new List <PoolNode> (512); head = new PoolNode(0, null); list.Add(head); // list.Add ( new PoolNode ( 1, null ) ); count = list.Count; }
/// <summary> /// Send the given object to the recycle pool. /// </summary> /// <param name="value"></param> public static void Recycle(T value) { PoolNode node = PoolNode.GetNewNode(); node.Value = value; node.Next = _root; _root = node; }
public LuaObjectPool() { list = new NodeBase[1024]; object temp = new object(); list[0] = new PoolNode <object>(0, temp); list[1] = new PoolNode <object>(1, temp); count = 2; }
public Transform GetTransform(int pos) { if (pos > 0 && pos < count) { PoolNode node = list[pos]; return(node.transform); } return(null); }
public void ReturnPooledInstance(object instance) { if (count < poolParams.Size || poolParams.Size < 0) { PoolNode node = emptyNodesPool.GetNode(); node.Value = instance; Push(node); } }
private PoolNode Pop() { PoolNode node = head; head = head.Next; node.Next = null; count--; return(node); }
private T Pop() { PoolNode node = head; head = head.Next; node.Next = null; T value = (T)node.Value; emptyNodesPool.ReturnNode(node); count--; return(value); }
/// <summary> /// Gets the pool's current capacity /// </summary> public static uint Count() { uint count = 0; PoolNode it = _root; while (it != null) { count++; it = it.Next; } return(count); }
public static PoolNode GetNewNode() { if (_root == null) { return(new PoolNode()); } var n = _root; _root = _root.Next; n.Next = null; n.Value = null; return(n); }
public object Replace(int pos, object o) { if (pos > 0 && pos < count) { object obj = list[pos].obj; list[pos].obj = o; #if UNITY_EDITOR list[pos].debugLog = PoolNode.getPath(o); #endif return(obj); } return(null); }
public IEnumerator <T> GetEnumerator() { foreach (var node in m_InactiveObjs) { yield return(node.item); } PoolNode curNode = m_FirstNode; while (curNode != null) { yield return(curNode.item); curNode = curNode.next; } }
//需要检查避免重复 public int New(GameObject obj, int parent) { #if UNITY_EDITOR for (int i = 0; i < list.Count; i++) { PoolNode node = list[i]; if (node != null && node.obj == obj) { Debug.Log("GameObject Get Twice Obj Name = " + obj.name); } } #endif int pos = Add(obj, parent); return(pos); }
/// <summary> /// Returns a recycled object instance. /// If there is no objects inside the pool, a new one is created. /// </summary> public static T GetObject() { if (_root == null) { GeneratedAmount++; return(new T()); } PoolNode node = _root; _root = node.Next; var value = (T)node.Value; node.Value = null; PoolNode.Recycle(node); return(value); }
// 获取一个废弃的对象 public T getObjectInstance() { if (length == 0) { return(addNewObject().t); } else { if (lastActive.next != firstActive) // 还有废弃的没有,直接拿出去 { lastActive = lastActive.next; return(lastActive.t); } else // 没有废弃的了,需要添加新的进来 { return(addNewObject().t); } } }
public bool Add <T>(T obj, out int pos) { NodeBase node = null; pos = -1; var head = PoolNode <T> .head; if (head.index != 0) { pos = head.index; node = list[pos]; var gNode = node as PoolNode <T>; gNode.obj = obj; gNode.eleType = obj.GetType(); head.index = node.index; } else { if (count == list.Length) { EnsureCapacity(count + 1); } pos = count; node = new PoolNode <T>(pos, obj); list[pos] = node; count++; } #if UNITY_EDITOR Type dataType = TypeTraits <T> .type; if (node.bObjectType && dataType != objType) { Debugger.LogError("strict object type required, got :" + node.eleType); return(false); } #endif StackDataMapping <T> .InstanceGet(this).Add(obj, pos); return(true); }
public bool ReturnToPool(T obj) { if (obj == null) { return(false); } if (!m_ActiveObjsDict.TryGetValue(obj, out var node)) { return(false); } if (node.item == null) { throw new NullReferenceException(); } OnDeactivation(node.item); if (node.prev == null) { m_FirstNode = node.next; } else { node.prev.next = node.next; node.prev = null; } if (node.next == null) { m_LastNode = node.prev; } else { node.next.prev = node.prev; node.next = null; } m_ActiveObjsDict.Remove(node.item); m_InactiveObjsSet.Add(node.item); m_InactiveObjs.Push(node); return(true); }
public int Add(object obj) { int pos = -1; if (head.index != 0) { pos = head.index; list[pos].obj = obj; #if UNITY_EDITOR list[pos].debugLog = PoolNode.getPath(obj); #endif head.index = list[pos].index; } else { pos = list.Count; list.Add(new PoolNode(pos, obj)); count = pos + 1; } return(pos); }
// 往对象池内添加新对象 private PoolNode <T> addNewObject() { PoolNode <T> node = new PoolNode <T>(); if (length == 0) // 空 { node.next = node; node.previous = node; firstActive = node; lastActive = node; } else // 满 { lastActive.next.previous = node; node.next = lastActive.next; lastActive.next = node; node.previous = lastActive; lastActive = node; } return(node); }
//关于Destory的一些问题 //调用前删除(对象) //是否child需要从List中移除 //是否父对象需要删除子对象 //删除成为head public GameObject Destroy(int pos, bool detach = true) { if (pos > 0 && pos < count) { PoolNode node = list[pos]; GameObject o = node.obj; node.obj = null; node.index = head.index; node.mComponents.Clear(); for (int i = 0; i != node.mChilds.Count; i++) { Destroy(node.mChilds[i], false); //子物体删除 } node.mChilds.Clear(); head.index = pos; //下一个Add使用此位置 if (detach && node.parent != 0) { PoolNode parent = list[node.parent]; parent.RemoveChild(pos); } return(o); } return(null); }
public void Clear() { list.Clear(); head = null; count = 0; }
private void Push(PoolNode node) { node.Next = head; head = node; count++; }