{"id":301050,"date":"2020-03-31T15:00:54","date_gmt":"2020-03-31T15:00:54","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=301050"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=301050","title":{"rendered":"Unity3D: \u0410\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0430\u0433\u0440\u0435\u0433\u0430\u0442\u043e\u0440 \u0441\u043a\u0440\u0438\u043f\u0442\u043e\u0432-\u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440\u043e\u0432"},"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\/494930\/\">\n<h3>\u0412\u0441\u0442\u0443\u043f\u043b\u0435\u043d\u0438\u0435<\/h3>\n<p>  <i>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0440\u0435\u0447\u044c \u043f\u043e\u0439\u0434\u0435\u0442 \u043e\u0431 \u043e\u0434\u043d\u043e\u043c \u0432\u0438\u0434\u0435 \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u043c\u0435\u0436\u0434\u0443 \u0441\u043a\u0440\u0438\u043f\u0442\u0430\u043c\u0438-\u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440\u0430\u043c\u0438 (\u0441\u0438\u043d\u0433\u043b\u0442\u043e\u043d\u0430\u043c\u0438 \u0438\u043c\u0435\u043d\u0443\u0435\u043c\u044b\u043c\u0438), \u0430 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e \u2014 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430-\u0430\u0433\u0440\u0435\u0433\u0430\u0442\u043e\u0440\u0430, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c\u0441\u044f \u0441\u0441\u044b\u043b\u043a\u0438 \u043d\u0430 \u0432\u0441\u0435 instance \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440\u043e\u0432. \u0418\u0434\u0435\u044f \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043a\u043b\u0430\u0441\u0441-\u0430\u0433\u0440\u0435\u0433\u0430\u0442\u043e\u0440 \u043f\u0440\u0438\u0448\u043b\u0430 \u043c\u043d\u0435 \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0447\u0442\u0435\u043d\u0438\u044f \u044d\u0442\u043e\u0439 <a href=\"https:\/\/habr.com\/ru\/post\/212055\/\">\u0441\u0442\u0430\u0442\u044c\u0438<\/a>.<\/i><\/p>\n<h3>\u0417\u0430\u0434\u0430\u0447\u0438<\/h3>\n<p>  \u042f \u043f\u0440\u0438\u0448\u0435\u043b \u043a \u0432\u044b\u0432\u043e\u0434\u0443, \u0447\u0442\u043e \u0437\u0430\u0431\u0438\u0432\u0430\u0442\u044c \u0441\u0432\u043e\u0438\u043c\u0438 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u043c\u0438 \u0440\u0443\u0447\u043a\u0430\u043c\u0438 \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440\u044b \u0432 \u043a\u043b\u0430\u0441\u0441-\u0430\u0433\u0440\u0435\u0433\u0430\u0442\u043e\u0440 \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u2014 \u044d\u0442\u043e \u0434\u0438\u043a\u043e \u043d\u0435\u0443\u0434\u043e\u0431\u043d\u043e. \u041f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u043d\u043e\u0432\u043e\u0433\u043e \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440\u0430 \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u043e\u0442\u043a\u0440\u044b\u0432\u0430\u0442\u044c \u043a\u043b\u0430\u0441\u0441-\u0430\u0433\u0440\u0435\u0433\u0430\u0442\u043e\u0440 \u0438 \u0432\u043d\u043e\u0441\u0438\u0442\u044c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f. \u041f\u0440\u0438 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0435 \u043e\u043f\u044b\u0442\u0430 \u043c\u043e\u0436\u043d\u043e \u0442\u0430\u043a\u0438\u0435 \u043d\u0443\u0434\u043d\u044b\u0435 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u044b \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043f\u043e\u0441\u0442\u0430\u0432\u0438\u043b\u0438\u0441\u044c \u0437\u0430\u0434\u0430\u0447\u0438: <b>\u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u0438\u043d\u0433\u043b\u0442\u043e\u043d\u043e\u0432 \u0438 \u0438\u0445 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0441\u0431\u043e\u0440\u0449\u0438\u043a.<\/b><br \/>   <a name=\"habracut\"><\/a>  <\/p>\n<h3>\u041a\u043b\u0430\u0441\u0441-Singleton<\/h3>\n<p>  <\/p>\n<pre><code class=\"objectivec\">using UnityEngine; public class Singleton&lt;T&gt;: MonoBehaviour where T: MonoBehaviour { \tpublic static T instance { get; private set; }  \tpublic void Awake() \t{ \t\tif (instance == null) \t\t{ \t\t\tinstance = GetComponent&lt;T&gt;(); \t\t\tManagersAregator.addManager(instance); \t\t} \t\telse \t\t{ \t\t\tDebug.Log($&quot;&lt;color=red&gt;\u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0434\u0443\u0431\u043b\u0438\u043a\u0430\u0442\u0430 \u0441\u0438\u043d\u0433\u043b\u0442\u043e\u043d\u0430 {typeof(T).ToString()}&lt;\/color&gt;&quot;); \t\t\tDestroy(gameObject); \t\t} \t}  \tpublic void OnDestroy() \t{ \t\tManagersAregator.removeManager&lt;T&gt;(); \t} } <\/code><\/pre>\n<p>  \u0412 \u043a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0441\u0438\u043d\u0433\u043b\u0442\u043e\u043d\u0430 \u0432 Unity \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f <i>instance<\/i>. \u0412 \u043c\u0435\u0442\u043e\u0434\u0435 <i>Awake()<\/i> \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d \u043b\u0438 <i>instance<\/i>. \u0415\u0441\u043b\u0438 \u043d\u0435\u0442, \u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u043a\u043b\u0430\u0441\u0441\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043b\u044e\u0447\u0435\u0432\u043e\u0433\u043e \u0441\u043b\u043e\u0432\u0430 <i>this<\/i>. \u041d\u043e \u0442.\u043a. \u0432 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u00ab\u0448\u0430\u0431\u043b\u043e\u043d\u043d\u0430\u044f\u00bb \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u043a\u043b\u0430\u0441\u0441\u0430 <i>\u0422<\/i>, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <i>this <\/i> \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c. \u041d\u043e \u043c\u044b \u0441 \u043b\u0435\u0433\u043a\u043e\u0441\u0442\u044c\u044e \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 <i>\u0422<\/i> \u0441 \u043e\u0431\u044a\u0435\u043a\u0442\u0430, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0432\u0438\u0441\u0438\u0442 \u0441\u043a\u0440\u0438\u043f\u0442. \u0415\u0441\u043b\u0438 \u0436\u0435 <i>instance <\/i>\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d, \u0442\u043e \u0435\u0433\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0443\u043d\u0438\u0447\u0442\u043e\u0436\u0438\u0442\u044c. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043e\u0431\u044a\u0435\u043a\u0442 \u0432\u0441\u0435\u0433\u0434\u0430 \u0431\u0443\u0434\u0435\u0442 \u043d\u0430 \u0441\u0446\u0435\u043d\u0435 \u0432 \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u043c \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0435.<\/p>\n<p>  \u0412 \u043c\u0435\u0442\u043e\u0434\u0435 <i>Awake()<\/i> \u0432 \u043a\u043b\u0430\u0441\u0441-\u0430\u0433\u0440\u0435\u0433\u0430\u0442\u043e\u0440 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u043e\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442, \u0430 \u0432 \u043c\u0435\u0442\u043e\u0434\u0435 <i>OnDestroy() <\/i>\u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0443\u0434\u0430\u043b\u044f\u0435\u0442\u0441\u044f \u0438\u0437 \u043a\u043b\u0430\u0441\u0441\u0430-\u0430\u0433\u0440\u0435\u0433\u0430\u0442\u043e\u0440\u0430.<\/p>\n<p>  \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u0438\u043d\u0433\u043b\u0442\u043e\u043d\u0430 \u043a\u043b\u0430\u0441\u0441\u0430 <i>MyClass <\/i>\u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0442\u0430\u043a:<\/p>\n<pre><code class=\"objectivec\">public class MyClass: Singleton&lt;MyClass&gt; <\/code><\/pre>\n<p>  \u0415\u0441\u043b\u0438 \u043d\u0430\u0434\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434 <i>Awake()<\/i> \u0432 \u043a\u043b\u0430\u0441\u0441\u0435 <i>MyClass<\/i>, \u0442\u043e \u043d\u0430\u0434\u043e \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u043c \u0441\u043b\u043e\u0432\u043e\u043c <i>base <\/i>(<a href=\"https:\/\/metanit.com\/sharp\/tutorial\/3.7.php\">\u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043e base \u0438 \u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u0438<\/a>):<\/p>\n<pre><code class=\"objectivec\">new void Awake() \t{ \t\tbase.Awake(); \/\/\u0412\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043c\u0435\u0442\u043e\u0434 Awake() \u043a\u043b\u0430\u0441\u0441\u0430-\u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f \t\t\/\/\u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0439 \u043a\u043e\u0434 \t} <\/code><\/pre>\n<p>  <\/p>\n<h3>\u041a\u043b\u0430\u0441\u0441-\u0430\u0433\u0440\u0435\u0433\u0430\u0442\u043e\u0440<\/h3>\n<p>  <\/p>\n<pre><code class=\"objectivec\">using System.Collections.Generic; using UnityEngine;  public static class ManagersAregator { \tstatic Dictionary&lt;string, MonoBehaviour&gt; Managers = new Dictionary&lt;string, MonoBehaviour&gt;();  \tpublic static void addManager&lt;T&gt;(T newManager) \t{ \t\tstring keyWord = typeof(T).ToString();  \t\tif(Managers.ContainsKey(keyWord)) \t\t{ \t\t\tDebug.Log($&quot;[ManagersAregator] \u041c\u0435\u043d\u0435\u0434\u0436\u0435\u0440 -{newManager}- \u0441 \u043a\u043b\u044e\u0447\u043e\u043c -{keyWord}- \u0443\u0436\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442&quot;); \t\t} \t\telse \t\t{ \t\t\tManagers.Add(keyWord, newManager as MonoBehaviour); \t\t\tDebug.Log($&quot;&lt;color=green&gt;[ManagersAregator] \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d \u043d\u043e\u0432\u044b\u0439 \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440 -{newManager}- \u0441 \u043a\u043b\u044e\u0447\u043e\u043c -{keyWord}-&lt;\/color&gt;&quot;); \t\t} \t}  \tpublic static T getManager&lt;T&gt;(string callback) where T: Singleton&lt;T&gt; \t{ \t\tstring keyWord = typeof(T).ToString();  \t\tif(Managers.ContainsKey(keyWord)) \t\t{ \t\t\tDebug.Log($&quot;&lt;color=yellow&gt;[{callback}] \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440\u0430 -{keyWord}-&lt;\/color&gt;&quot;);  \t\t\tMonoBehaviour mbTemp = null; \t\t\tT manager = null;  \t\t\tif(Managers.TryGetValue(keyWord, out mbTemp)) \t\t\t{ \t\t\t\tmanager = (T)mbTemp; \t\t\t\tDebug.Log($&quot;&lt;color=green&gt;[{callback}] \u041c\u0435\u043d\u0435\u0434\u0436\u0435\u0440 -{manager}- \u043f\u043e\u043b\u0443\u0447\u0435\u043d&lt;\/color&gt;&quot;); \t\t\t} \t\t\telse \t\t\t{ \t\t\t\tDebug.Log($&quot;&lt;color=red&gt;[{callback}] \u041e\u0448\u0438\u0431\u043a\u0430 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440\u0430 -{keyWord}-&lt;\/color&gt;&quot;); \t\t\t}  \t\t\treturn manager; \t\t}  \t\tDebug.Log($&quot;&lt;color=red&gt;[ManagersAregator] \u041c\u0435\u043d\u0435\u0434\u0436\u0435\u0440 \u0441 \u043a\u043b\u044e\u0447\u043e\u043c -{keyWord}- \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0432 \u0441\u043b\u043e\u0432\u0430\u0440\u0435.&lt;\/color&gt;&quot;); \t\treturn null; \t}  \tpublic static void removeManager&lt;T&gt;() \t{ \t\tstring keyWord = typeof(T).ToString();  \t\tif(Managers.ContainsKey(keyWord)) \t\t{ \t\t\tManagers.Remove(keyWord); \t\t\tDebug.Log($&quot;[ManagersAregator] \u041c\u0435\u043d\u0435\u0434\u0436\u0435\u0440 \u0441 \u043a\u043b\u044e\u0447\u043e\u043c -{keyWord}- \u0443\u0434\u0430\u043b\u0435\u043d \u0438\u0437 \u0441\u043b\u043e\u0432\u0430\u0440\u044f&quot;); \t\t} \t\telse \t\t{ \t\t\tDebug.Log($&quot;[ManagersAregator] \u041c\u0435\u043d\u0435\u0434\u0436\u0435\u0440 \u0441 \u043a\u043b\u044e\u0447\u043e\u043c -{keyWord}- \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0432 \u0441\u043b\u043e\u0432\u0430\u0440\u0435.&quot;); \t\t} \t} } <\/code><\/pre>\n<p>  \u041a\u043b\u0430\u0441\u0441 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u043c, \u0442.\u0435. \u0443 \u043d\u0435\u0433\u043e \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430 \u043d\u0430 \u0441\u0446\u0435\u043d\u0435. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043e\u043d \u0431\u0443\u0434\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u0438\u0437 \u043b\u044e\u0431\u043e\u0439 \u0441\u0446\u0435\u043d\u044b \u0432 \u043b\u044e\u0431\u043e\u0435 \u0432\u0440\u0435\u043c\u044f.<\/p>\n<p>  \u0412\u0441\u0435 \u0441\u043a\u0440\u0438\u043f\u0442\u044b-\u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440\u044b (\u0441\u0438\u043d\u0433\u043b\u0442\u043e\u043d\u044b) \u0445\u0440\u0430\u043d\u044f\u0442\u0441\u044f \u0432 \u0441\u043b\u043e\u0432\u0430\u0440\u0435 <i>Managers<\/i>. \u041a\u043b\u044e\u0447\u043e\u043c \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440\u0430 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0438\u043c\u044f \u043a\u043b\u0430\u0441\u0441\u0430 \u044d\u0442\u043e\u0433\u043e \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440\u0430. \u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u043d\u043e\u0432\u0438\u0447\u043a\u0438 \u0432 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u0437\u0430\u0434\u0430\u0434\u0443\u0442 \u0432\u043e\u043f\u0440\u043e\u0441: \u00ab\u0411\u0430, \u0430 \u0447\u0442\u043e \u044d\u0442\u043e \u0441\u043b\u043e\u0432\u0430\u0440\u044c \u0445\u0440\u0430\u043d\u0438\u0442 <i>MonoBehaviour<\/i>, \u0430 \u0432\u0441\u0435 \u043a\u043b\u0430\u0441\u0441\u044b \u043d\u0430\u0441\u043b\u0435\u0434\u0443\u044e\u0442\u0441\u044f \u043e\u0442 <i>Singleton<\/i>?\u00bb. \u042d\u0442\u043e \u0445\u043e\u0440\u043e\u0448\u0438\u0439 \u0432\u043e\u043f\u0440\u043e\u0441, \u043e\u0442\u0432\u0435\u0442 \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0438 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043a\u043b\u044e\u0447\u043e\u043c \u043a \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0430\u0433\u0440\u0435\u0433\u0430\u0442\u043e\u0440\u0430 \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440\u043e\u0432 \u043b\u044e\u0431\u044b\u0445 \u043a\u043b\u0430\u0441\u0441\u043e\u0432.<\/p>\n<p>  \u0412 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043f\u043e\u043d\u044f\u0442\u0438\u0435 <a href=\"https:\/\/metanit.com\/sharp\/tutorial\/3.11.php\">upcasting <\/a> \u2014 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0442\u0438\u043f\u0430 \u043a \u0431\u0430\u0437\u043e\u0432\u043e\u043c\u0443 \u043a\u043b\u0430\u0441\u0441\u0443. \u0411\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u0442\u043e\u043c\u0443, \u0447\u0442\u043e \u0432\u0441\u0435 \u043a\u043b\u0430\u0441\u0441\u044b \u0432 Unity \u043d\u0430\u0441\u043b\u0435\u0434\u0443\u044e\u0442\u0441\u044f \u043e\u0442 <i>MonoBehaviour<\/i>, <b>\u0438\u0445 \u043c\u043e\u0436\u043d\u043e \u0430\u043f\u043a\u0430\u0441\u0442\u0438\u0442\u044c \u043a MonoBehabiour<\/b>. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0441\u043b\u043e\u0432\u0430\u0440\u044c <i>Managers <\/i>\u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u043a\u043b\u0430\u0441\u0441\u0430 <i>MonoBehaviour<\/i>.<\/p>\n<p>  \u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043c\u0435\u0442\u043e\u0434\u044b \u043a\u043b\u0430\u0441\u0441\u0430-\u0430\u0433\u0440\u0435\u0433\u0430\u0442\u043e\u0440\u0430:<\/p>\n<pre><code class=\"objectivec\">void addManager&lt;T&gt;(T newManager)<\/code><\/pre>\n<p>  \u042d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0432 \u043c\u0435\u0442\u043e\u0434\u0435 <i>Awake()<\/i> \u043a\u043b\u0430\u0441\u0441\u0430 <i>Singleton<\/i>. \u0410\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u043c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u043a\u043b\u0430\u0441\u0441\u0430 <i>instance<\/i>. \u0414\u0430\u043b\u0435\u0435 \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u043a\u043b\u044e\u0447 \u043f\u043e \u0438\u043c\u0435\u043d\u0438 \u043a\u043b\u0430\u0441\u0441\u0430, \u043a\u043e\u0442\u043e\u0440\u043e\u043c\u0443 \u043f\u0440\u0438\u043d\u0430\u0434\u043b\u0435\u0436\u0438\u0442 <i>instance<\/i>, \u0438 \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0432 \u0441\u043b\u043e\u0432\u0430\u0440\u044c.<\/p>\n<pre><code class=\"objectivec\">T getManager&lt;T&gt;(string callback) where T: Singleton&lt;T&gt;<\/code><\/pre>\n<p>  \u0424\u0443\u043d\u043a\u0446\u0438\u044f \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u043c \u0441\u0442\u0440\u043e\u043a\u0443 \u0441 \u0438\u043c\u0435\u043d\u0435\u043c \u043a\u043b\u0430\u0441\u0441\u0430, \u043e\u0442\u043a\u0443\u0434\u0430 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043c\u0435\u0442\u043e\u0434. \u042d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u043d\u043e \u0438\u0441\u043a\u043b\u044e\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0434\u043b\u044f \u0443\u0434\u043e\u0431\u043d\u043e\u0433\u043e \u0434\u0435\u0431\u0430\u0433\u0430 (\u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u0438 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0435\u0442\u0441\u044f \u043a\u043b\u0430\u0441\u0441, \u043e\u0442\u043a\u0443\u0434\u0430 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043c\u0435\u0442\u043e\u0434). \u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u044d\u0442\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430 \u0432 \u043a\u043b\u0430\u0441\u0441\u0430 <i>AnotherMyClass<\/i>:<\/p>\n<pre><code class=\"objectivec\">public class AnotherMyClass: MonoBehaviour \tvoid Start() \t{ \t\tstring cb = GetType().ToString(); \/\/\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0438\u043c\u0435\u043d\u0438 \u043a\u043b\u0430\u0441\u0441\u0430 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \t\tMyClass MC = ManagersAregator.getManager&lt;MyClass &gt;(cb); \t} <\/code><\/pre>\n<p>  \u0412 \u043a\u043e\u043d\u0441\u043e\u043b\u0438 \u0431\u0443\u0434\u0435\u0442 \u0432\u0438\u0441\u0435\u0442\u044c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435: <b>&quot;<s>\u0425\u0443**\u044f, \u043f\u0435\u0440\u0435\u0434\u0435\u043b\u044b\u0432\u0430\u0439<\/s> [AnotherMyClass] \u041c\u0435\u043d\u0435\u0434\u0436\u0435\u0440 -MyClass- \u043f\u043e\u043b\u0443\u0447\u0435\u043d&quot;.<\/b><\/p>\n<pre><code class=\"objectivec\">void removeManager&lt;T&gt;()<\/code><\/pre>\n<p>  \u0423\u0434\u0430\u043b\u044f\u0435\u0442 \u0438\u0437 \u0441\u043b\u043e\u0432\u0430\u0440\u044f \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440 \u0442\u0438\u043f\u0430 <i>\u0422<\/i>, \u0435\u0441\u043b\u0438 \u043e\u043d \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442\u0441\u044f \u0432 \u0441\u043b\u043e\u0432\u0430\u0440\u0435.<\/p>\n<h3>\u0418\u0442\u043e\u0433\u0438<\/h3>\n<p>  \u0424\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438, \u0438\u0437 \u0442\u0440\u0435\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u043a\u043b\u0430\u0441\u0441\u0430-\u0430\u0433\u0440\u0435\u0433\u0430\u0442\u043e\u0440\u0430 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0443 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043c\u0435\u0442\u043e\u0434 <i>getManager<\/i>. \u041e\u0441\u043e\u0431\u044b\u043c \u043f\u043b\u044e\u0441\u043e\u043c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0445\u043e\u0440\u043e\u0448\u0430\u044f \u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439 \u0434\u0435\u0431\u0430\u0433\u0430 \u043d\u0430 \u0432\u0441\u0435 \u0441\u043b\u0443\u0447\u0430\u0438 \u0436\u0438\u0437\u043d\u0438. \u041f\u043e \u0436\u0435\u043b\u0430\u043d\u0438\u044e, \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0436\u0435, \u0438\u0445 \u043c\u043e\u0436\u043d\u043e \u043e\u0442\u043a\u043b\u044e\u0447\u0438\u0442\u044c. \u042f \u0436\u0435 \u0441\u0447\u0438\u0442\u0430\u044e, \u0447\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u043e\u0447\u0435\u043d\u044c \u0443\u0434\u043e\u0431\u043d\u043e \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u0432 \u043a\u0430\u043a\u043e\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u0432\u0440\u0435\u043c\u0435\u043d\u0438, \u043a\u0430\u043a\u043e\u0439 \u043a\u043b\u0430\u0441\u0441 \u043f\u044b\u0442\u0430\u0435\u0442\u0441\u044f \u0447\u0442\u043e-\u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438 \u0447\u0442\u043e \u043e\u043d \u043f\u044b\u0442\u0430\u0435\u0442\u0441\u044f \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c.<\/p>\n<p>  <i>\u041d\u0430\u0434\u0435\u044e\u0441\u044c, \u044d\u0442\u0430 \u0441\u0442\u0430\u0442\u044c\u044f \u0431\u044b\u043b\u0430 \u0434\u043b\u044f \u0432\u0430\u0441 \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0439 \u0438 \u0432\u044b \u0443\u0437\u043d\u0430\u043b\u0438 \u0447\u0442\u043e-\u0442\u043e \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0435 \u0434\u043b\u044f \u0441\u0435\u0431\u044f!<\/i><\/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\/494930\/\"> https:\/\/habr.com\/ru\/post\/494930\/<\/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\/494930\/\">\n<h3>\u0412\u0441\u0442\u0443\u043f\u043b\u0435\u043d\u0438\u0435<\/h3>\n<p>  <i>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0440\u0435\u0447\u044c \u043f\u043e\u0439\u0434\u0435\u0442 \u043e\u0431 \u043e\u0434\u043d\u043e\u043c \u0432\u0438\u0434\u0435 \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u043c\u0435\u0436\u0434\u0443 \u0441\u043a\u0440\u0438\u043f\u0442\u0430\u043c\u0438-\u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440\u0430\u043c\u0438 (\u0441\u0438\u043d\u0433\u043b\u0442\u043e\u043d\u0430\u043c\u0438 \u0438\u043c\u0435\u043d\u0443\u0435\u043c\u044b\u043c\u0438), \u0430 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e \u2014 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430-\u0430\u0433\u0440\u0435\u0433\u0430\u0442\u043e\u0440\u0430, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c\u0441\u044f \u0441\u0441\u044b\u043b\u043a\u0438 \u043d\u0430 \u0432\u0441\u0435 instance \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440\u043e\u0432. \u0418\u0434\u0435\u044f \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043a\u043b\u0430\u0441\u0441-\u0430\u0433\u0440\u0435\u0433\u0430\u0442\u043e\u0440 \u043f\u0440\u0438\u0448\u043b\u0430 \u043c\u043d\u0435 \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0447\u0442\u0435\u043d\u0438\u044f \u044d\u0442\u043e\u0439 <a href=\"https:\/\/habr.com\/ru\/post\/212055\/\">\u0441\u0442\u0430\u0442\u044c\u0438<\/a>.<\/i><\/p>\n<h3>\u0417\u0430\u0434\u0430\u0447\u0438<\/h3>\n<p>  \u042f \u043f\u0440\u0438\u0448\u0435\u043b \u043a \u0432\u044b\u0432\u043e\u0434\u0443, \u0447\u0442\u043e \u0437\u0430\u0431\u0438\u0432\u0430\u0442\u044c \u0441\u0432\u043e\u0438\u043c\u0438 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u043c\u0438 \u0440\u0443\u0447\u043a\u0430\u043c\u0438 \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440\u044b \u0432 \u043a\u043b\u0430\u0441\u0441-\u0430\u0433\u0440\u0435\u0433\u0430\u0442\u043e\u0440 \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u2014 \u044d\u0442\u043e \u0434\u0438\u043a\u043e \u043d\u0435\u0443\u0434\u043e\u0431\u043d\u043e. \u041f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u043d\u043e\u0432\u043e\u0433\u043e \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440\u0430 \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u043e\u0442\u043a\u0440\u044b\u0432\u0430\u0442\u044c \u043a\u043b\u0430\u0441\u0441-\u0430\u0433\u0440\u0435\u0433\u0430\u0442\u043e\u0440 \u0438 \u0432\u043d\u043e\u0441\u0438\u0442\u044c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f. \u041f\u0440\u0438 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0435 \u043e\u043f\u044b\u0442\u0430 \u043c\u043e\u0436\u043d\u043e \u0442\u0430\u043a\u0438\u0435 \u043d\u0443\u0434\u043d\u044b\u0435 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u044b \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043f\u043e\u0441\u0442\u0430\u0432\u0438\u043b\u0438\u0441\u044c \u0437\u0430\u0434\u0430\u0447\u0438: <b>\u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u0438\u043d\u0433\u043b\u0442\u043e\u043d\u043e\u0432 \u0438 \u0438\u0445 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0441\u0431\u043e\u0440\u0449\u0438\u043a.<\/b>   <\/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-301050","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/301050","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=301050"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/301050\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=301050"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=301050"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=301050"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}