{"id":301220,"date":"2020-04-03T09:00:41","date_gmt":"2020-04-03T09:00:41","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=301220"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=301220","title":{"rendered":"Singelton c \u0443\u0447\u0435\u0442\u043e\u043c \u043f\u043e\u0434\u0432\u043e\u0434\u043d\u044b\u0445 \u043a\u0430\u043c\u043d\u0435\u0439"},"content":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/495432\/\">\n<h1 id=\"vvedenie\">\u0412\u0432\u0435\u0434\u0435\u043d\u0438\u0435<\/h1>\n<p>  <\/p>\n<p>\u0421\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043e\u0433\u0440\u043e\u043c\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u043e\u0432 \u0438 \u0430\u043d\u0442\u0438\u043f\u0430\u0442\u0442\u0435\u0440\u043d\u043e\u0432 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f. \u0417\u0430\u0447\u0430\u0441\u0442\u0443\u044e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u043e\u0432 \u0434\u0438\u043a\u0442\u0443\u0435\u0442 \u043d\u0430\u043c \u043e\u043f\u044b\u0442 \u0438 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0437\u043d\u0430\u043d\u0438\u044f \u0438\u0445 \u0441\u0430\u043c\u0438\u0445. \u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u0445\u043e\u0447\u0443 \u043e\u0431\u0441\u0443\u0434\u0438\u0442\u044c \u0441 \u0432\u0430\u043c\u0438 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u0430 <strong><em>Singelton<\/em><\/strong>, \u0430 \u0438\u043c\u0435\u043d\u043d\u043e \u0435\u0433\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0432 Net \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043a Unity.<\/p>\n<p><a name=\"habracut\"><\/a>   <\/p>\n<h1 id=\"singelton\">Singelton<\/h1>\n<p>  <\/p>\n<p>\u041e\u0442\u043c\u0435\u0447\u0443, \u0447\u0442\u043e \u0437\u0430\u043d\u0438\u043c\u0430\u044e \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0435\u043c \u043a\u043e\u0434\u0430 \u0432 \u043a\u043e\u043c\u0430\u043d\u0434\u0435, \u043f\u043e\u044d\u0442\u043e\u043c\u0443, \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0431\u043e\u043b\u044c\u0448\u0435 \u0440\u0430\u0431\u043e\u0442\u044b \u0443\u0432\u043e\u0436\u0443 \u0432\u043d\u0443\u0442\u0440\u044c \u043a\u043e\u0434\u0430, \u0447\u0442\u043e\u0431\u044b \u0440\u0430\u0437\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u043a\u043e\u043c\u0430\u043d\u0434\u0443 \u0438 \u0443\u0441\u0442\u0440\u0430\u043d\u0438\u0442\u044c \u043e\u0442 \u043d\u0435\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c \u0437\u0430\u0434\u0443\u043c\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u043e \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044f\u0445 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0442\u0435\u0445 \u0438\u043b\u0438 \u0438\u043d\u044b\u0445 \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u043e\u0432 \u0432 Unity.<\/p>\n<p>  <\/p>\n<p>\u0418\u0437\u0443\u0447\u0430\u044f \u043b\u0438\u0442\u0435\u0440\u0430\u0442\u0443\u0440\u0443 \u043f\u043e Net<sup><a href=\"#tag1\">1<\/a><\/sup>, \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043a \u0434\u0430\u043d\u043d\u043e\u043c\u0443 \u0432\u043e\u043f\u0440\u043e\u0441\u0443<sup><a href=\"#tag2\">2<\/a><\/sup> \u0438 \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u0440\u0430\u0431\u043e\u0442\u044b \u043d\u0430\u0434 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u043c\u0438 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u043c\u0438, \u0440\u043e\u0434\u0438\u043b\u0441\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043a\u043b\u0430\u0441\u0441:<\/p>\n<p>  <\/p>\n<pre><code class=\"cs\">using UnityEngine;  \/\/\/ &lt;summary&gt; \/\/\/ \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u0438\u043d\u0433\u043b\u0442\u043e\u043d\u0430 \u0434\u043b\u044f \u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u044f. \/\/\/ &lt;\/summary&gt; \/\/\/ &lt;typeparam name=&quot;T&quot;&gt;\u041a\u043b\u0430\u0441\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0441\u0438\u043d\u0433\u043b\u0442\u043e\u043d\u043e\u043c&lt;\/typeparam&gt; \/\/\/ &lt;remarks&gt; \/\/\/ \u0415\u0441\u043b\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043e\u0431\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u043a \u043a\u043b\u0430\u0441\u0441\u0443 \u0432\u043e \u0432\u0440\u0435\u043c\u044f OnDestroy \u0438\u043b\u0438 OnApplicationQuit \/\/\/ \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0447\u0435\u0440\u0435\u0437 IsAlive. \u041e\u0431\u044a\u0435\u043a\u0442 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0443\u0436\u0435  \/\/\/ \u0443\u043d\u0438\u0447\u0442\u043e\u0436\u0435\u043d, \u0438 \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043a \u043d\u0435\u043c\u0443 \u0432\u044b\u0437\u043e\u0432\u0435\u0442 \u0435\u0433\u043e \u0435\u0449\u0435 \u0440\u0430\u0437. \/\/\/  \/\/\/  \/\/\/ \u041f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u0432 \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u043c \u043a\u043b\u0430\u0441\u0441\u0435 Awake, OnDestroy,  \/\/\/ OnApplicationQuit \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0431\u0430\u0437\u043e\u0432\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \/\/\/ base.Awake() \u0438 \u0442\u0434. \/\/\/  \/\/\/ \u0414\u043e\u0431\u0430\u0432\u0438\u043b \u0441\u043a\u0440\u044b\u0432\u0430\u0435\u043c\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 Initialization - \u0447\u0442\u043e\u0431\u044b \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0436\u0430\u0442\u044c \u0435\u0433\u043e \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c  \/\/\/ \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f. \/\/\/  \/\/\/ \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 unity, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0443  \/\/\/ \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u043d\u0435\u0442 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438. \u041e\u0434\u043d\u0430\u043a\u043e \u0435\u0435 \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c, \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0435\u0441\u043b\u0438  \/\/\/ \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043a \u043e\u0431\u044a\u0435\u043a\u0442\u0443 \u0438\u0437 \u0434\u0440\u0443\u0433\u0438\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432. \/\/\/  \/\/\/ \u0418\u0437 \u043a\u043d\u0438\u0433: \/\/\/     - \u0420\u0438\u0445\u0442\u0435\u0440 &quot;CLR via C#&quot; \/\/\/     - Chris Dickinson &quot;Unity 2017 Game optimization&quot; \/\/\/&lt;\/remarks&gt;  public class Singelton&lt;T&gt; : MonoBehaviour where T : Singelton&lt;T&gt; {      private static T instance = null;      private bool alive = true;      public static T Instance     {         get         {             if (instance != null)             {                 return instance;             }             else             {                 \/\/Find T                 T[] managers = GameObject.FindObjectsOfType&lt;T&gt;();                 if (managers != null)                 {                     if (managers.Length == 1)                     {                         instance = managers[0];                         DontDestroyOnLoad(instance);                         return instance;                     }                     else                     {                         if (managers.Length &gt; 1)                         {                             Debug.LogError($&quot;Have more that one {typeof(T).Name} in scene. &quot; +                                             &quot;But this is Singelton! Check project.&quot;);                             for (int i = 0; i &lt; managers.Length; ++i)                             {                                 T manager = managers[i];                                 Destroy(manager.gameObject);                             }                         }                     }                 }                 \/\/create                  GameObject go = new GameObject(typeof(T).Name, typeof(T));                 instance = go.GetComponent&lt;T&gt;();         instance.Initialization();                 DontDestroyOnLoad(instance.gameObject);                 return instance;             }         }          \/\/Can be initialized externally         set         {             instance = value as T;         }     }      \/\/\/ &lt;summary&gt;     \/\/\/ Check flag if need work from OnDestroy or OnApplicationExit     \/\/\/ &lt;\/summary&gt;     public static bool IsAlive     {         get         {             if (instance == null)                 return false;             return instance.alive;         }     }      protected void Awake()     {         if (instance == null)         {             DontDestroyOnLoad(gameObject);             instance = this as T;             Initialization();         }         else         {             Debug.LogError($&quot;Have more that one {typeof(T).Name} in scene. &quot; +                             &quot;But this is Singelton! Check project.&quot;);             DestroyImmediate(this);         }     }      protected void OnDestroy() { alive = false; }      protected void OnApplicationQuit() { alive = false; }      protected virtual void Initialization() { } } <\/code><\/pre>\n<p>  <\/p>\n<p>\u0421\u043e\u0441\u0440\u0435\u0434\u043e\u0442\u043e\u0447\u0443 \u0441\u0432\u043e\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u0430\u0441\u043f\u0435\u043a\u0442\u0430\u0445.<\/p>\n<p>  <\/p>\n<h2 id=\"sozdanie-obekta\">\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u0430<\/h2>\n<p>  <\/p>\n<p>\u041f\u0440\u0438 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0435\u043a\u0442\u0430, \u0430 \u0442\u0435\u043c \u0431\u043e\u043b\u0435\u0435 \u0440\u0430\u0431\u043e\u0442\u044b \u0432 \u043a\u043e\u043c\u0430\u043d\u0434\u0435 &gt; 3 \u0447\u0435\u043b\u043e\u0432\u0435\u043a \u0437\u0430\u0447\u0430\u0441\u0442\u0443\u044e \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044f, \u043a\u043e\u0433\u0434\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u043d\u0435\u044f\u0441\u043d\u0430. \u0421\u0442\u0440\u043e\u0433\u043e \u0433\u043e\u0432\u043e\u0440\u044f<sup><a href=\"#tag3\">3<\/a><\/sup>, \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0432\u044b\u0437\u043e\u0432\u043e\u0432 <strong>Awake()<\/strong> \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u0430 (\u043a\u043e\u043d\u0435\u0447\u043d\u043e \u044d\u0442\u043e \u043d\u0435 \u0441\u043e\u0432\u0441\u0435\u043c \u0442\u0430\u043a, \u0438 \u043d\u0430 \u044d\u0442\u043e\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043c\u043e\u0436\u043d\u043e \u0432\u043b\u0438\u044f\u0442\u044c, \u043d\u043e \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f \u0441\u0432\u044f\u0442\u0430), \u0432\u0432\u0438\u0434\u0443 \u0447\u0435\u0433\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0443\u0441\u0442\u0440\u0430\u043d\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u0434\u043e\u0441\u0430\u0434\u043d\u044b\u0439 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u043a \u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u043e\u043c \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430 <strong>Instance{get;}<\/strong>. \u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u043f\u043e\u043b\u043d\u043e\u0446\u0435\u043d\u043d\u044b\u0439 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0441\u0438\u043d\u0433\u043b\u0442\u043e\u043d\u0443 \u0438\u0437 <strong>Awake()<\/strong> \u0434\u0440\u0443\u0433\u0438\u0445 \u043a\u043b\u0430\u0441\u0441\u043e\u0432.<\/p>\n<p>  <\/p>\n<p>\u0412 \u0442\u043e\u0436\u0435 \u0432\u0440\u0435\u043c\u044f \u044d\u0442\u043e\u0442 \u0436\u0435 \u0444\u0430\u043a\u0442, \u043d\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0430\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u044f\u0432\u043d\u043e <strong>Lazy, \u0442\u0430\u043a \u043a\u0430\u043a \u043e\u0431\u044a\u0435\u043a\u0442 \u0431\u0443\u0434\u0435\u0442 \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0432\u044b\u0437\u0432\u0430\u043d \u0447\u0435\u0440\u0435\u0437 <strong>Awake()<\/strong> \u0432 \u043b\u044e\u0431\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435.<\/strong>  <\/p>\n<p>\u041e\u0441\u043d\u043e\u0432\u044b\u0432\u0430\u044f\u0441\u044c \u043d\u0430 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0438 \u0438\u0437 \u0431\u0430\u043d\u0434\u044b 4-\u0445<sup><a href=\"#tag4\">4<\/a><\/sup>, \u0434\u0430\u043d\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u0442 \u0441\u0432\u043e\u0435 \u0435\u0434\u0438\u043d\u043e\u043b\u0438\u0447\u043d\u043e\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043e\u0432\u0430\u043d\u0438\u0435, \u0432\u0432\u0438\u0434\u0443 \u044d\u0442\u043e\u0433\u043e \u043e\u0431\u044a\u044f\u0441\u043d\u044f\u0435\u0442\u0441\u044f \u0442\u0430\u043a\u0430\u044f \u043b\u043e\u0433\u0438\u043a\u0430 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430 <strong>Instance{get;}<\/strong>.<\/p>\n<p>  <\/p>\n<h2 id=\"inicializaciya-obekta\">\u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u0430<\/h2>\n<p>  <\/p>\n<p>\u041e\u0431\u0449\u0435\u0435 \u043f\u0440\u0430\u0432\u0438\u043b\u043e \u0434\u043b\u044f Unity \u2014 \u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e \u0432 <strong>Awake()<\/strong>. \u041e\u0434\u043d\u0430\u043a\u043e, \u0447\u0430\u0441\u0442\u043e \u0437\u0430\u0431\u044b\u0432\u0430\u044e\u0442 \u0432\u044b\u0437\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434\u044b \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0440\u0435\u0434\u0443\u0441\u043c\u043e\u0442\u0440\u0435\u043d \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 <strong>Initialization()<\/strong>. \u042d\u0442\u043e \u0443\u043f\u0440\u043e\u0449\u0430\u0435\u0442 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u043f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0438 \u043e\u0434\u043d\u043e\u0437\u043d\u0430\u0447\u043d\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u0442 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u0434\u043b\u044f \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 (<em>KISS<\/em>).<\/p>\n<p>  <\/p>\n<h2 id=\"vneshnyaya-inicializaciya\">\u0412\u043d\u0435\u0448\u043d\u044f\u044f \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/h2>\n<p>  <\/p>\n<p>\u041f\u0440\u0435\u0434\u0443\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u0430 \u043f\u043e \u0440\u044f\u0434\u0443 \u043f\u0440\u0438\u0447\u0438\u043d, \u0432 \u0442\u043e\u043c \u0447\u0438\u0441\u043b\u0435 \u0434\u043b\u044f \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0447\u0435\u0440\u0435\u0437 <em>DI<\/em> \u0438 <em>SD<\/em>. \u041f\u043e\u0434\u0440\u043e\u0431\u043d\u043e\u0435 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 \u0438 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u0432\u044b\u0448\u0435 \u043a\u043b\u0430\u0441\u0441\u0430. \u0414\u0430\u043d\u043d\u044b\u0439 \u0437\u0430\u0434\u0435\u043b \u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d \u043d\u0430 \u0431\u0443\u0434\u0443\u0449\u0435\u0435.<\/p>\n<p>  <\/p>\n<h2 id=\"unichtozhenie-obekta\">\u0423\u043d\u0438\u0447\u0442\u043e\u0436\u0435\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u0430<\/h2>\n<p>  <\/p>\n<p>\u041a\u0430\u043a \u043f\u0440\u0430\u0432\u0438\u043b\u043e, \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u043e\u0431\u044b\u0447\u043d\u044b\u0445 \u0441\u0438\u043d\u0433\u043b\u0442\u043e\u043d\u043e\u0432, \u0438 \u0432\u044b\u0437\u043e\u0432\u0430 \u0438\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u0438\u0437 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 <strong>OnDestroy()<\/strong>, <strong>OnApplicationQuit()<\/strong> \u0434\u0440\u0443\u0433\u0438\u0445 \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043e\u0448\u0438\u0431\u043a\u0443 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0432\u0438\u0434\u0430<sup><a href=\"#tag5\">5<\/a><\/sup>:<\/p>\n<p>  <\/p>\n<blockquote><p>Did you spawn new GameObjects from OnDestroy?<\/p><\/blockquote>\n<p>\u041a\u0430\u043a \u043f\u0440\u0430\u0432\u0438\u043b\u043e, \u044f \u0441\u0447\u0438\u0442\u0430\u044e \u043a\u043e\u0434, \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0439 \u0432 \u0442\u0430\u043a\u043e\u043c \u0441\u0442\u0438\u043b\u0435 \u043e\u0448\u0438\u0431\u043e\u0447\u043d\u044b\u043c, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0440\u043e\u0449\u0443 \u0435\u0433\u043e \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u0430\u0442\u044c. \u041d\u043e \u0432 \u0441\u043b\u0443\u0447\u0430\u0435, \u0435\u0441\u043b\u0438 \u0442\u0430\u043a \u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u0440\u0435\u0434\u0443\u0441\u043c\u043e\u0442\u0440\u0435\u043d \u043c\u0435\u0442\u043e\u0434 <strong>IsAlive()<\/strong>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c, \u043f\u0435\u0440\u0435\u0434 \u0432\u044b\u0437\u043e\u0432\u043e\u043c \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u0441\u0438\u043d\u0433\u043b\u0442\u043e\u043d\u0430. \u041c\u0435\u0442\u043e\u0434 \u043d\u0435 \u043b\u0443\u0447\u0448\u0438\u0439, \u043d\u043e \u043d\u0430 \u0431\u0435\u0437\u0440\u044b\u0431\u044c\u0435&#8230;<\/p>\n<p>  <\/p>\n<h1 id=\"zaklyuchenie\">\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h1>\n<p>  <\/p>\n<p>\u0412\u0441\u0435 \u0447\u0430\u0449\u0435 \u044f \u043f\u0440\u0438\u0445\u043e\u0436\u0443 \u043a \u043c\u043d\u0435\u043d\u0438\u044e, \u0447\u0442\u043e \u043f\u043e\u043b\u044c\u0437\u0443\u044f\u0441\u044c \u043f\u0430\u0440\u0430\u0434\u0438\u0433\u043c\u043e\u0439 Unity \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0441\u0432\u043e\u0438 \u043f\u0440\u043e\u0435\u043a\u0442\u044b \u0431\u0435\u0437 Singleton. \u0417\u0430\u0447\u0430\u0441\u0442\u0443\u044e \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u044d\u0442\u043e\u0433\u043e \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u0430 \u0434\u0435\u043b\u0430\u0435\u0442 \u0432\u0430\u0448 \u043a\u043e\u0434 \u0441\u0438\u043b\u044c\u043d\u043e-\u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u043c \u0438 \u043a\u0440\u0430\u0439\u043d\u0435 \u0445\u0440\u0443\u043f\u043a\u0438\u043c.<\/p>\n<p>  <\/p>\n<p>\u0421\u043f\u0430\u0441\u0438\u0431\u043e.<\/p>\n<p>  <\/p>\n<h1 id=\"istochniki\">\u0418\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u0438<\/h1>\n<p>  <\/p>\n<p> \u2014 \u0420\u0438\u0445\u0442\u0435\u0440 \u0414\u0436 &quot;CLR via C#. \u041f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043d\u0430 \u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0435 Microsoft.NET# Framework 4.5 \u043d\u0430 \u044f\u0437\u044b\u043a\u0435 C#&quot;, 2013<\/p>\n<p>  <\/p>\n<p> \u2014 <a href=\"https:\/\/www.codingame.com\/playgrounds\/1979\/different-ways-to-implement-singleton-in--net-and-make-people-hate-you-along-the-way\">https:\/\/www.codingame.com\/playgrounds\/1979\/different-ways-to-implement-singleton-in&#8212;net-and-make-people-hate-you-along-the-way<\/a><\/p>\n<p>  <\/p>\n<p> \u2014 <a href=\"https:\/\/docs.unity3d.com\/ru\/current\/ScriptReference\/MonoBehaviour.Awake.html\">https:\/\/docs.unity3d.com\/ru\/current\/ScriptReference\/MonoBehaviour.Awake.html<\/a><\/p>\n<p>  <\/p>\n<p> \u2014 <a href=\"https:\/\/ru.wikipedia.org\/wiki\/Design_Patterns\">https:\/\/ru.wikipedia.org\/wiki\/Design_Patterns<\/a><\/p>\n<p>  <\/p>\n<p> \u2014 Dickinson Chris &quot;Unity 2017 Game Optimization, Second Edition&quot;, 2017<\/p>\n<p><cut text=\"\u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435\"\/><\/p>\n<\/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=\"https:\/\/habr.com\/ru\/post\/495432\/\"> https:\/\/habr.com\/ru\/post\/495432\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/495432\/\">\n<h1 id=\"vvedenie\">\u0412\u0432\u0435\u0434\u0435\u043d\u0438\u0435<\/h1>\n<p>  <\/p>\n<p>\u0421\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043e\u0433\u0440\u043e\u043c\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u043e\u0432 \u0438 \u0430\u043d\u0442\u0438\u043f\u0430\u0442\u0442\u0435\u0440\u043d\u043e\u0432 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f. \u0417\u0430\u0447\u0430\u0441\u0442\u0443\u044e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u043e\u0432 \u0434\u0438\u043a\u0442\u0443\u0435\u0442 \u043d\u0430\u043c \u043e\u043f\u044b\u0442 \u0438 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0437\u043d\u0430\u043d\u0438\u044f \u0438\u0445 \u0441\u0430\u043c\u0438\u0445. \u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u0445\u043e\u0447\u0443 \u043e\u0431\u0441\u0443\u0434\u0438\u0442\u044c \u0441 \u0432\u0430\u043c\u0438 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u0430 <strong><em>Singelton<\/em><\/strong>, \u0430 \u0438\u043c\u0435\u043d\u043d\u043e \u0435\u0433\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0432 Net \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043a Unity.<\/p>\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-301220","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/301220","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=301220"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/301220\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=301220"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=301220"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=301220"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}