{"id":254992,"date":"2015-04-10T21:00:02","date_gmt":"2015-04-10T17:00:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=254992"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=254992","title":{"rendered":"Pool \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0434\u043b\u044f Unity3d"},"content":{"rendered":"<p>     \t<habracut\/><br \/>  \u0412\u0441\u0435\u043c \u043f\u0440\u0438\u0432\u0435\u0442. <\/p>\n<p>  \u0412\u0441\u0435 \u0437\u043d\u0430\u044e\u0442 \u0447\u0442\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u043e\u0431\u044b\u0447\u043d\u043e \u043d\u0435 \u0434\u0435\u0448\u0435\u0432\u044b\u0435. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u043f\u0443\u043b\u044e \u0438 \u0443\u043d\u0438\u0447\u0442\u043e\u0436\u0430\u0442\u044c, \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043d\u0430\u043a\u043b\u0430\u0434\u043d\u043e \u0434\u043b\u044f \u0442\u0435\u0445 \u0436\u0435 \u043c\u043e\u0431\u0438\u043b\u044c\u043d\u044b\u0445 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432. \u041c\u043e\u0436\u0435\u0442 \u0441\u0442\u043e\u0438\u0442 \u043d\u0435 \u0443\u043d\u0438\u0447\u0442\u043e\u0436\u0430\u0442\u044c \u043f\u0443\u043b\u044e, \u0430 \u0441\u043a\u0440\u044b\u0432\u0430\u0442\u044c \u0435\u0435. \u0412\u043e\u0442 \u0440\u0435\u0448\u0438\u043b \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u0441\u044f \u0441\u0432\u043e\u0435\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0435\u0439 Pool Manager. \u041a\u043e\u0442\u043e\u0440\u044b\u0439 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e \u0432 \u0440\u0430\u0437\u043d\u044b\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445, \u0432 \u0442\u043e\u043c \u0447\u0438\u0441\u043b\u0435 \u0438 \u043d\u0430 photon server.<\/p>\n<h4>\u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b<\/h4>\n<p>  \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430, \u043d\u0443\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441.  <\/p>\n<pre><code class=\"cs\">public interface IPoolObject&lt;T&gt; {     T Group { get; }     void Create();     void OnPush();     void FailedPush();    } <\/code><\/pre>\n<p>  \u0413\u0434\u0435 \u043c\u0435\u0442\u043e\u0434 Create() \u0431\u0443\u0434\u0435\u0442 \u0438\u0433\u0440\u0430\u0442\u044c \u0440\u043e\u043b\u044c \u043f\u0441\u0435\u0432\u0434\u043e-\u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430. \u0412\u0435\u0434\u044c \u043a\u043e\u0433\u0434\u0430 \u0432\u044b \u0434\u043e\u0441\u0442\u0430\u043d\u0435\u0442\u0435 \u043e\u0431\u044a\u0435\u043a\u0442 \u0438\u0437 \u043f\u0443\u043b\u0430, \u0435\u0433\u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0431\u0443\u0434\u0435\u0442 \u043d\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043e, \u0447\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u043f\u0430\u0433\u0443\u0431\u043d\u043e \u043e\u0442\u0440\u0430\u0437\u0438\u0442\u0441\u044f \u043d\u0430 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u043c \u0435\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438.<\/p>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u0441\u0430\u043c Pool Manager  <\/p>\n<pre><code class=\"cs\">using System.Collections.Generic; using System;  public class PoolManager&lt;K, V&gt; where V :IPoolObject&lt;K&gt; {     public virtual int MaxInstances { get; protected set; }     public virtual int InctanceCount { get { return objects.Count; } }     public virtual int CacheCount { get { return cache.Count; } }      public delegate bool Compare&lt;T&gt;(T value) where T: V;      protected Dictionary&lt;K, List&lt;V&gt;&gt; objects;     protected Dictionary&lt;Type, List&lt;V&gt;&gt; cache;      public PoolManager(int maxInstance)      {         MaxInstances = maxInstance;         objects = new Dictionary&lt;K, List&lt;V&gt;&gt;();         cache = new Dictionary&lt;Type, List&lt;V&gt;&gt;();     }      public virtual bool CanPush()     {         return InctanceCount + 1 &lt; MaxInstances;     }      public virtual bool Push(K groupKey, V value)     {         bool result = false;         if (CanPush())         {             value.OnPush();             if (!objects.ContainsKey(groupKey))             {                 objects.Add(groupKey, new List&lt;V&gt;());             }             objects[groupKey].Add(value);             Type type = value.GetType();             if (!cache.ContainsKey(type))             {                 cache.Add(type, new List&lt;V&gt;());             }             cache[type].Add(value);         }         else         {             value.FailedPush();         }         return result;     }      public virtual T Pop&lt;T&gt;(K groupKey) where T : V     {         T result = default(T);         if (Contains(groupKey) && objects[groupKey].Count &gt; 0)         {              for (int i = 0; i &lt; objects[groupKey].Count; i++)             {                 if (objects[groupKey][i] is T)                 {                     result = (T)objects[groupKey][i];                     Type type = result.GetType();                     RemoveObject(groupKey, i);                     RemoveFromCache(result, type);                     result.Create();                     break;                 }             }         }         return result;     }      public virtual T Pop&lt;T&gt;() where T: V     {         T result = default(T);         Type type = typeof(T);         if (ValidateForPop(type))         {             for (int i = 0; i &lt; cache[type].Count; i++)             {                 result = (T)cache[type][i];                 if (result != null && objects.ContainsKey(result.Group))                 {                     objects[result.Group].Remove(result);                     RemoveFromCache(result, type);                     result.Create();                     break;                 }                              }         }         return result;     }      public virtual T Pop&lt;T&gt;(Compare&lt;T&gt; comparer) where T : V     {         T result = default(T);         Type type = typeof(T);         if (ValidateForPop(type))         {             for(int i = 0; i &lt; cache[type].Count; i++)              {                 T value = (T)cache[type][i];                 if (comparer(value))                 {                     objects[value.Group].Remove(value);                     RemoveFromCache(result, type);                     result = value;                     result.Create();                     break;                 }                            }         }         return result;     }         public virtual bool Contains(K groupKey)     {         return objects.ContainsKey(groupKey);     }      public virtual void Clear()     {         objects.Clear();     }      protected virtual bool ValidateForPop(Type type)     {         return cache.ContainsKey(type) && cache[type].Count &gt; 0;     }      protected virtual void RemoveObject(K groupKey, int idx)     {         if (idx &gt;= 0 && idx &lt; objects[groupKey].Count)         {             objects[groupKey].RemoveAt(idx);             if (objects[groupKey].Count == 0)             {                 objects.Remove(groupKey);             }         }     }      protected void RemoveFromCache(V value, Type type)     {         if (cache.ContainsKey(type))         {             cache[type].Remove(value);             if (cache[type].Count == 0)             {                 cache.Remove(type);             }         }     } } <\/code><\/pre>\n<p>  \u041d\u0430 \u0447\u0442\u043e \u0441\u0442\u043e\u0438\u0442 \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u044c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435. <br \/>  MaxInstances \u2014 \u043f\u043e\u043b\u0435 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 pool \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432. \u0412 \u0441\u043b\u0443\u0447\u0430\u0435, \u0435\u0441\u043b\u0438 \u043d\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u043f\u0443\u043b \u043e\u0447\u0435\u0440\u0435\u0434\u043d\u043e\u0439 \u043e\u0431\u044a\u0435\u043a\u0442, \u0441\u0430\u043c \u043e\u0431\u044a\u0435\u043a\u0442 \u0432\u044b\u0432\u043e\u0437\u0438\u0442 \u043c\u0435\u0442\u043e\u0434 FailedPush(); <br \/>  OnPush() \u2014 \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043f\u0435\u0440\u0435\u0434 \u043f\u043e\u043f\u0430\u0434\u0430\u043d\u0438\u0435\u043c \u0432 \u043f\u0443\u043b. <br \/>  Create() \u2014 \u043f\u0435\u0440\u0435\u0434 \u0442\u0435\u043c, \u043a\u0430\u043a \u043f\u0443\u043b \u0432\u0435\u0440\u043d\u0435\u0442 \u043d\u0430\u043c \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440.<\/p>\n<p>  \u041e\u0431\u0449\u0438\u0439 Pool \u0433\u043e\u0442\u043e\u0432. \u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u0430\u0440\u0438\u0430\u0446\u0438\u044e \u0434\u043b\u044f Unity3d. \u041f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u043c <\/p>\n<pre><code class=\"cs\">using UnityEngine; using System.Collections;  public class UnityPoolManager : MonoBehaviour {     public static UnityPoolManager Instance {get; protected set;}      public int maxInstanceCount = 128;      protected PoolManager&lt;string, UnityPoolObject&gt; poolManager;       protected virtual void Awake()     {         Instance = this;         poolManager = new PoolManager&lt;string, UnityPoolObject&gt;(maxInstanceCount);     }       public virtual bool CanPush()     {         return poolManager.CanPush();     }      public virtual bool Push(string groupKey, UnityPoolObject poolObject)     {         return poolManager.Push(groupKey, poolObject);     }      public virtual T PopOrCreate&lt;T&gt;(T prefab) where T : UnityPoolObject     {         return PopOrCreate(prefab, Vector3.zero, Quaternion.identity);     }      public virtual T PopOrCreate&lt;T&gt;(T prefab, Vector3 position, Quaternion rotation) where T : UnityPoolObject     {         T result = poolManager.Pop&lt;T&gt;(prefab.Group);         if (result == null)         {             result = CreateObject&lt;T&gt;(prefab, position, rotation);         }         else         {             result.SetTransform(position, rotation);         }         return result;     }      public virtual UnityPoolObject Pop(string groupKey)     {         return poolManager.Pop&lt;UnityPoolObject&gt;(groupKey);     }      public virtual T Pop&lt;T&gt;() where T : UnityPoolObject     {         return poolManager.Pop&lt;T&gt;();     }      public virtual T Pop&lt;T&gt;(PoolManager&lt;string, UnityPoolObject&gt;.Compare&lt;T&gt; comparer) where T : UnityPoolObject     {         return poolManager.Pop&lt;T&gt;(comparer);     }      public virtual T Pop&lt;T&gt;(string groupKey) where T : UnityPoolObject     {         return poolManager.Pop&lt;T&gt;(groupKey);     }      public virtual bool Contains(string groupKey)     {         return poolManager.Contains(groupKey);     }      public virtual void Clear()     {         poolManager.Clear();     }      protected virtual T CreateObject&lt;T&gt;(T prefab, Vector3 position, Quaternion rotation) where T : UnityPoolObject     {         GameObject go = Instantiate(prefab.gameObject, position, rotation) as GameObject;         T result = go.GetComponent&lt;T&gt;();         result.name = prefab.name;         return result;     } } <\/code><\/pre>\n<p>  \u041f\u043e \u0441\u0443\u0442\u0438 \u044d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u043e\u0431\u0432\u0435\u0440\u0442\u043a\u0430 \u043d\u0430\u0434 \u043f\u0435\u0440\u0432\u044b\u043c \u043f\u0443\u043b\u043e\u043c \u0438 \u0421\u0438\u043d\u0433\u0435\u043b\u0442\u043e\u043d. <br \/>  PopOrCreate() \u2014 \u043d\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u0432\u043e\u0442 \u044d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432.<br \/>  Push() \u2014 \u0443 \u0441\u0430\u043c\u0438\u0445 \u043f\u0443\u043b \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0438\u043b\u0438 Push \u0432 \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440\u0435.<\/p>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u0441\u0430\u043c GameObject<\/p>\n<pre><code class=\"cs\">using UnityEngine; using System.Collections;  public class UnityPoolObject : MonoBehaviour, IPoolObject&lt;string&gt; {     public virtual string Group { get {return name;} }     public Transform MyTransform { get { return myTransform; } }      protected Transform myTransform;      protected virtual void Awake()     {         myTransform = transform;     }      public virtual void SetTransform(Vector3 position, Quaternion rotation)     {         myTransform.position = position;         myTransform.rotation = rotation;     }      public virtual void Create()     {         gameObject.SetActive(true);     }      public virtual void OnPush()     {         gameObject.SetActive(false);     }      public virtual void Push()     {         UnityPoolManager.Instance.Push(Group, this);     }      public void FailedPush()     {         Debug.Log(&quot;FailedPush&quot;); \/\/ !!!         Destroy(gameObject);     } }  <\/code><\/pre>\n<p>  \u0412\u0441\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0431\u0443\u0434\u0435\u043c \u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u044c \u043e\u0442 \u043d\u0435\u0433\u043e. <br \/>  FailedPush() \u2014 \u0441\u0442\u043e\u0438\u0442 \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u044c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u044d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434. \u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0432\u044b \u0437\u0430\u0445\u043e\u0442\u0438\u0442\u0435 \u0431\u0440\u043e\u0441\u0430\u0442\u044c \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435, \u043c\u043e\u043b \u043f\u0443\u043b \u0437\u0430\u0431\u0438\u0442 \u0438\u043b\u0438 \u0447\u0442\u043e-\u0442\u043e \u0435\u0449\u0435.<\/p>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u043a \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044e. \u041d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043f\u0443\u043b\u0438 \u0441\u043b\u0435\u0434\u043e\u0432 \u0438 UI List item.<\/p>\n<pre><code class=\"cs\">public class Bullet : UnityPoolObject  { ... } \/\/ \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435  Bullet bullet = UnityPoolManager.Instance.PopOrCreate&lt;Bullet&gt;(bulletPrefab, bulletPoint.position, Quaternion.identity);   bullet.Execute(sender, bulletPoint.position, CalculateTarget(target, accuracy01), damage, blockTime, range, bulletSpeed); \/\/ \u0443\u043d\u0438\u0447\u0442\u043e\u0436\u0435\u043d\u0438\u0435, \u0442\u043e\u0447\u043d\u0435\u0439 \u0432\u043e\u0437\u0440\u0430\u0449\u0430\u0435\u043c \u0432 \u043f\u0443\u043b timer-= Time.deltaTime;  if (timer&lt; 0)             {                 Push();             } <\/code><\/pre>\n<pre><code class=\"cs\">public class StepObject : UnityPoolObject  { ... }  \/\/\/ ---  StepObject stepObject = UnityPoolManager.Instance.PopOrCreate&lt;StepObject&gt;(prefab, sender.position, sender.rotation);             FXManager.Instance.InitDecal(null, stepObject.gameObject, hit, direction);             stepObject.MyTransform.rotation *= rotation;             StartCoroutine(ApplyDecalC(stepObject)); \/\/\/ --- protected virtual IEnumerator ApplyDecalC(StepObject decalObject)     {         yield return new WaitForSeconds(waitTime);         yield return StartCoroutine(FXManager.Instance.HideOjectC(decalObject.gameObject, hideTime));         decalObject.Push();     } <\/code><\/pre>\n<pre><code class=\"cs\">public class ProfileListItem : UnityPoolObject  { ... }  \/\/ ---  ProfileListItem profileItem = UnityPoolManager.Instance.PopOrCreate(prefab);  ...  profileItem.profileId = profileId;  list.AddItem(profileItem);  \/\/ ---- <\/code><\/pre>\n<p>  \u041d\u0430\u0434\u0435\u044e\u0441\u044c \u0434\u0430\u043d\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u0432\u0430\u043c \u0432 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0438 \u0441\u0432\u043e\u0438\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u043e\u0432 \u043d\u0430 Unity3d\/     \t<\/p>\n<div class=\"clear\"><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/255499\/\"> http:\/\/habrahabr.ru\/post\/255499\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>     \t<habracut\/><br \/>  \u0412\u0441\u0435\u043c \u043f\u0440\u0438\u0432\u0435\u0442. <\/p>\n<p>  \u0412\u0441\u0435 \u0437\u043d\u0430\u044e\u0442 \u0447\u0442\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u043e\u0431\u044b\u0447\u043d\u043e \u043d\u0435 \u0434\u0435\u0448\u0435\u0432\u044b\u0435. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u043f\u0443\u043b\u044e \u0438 \u0443\u043d\u0438\u0447\u0442\u043e\u0436\u0430\u0442\u044c, \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043d\u0430\u043a\u043b\u0430\u0434\u043d\u043e \u0434\u043b\u044f \u0442\u0435\u0445 \u0436\u0435 \u043c\u043e\u0431\u0438\u043b\u044c\u043d\u044b\u0445 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432. \u041c\u043e\u0436\u0435\u0442 \u0441\u0442\u043e\u0438\u0442 \u043d\u0435 \u0443\u043d\u0438\u0447\u0442\u043e\u0436\u0430\u0442\u044c \u043f\u0443\u043b\u044e, \u0430 \u0441\u043a\u0440\u044b\u0432\u0430\u0442\u044c \u0435\u0435. \u0412\u043e\u0442 \u0440\u0435\u0448\u0438\u043b \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u0441\u044f \u0441\u0432\u043e\u0435\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0435\u0439 Pool Manager. \u041a\u043e\u0442\u043e\u0440\u044b\u0439 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e \u0432 \u0440\u0430\u0437\u043d\u044b\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445, \u0432 \u0442\u043e\u043c \u0447\u0438\u0441\u043b\u0435 \u0438 \u043d\u0430 photon server.<\/p>\n<h4>\u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b<\/h4>\n<p>  \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430, \u043d\u0443\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441.  <\/p>\n<pre><code class=\"cs\">public interface IPoolObject&lt;T&gt; {     T Group { get; }     void Create();     void OnPush();     void FailedPush();    } <\/code><\/pre>\n<p>  \u0413\u0434\u0435 \u043c\u0435\u0442\u043e\u0434 Create() \u0431\u0443\u0434\u0435\u0442 \u0438\u0433\u0440\u0430\u0442\u044c \u0440\u043e\u043b\u044c \u043f\u0441\u0435\u0432\u0434\u043e-\u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430. \u0412\u0435\u0434\u044c \u043a\u043e\u0433\u0434\u0430 \u0432\u044b \u0434\u043e\u0441\u0442\u0430\u043d\u0435\u0442\u0435 \u043e\u0431\u044a\u0435\u043a\u0442 \u0438\u0437 \u043f\u0443\u043b\u0430, \u0435\u0433\u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0431\u0443\u0434\u0435\u0442 \u043d\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043e, \u0447\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u043f\u0430\u0433\u0443\u0431\u043d\u043e \u043e\u0442\u0440\u0430\u0437\u0438\u0442\u0441\u044f \u043d\u0430 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u043c \u0435\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438.<\/p>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u0441\u0430\u043c Pool Manager  <\/p>\n<pre><code class=\"cs\">using System.Collections.Generic; using System;  public class PoolManager&lt;K, V&gt; where V :IPoolObject&lt;K&gt; {     public virtual int MaxInstances { get; protected set; }     public virtual int InctanceCount { get { return objects.Count; } }     public virtual int CacheCount { get { return cache.Count; } }      public delegate bool Compare&lt;T&gt;(T value) where T: V;      protected Dictionary&lt;K, List&lt;V&gt;&gt; objects;     protected Dictionary&lt;Type, List&lt;V&gt;&gt; cache;      public PoolManager(int maxInstance)      {         MaxInstances = maxInstance;         objects = new Dictionary&lt;K, List&lt;V&gt;&gt;();         cache = new Dictionary&lt;Type, List&lt;V&gt;&gt;();     }      public virtual bool CanPush()     {         return InctanceCount + 1 &lt; MaxInstances;     }      public virtual bool Push(K groupKey, V value)     {         bool result = false;         if (CanPush())         {             value.OnPush();             if (!objects.ContainsKey(groupKey))             {                 objects.Add(groupKey, new List&lt;V&gt;());             }             objects[groupKey].Add(value);             Type type = value.GetType();             if (!cache.ContainsKey(type))             {                 cache.Add(type, new List&lt;V&gt;());             }             cache[type].Add(value);         }         else         {             value.FailedPush();         }         return result;     }      public virtual T Pop&lt;T&gt;(K groupKey) where T : V     {         T result = default(T);         if (Contains(groupKey) &#038;& objects[groupKey].Count &gt; 0)         {              for (int i = 0; i &lt; objects[groupKey].Count; i++)             {                 if (objects[groupKey][i] is T)                 {                     result = (T)objects[groupKey][i];                     Type type = result.GetType();                     RemoveObject(groupKey, i);                     RemoveFromCache(result, type);                     result.Create();                     break;                 }             }         }         return result;     }      public virtual T Pop&lt;T&gt;() where T: V     {         T result = default(T);         Type type = typeof(T);         if (ValidateForPop(type))         {             for (int i = 0; i &lt; cache[type].Count; i++)             {                 result = (T)cache[type][i];                 if (result != null &#038;& objects.ContainsKey(result.Group))                 {                     objects[result.Group].Remove(result);                     RemoveFromCache(result, type);                     result.Create();                     break;                 }                              }         }         return result;     }      public virtual T Pop&lt;T&gt;(Compare&lt;T&gt; comparer) where T : V     {         T result = default(T);         Type type = typeof(T);         if (ValidateForPop(type))         {             for(int i = 0; i &lt; cache[type].Count; i++)              {                 T value = (T)cache[type][i];                 if (comparer(value))                 {                     objects[value.Group].Remove(value);                     RemoveFromCache(result, type);                     result = value;                     result.Create();                     break;                 }                            }         }         return result;     }         public virtual bool Contains(K groupKey)     {         return objects.ContainsKey(groupKey);     }      public virtual void Clear()     {         objects.Clear();     }      protected virtual bool ValidateForPop(Type type)     {         return cache.ContainsKey(type) &#038;& cache[type].Count &gt; 0;     }      protected virtual void RemoveObject(K groupKey, int idx)     {         if (idx &gt;= 0 &#038;& idx &lt; objects[groupKey].Count)         {             objects[groupKey].RemoveAt(idx);             if (objects[groupKey].Count == 0)             {                 objects.Remove(groupKey);             }         }     }      protected void RemoveFromCache(V value, Type type)     {         if (cache.ContainsKey(type))         {             cache[type].Remove(value);             if (cache[type].Count == 0)             {                 cache.Remove(type);             }         }     } } <\/code><\/pre>\n<p>  \u041d\u0430 \u0447\u0442\u043e \u0441\u0442\u043e\u0438\u0442 \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u044c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435. <br \/>  MaxInstances \u2014 \u043f\u043e\u043b\u0435 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 pool \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432. \u0412 \u0441\u043b\u0443\u0447\u0430\u0435, \u0435\u0441\u043b\u0438 \u043d\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u043f\u0443\u043b \u043e\u0447\u0435\u0440\u0435\u0434\u043d\u043e\u0439 \u043e\u0431\u044a\u0435\u043a\u0442, \u0441\u0430\u043c \u043e\u0431\u044a\u0435\u043a\u0442 \u0432\u044b\u0432\u043e\u0437\u0438\u0442 \u043c\u0435\u0442\u043e\u0434 FailedPush(); <br \/>  OnPush() \u2014 \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043f\u0435\u0440\u0435\u0434 \u043f\u043e\u043f\u0430\u0434\u0430\u043d\u0438\u0435\u043c \u0432 \u043f\u0443\u043b. <br \/>  Create() \u2014 \u043f\u0435\u0440\u0435\u0434 \u0442\u0435\u043c, \u043a\u0430\u043a \u043f\u0443\u043b \u0432\u0435\u0440\u043d\u0435\u0442 \u043d\u0430\u043c \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440.<\/p>\n<p>  \u041e\u0431\u0449\u0438\u0439 Pool \u0433\u043e\u0442\u043e\u0432. \u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u0430\u0440\u0438\u0430\u0446\u0438\u044e \u0434\u043b\u044f Unity3d. \u041f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u043c <\/p>\n<pre><code class=\"cs\">using UnityEngine; using System.Collections;  public class UnityPoolManager : MonoBehaviour {     public static UnityPoolManager Instance {get; protected set;}      public int maxInstanceCount = 128;      protected PoolManager&lt;string, UnityPoolObject&gt; poolManager;       protected virtual void Awake()     {         Instance = this;         poolManager = new PoolManager&lt;string, UnityPoolObject&gt;(maxInstanceCount);     }       public virtual bool CanPush()     {         return poolManager.CanPush();     }      public virtual bool Push(string groupKey, UnityPoolObject poolObject)     {         return poolManager.Push(groupKey, poolObject);     }      public virtual T PopOrCreate&lt;T&gt;(T prefab) where T : UnityPoolObject     {         return PopOrCreate(prefab, Vector3.zero, Quaternion.identity);     }      public virtual T PopOrCreate&lt;T&gt;(T prefab, Vector3 position, Quaternion rotation) where T : UnityPoolObject     {         T result = poolManager.Pop&lt;T&gt;(prefab.Group);         if (result == null)         {             result = CreateObject&lt;T&gt;(prefab, position, rotation);         }         else         {             result.SetTransform(position, rotation);         }         return result;     }      public virtual UnityPoolObject Pop(string groupKey)     {         return poolManager.Pop&lt;UnityPoolObject&gt;(groupKey);     }      public virtual T Pop&lt;T&gt;() where T : UnityPoolObject     {         return poolManager.Pop&lt;T&gt;();     }      public virtual T Pop&lt;T&gt;(PoolManager&lt;string, UnityPoolObject&gt;.Compare&lt;T&gt; comparer) where T : UnityPoolObject     {         return poolManager.Pop&lt;T&gt;(comparer);     }      public virtual T Pop&lt;T&gt;(string groupKey) where T : UnityPoolObject     {         return poolManager.Pop&lt;T&gt;(groupKey);     }      public virtual bool Contains(string groupKey)     {         return poolManager.Contains(groupKey);     }      public virtual void Clear()     {         poolManager.Clear();     }      protected virtual T CreateObject&lt;T&gt;(T prefab, Vector3 position, Quaternion rotation) where T : UnityPoolObject     {         GameObject go = Instantiate(prefab.gameObject, position, rotation) as GameObject;         T result = go.GetComponent&lt;T&gt;();         result.name = prefab.name;         return result;     } } <\/code><\/pre>\n<p>  \u041f\u043e \u0441\u0443\u0442\u0438 \u044d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u043e\u0431\u0432\u0435\u0440\u0442\u043a\u0430 \u043d\u0430\u0434 \u043f\u0435\u0440\u0432\u044b\u043c \u043f\u0443\u043b\u043e\u043c \u0438 \u0421\u0438\u043d\u0433\u0435\u043b\u0442\u043e\u043d. <br \/>  PopOrCreate() \u2014 \u043d\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u0432\u043e\u0442 \u044d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432.<br \/>  Push() \u2014 \u0443 \u0441\u0430\u043c\u0438\u0445 \u043f\u0443\u043b \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0438\u043b\u0438 Push \u0432 \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440\u0435.<\/p>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u0441\u0430\u043c GameObject<\/p>\n<pre><code class=\"cs\">using UnityEngine; using System.Collections;  public class UnityPoolObject : MonoBehaviour, IPoolObject&lt;string&gt; {     public virtual string Group { get {return name;} }     public Transform MyTransform { get { return myTransform; } }      protected Transform myTransform;      protected virtual void Awake()     {         myTransform = transform;     }      public virtual void SetTransform(Vector3 position, Quaternion rotation)     {         myTransform.position = position;         myTransform.rotation = rotation;     }      public virtual void Create()     {         gameObject.SetActive(true);     }      public virtual void OnPush()     {         gameObject.SetActive(false);     }      public virtual void Push()     {         UnityPoolManager.Instance.Push(Group, this);     }      public void FailedPush()     {         Debug.Log(&quot;FailedPush&quot;); \/\/ !!!         Destroy(gameObject);     } }  <\/code><\/pre>\n<p>  \u0412\u0441\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0431\u0443\u0434\u0435\u043c \u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u044c \u043e\u0442 \u043d\u0435\u0433\u043e. <br \/>  FailedPush() \u2014 \u0441\u0442\u043e\u0438\u0442 \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u044c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u044d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434. \u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0432\u044b \u0437\u0430\u0445\u043e\u0442\u0438\u0442\u0435 \u0431\u0440\u043e\u0441\u0430\u0442\u044c \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435, \u043c\u043e\u043b \u043f\u0443\u043b \u0437\u0430\u0431\u0438\u0442 \u0438\u043b\u0438 \u0447\u0442\u043e-\u0442\u043e \u0435\u0449\u0435.<\/p>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u043a \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044e. \u041d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043f\u0443\u043b\u0438 \u0441\u043b\u0435\u0434\u043e\u0432 \u0438 UI List item.<\/p>\n<pre><code class=\"cs\">public class Bullet : UnityPoolObject  { ... } \/\/ \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435  Bullet bullet = UnityPoolManager.Instance.PopOrCreate&lt;Bullet&gt;(bulletPrefab, bulletPoint.position, Quaternion.identity);   bullet.Execute(sender, bulletPoint.position, CalculateTarget(target, accuracy01), damage, blockTime, range, bulletSpeed); \/\/ \u0443\u043d\u0438\u0447\u0442\u043e\u0436\u0435\u043d\u0438\u0435, \u0442\u043e\u0447\u043d\u0435\u0439 \u0432\u043e\u0437\u0440\u0430\u0449\u0430\u0435\u043c \u0432 \u043f\u0443\u043b timer-= Time.deltaTime;  if (timer&lt; 0)             {                 Push();             } <\/code><\/pre>\n<pre><code class=\"cs\">public class StepObject : UnityPoolObject  { ... }  \/\/\/ ---  StepObject stepObject = UnityPoolManager.Instance.PopOrCreate&lt;StepObject&gt;(prefab, sender.position, sender.rotation);             FXManager.Instance.InitDecal(null, stepObject.gameObject, hit, direction);             stepObject.MyTransform.rotation *= rotation;             StartCoroutine(ApplyDecalC(stepObject)); \/\/\/ --- protected virtual IEnumerator ApplyDecalC(StepObject decalObject)     {         yield return new WaitForSeconds(waitTime);         yield return StartCoroutine(FXManager.Instance.HideOjectC(decalObject.gameObject, hideTime));         decalObject.Push();     } <\/code><\/pre>\n<pre><code class=\"cs\">public<\/code><\/pre>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-254992","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/254992","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=254992"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/254992\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=254992"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=254992"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=254992"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}