{"id":38360,"date":"2025-05-20T09:18:21","date_gmt":"2025-05-20T01:18:21","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/38360.html"},"modified":"2025-05-20T09:18:21","modified_gmt":"2025-05-20T01:18:21","slug":"unity%e5%ba%8f%e5%88%97%e5%8c%96%e5%ad%97%e6%ae%b5%e3%80%81%e5%8d%95%e4%be%8b%e6%a8%a1%e5%bc%8fsingleton-pattern","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/38360.html","title":{"rendered":"Unity\u5e8f\u5217\u5316\u5b57\u6bb5\u3001\u5355\u4f8b\u6a21\u5f0f(Singleton Pattern)"},"content":{"rendered":"<h2>\u4e00\u3001\u5e8f\u5217\u5316\u5b57\u6bb5<\/h2>\n<p>\u5728Unity\u4e2d&#xff0c;\u5e8f\u5217\u5316\u5b57\u6bb5\u662f\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u6982\u5ff5&#xff0c;\u4e3b\u8981\u7528\u4e8e\u5728Unity\u7f16\u8f91\u5668\u4e2d\u663e\u793a\u548c\u7f16\u8f91\u7c7b\u7684\u6210\u5458\u53d8\u91cf&#xff0c;\u6216\u8005\u5728\u8fd0\u884c\u65f6\u5c06\u5bf9\u8c61\u7684\u72b6\u6001\u4fdd\u5b58\u5230\u6587\u4ef6\u6216\u7f51\u7edc\u4e2d\u3002<\/p>\n<h3>\u00a01.Unity\u5e8f\u5217\u5316\u5b57\u6bb5\u7684\u4f5c\u7528<\/h3>\n<ul>\n<li>\u5728\u7f16\u8f91\u5668\u4e2d\u663e\u793a\u548c\u7f16\u8f91\u5b57\u6bb5&#xff1a;\u9ed8\u8ba4\u60c5\u51b5\u4e0b&#xff0c;\u53ea\u6709\u516c\u5171\u5b57\u6bb5&#xff08; \u00a0public \u00a0&#xff09;\u624d\u4f1a\u5728Unity\u7f16\u8f91\u5668\u7684Inspector\u7a97\u53e3\u4e2d\u663e\u793a\u3002\u5982\u679c\u5e0c\u671b\u79c1\u6709\u5b57\u6bb5&#xff08; \u00a0private \u00a0&#xff09;\u6216\u53d7\u4fdd\u62a4\u5b57\u6bb5&#xff08; \u00a0protected \u00a0&#xff09;\u4e5f\u80fd\u5728\u7f16\u8f91\u5668\u4e2d\u663e\u793a\u5e76\u88ab\u7f16\u8f91&#xff0c;\u53ef\u4ee5\u901a\u8fc7\u5e8f\u5217\u5316\u5b57\u6bb5\u6765\u5b9e\u73b0\u3002<\/li>\n<li>\u4fdd\u5b58\u548c\u52a0\u8f7d\u5bf9\u8c61\u72b6\u6001&#xff1a;\u5e8f\u5217\u5316\u5b57\u6bb5\u8fd8\u53ef\u4ee5\u7528\u4e8e\u5c06\u5bf9\u8c61\u7684\u72b6\u6001\u4fdd\u5b58\u5230\u6587\u4ef6\u6216\u7f51\u7edc\u4e2d&#xff0c;\u5e76\u5728\u9700\u8981\u65f6\u91cd\u65b0\u52a0\u8f7d\u8fd9\u4e9b\u72b6\u6001\u3002<\/li>\n<\/ul>\n<h3>2. \u4f7f\u7528 \u00a0[SerializeField] \u00a0\u5c5e\u6027<\/h3>\n<p>Unity\u63d0\u4f9b\u4e86 \u00a0[SerializeField] \u00a0\u5c5e\u6027&#xff0c;\u7528\u4e8e\u5c06\u79c1\u6709\u6216\u53d7\u4fdd\u62a4\u7684\u5b57\u6bb5\u6807\u8bb0\u4e3a\u53ef\u5e8f\u5217\u5316\u3002\u8fd9\u6837&#xff0c;\u8fd9\u4e9b\u5b57\u6bb5\u5c31\u53ef\u4ee5\u5728Unity\u7f16\u8f91\u5668\u7684Inspector\u7a97\u53e3\u4e2d\u663e\u793a\u548c\u7f16\u8f91\u3002<\/p>\n<p>using UnityEngine;<\/p>\n<p>public class ExampleScript : MonoBehaviour<br \/>\n{<br \/>\n    \/\/ \u8fd9\u662f\u4e00\u4e2a\u516c\u5171\u5b57\u6bb5&#xff0c;\u4f1a\u5728Inspector\u4e2d\u663e\u793a<br \/>\n    public int publicField;<\/p>\n<p>    \/\/ \u8fd9\u662f\u4e00\u4e2a\u79c1\u6709\u5b57\u6bb5&#xff0c;\u4e0d\u4f1a\u5728Inspector\u4e2d\u663e\u793a<br \/>\n    private string privateField;<\/p>\n<p>    \/\/ \u8fd9\u662f\u4e00\u4e2a\u79c1\u6709\u5b57\u6bb5&#xff0c;\u4f46\u4f7f\u7528[Serialize]\u6807\u8bb0\u540e\u4f1a\u5728Inspector\u4e2d\u663e\u793a<br \/>\n    [SerializeField] private float serializedField;<\/p>\n<p>    void Start()<br \/>\n    {<br \/>\n        Debug.Log(&#034;Public Field: &#034; &#043; publicField);<br \/>\n        Debug.Log(&#034;Serialized Field: &#034; &#043; serializedField);<br \/>\n    }<br \/>\n}<br \/>\n\/\/publicField\u548cserializedField\u90fd\u4f1a\u5728Inspector\u4e2d\u663e\u793a&#xff0c;\u800cprivateField\u4e0d\u4f1a\u663e\u793a\u3002<br \/>\n\/\/\u4f7f\u7528[SerializeField]\u5c5e\u6027\u53ef\u4ee5\u8ba9\u5f00\u53d1\u8005\u66f4\u597d\u5730\u5c01\u88c5\u7c7b\u7684\u5b9e\u73b0\u7ec6\u8282&#xff0c;\u540c\u65f6\u4ecd\u7136\u5141\u8bb8\u5728\u7f16\u8f91\u5668\u4e2d\u5bf9\u5b57\u6bb5\u8fdb\u884c\u7f16\u8f91\u3002 <\/p>\n<h3>\u00a03. \u9ed8\u8ba4\u5e8f\u5217\u5316\u89c4\u5219<\/h3>\n<ul>\n<li>\u516c\u5171\u5b57\u6bb5&#xff1a;\u9ed8\u8ba4\u60c5\u51b5\u4e0b&#xff0c;\u6240\u6709\u516c\u5171\u5b57\u6bb5&#xff08; \u00a0public \u00a0&#xff09;\u90fd\u4f1a\u88ab\u5e8f\u5217\u5316&#xff0c;\u65e0\u8bba\u662f\u5426\u4f7f\u7528\u4e86 \u00a0[SerializeField] \u00a0\u5c5e\u6027\u3002<\/li>\n<li>\u79c1\u6709\u5b57\u6bb5&#xff1a;\u9ed8\u8ba4\u60c5\u51b5\u4e0b&#xff0c;\u79c1\u6709\u5b57\u6bb5&#xff08; \u00a0private \u00a0&#xff09;\u4e0d\u4f1a\u88ab\u5e8f\u5217\u5316&#xff0c;\u9664\u975e\u4f7f\u7528\u4e86 \u00a0[SerializeField] \u00a0\u5c5e\u6027\u3002<\/li>\n<li>\u9759\u6001\u5b57\u6bb5&#xff1a;\u9759\u6001\u5b57\u6bb5&#xff08; \u00a0static \u00a0&#xff09;\u6c38\u8fdc\u4e0d\u4f1a\u88ab\u5e8f\u5217\u5316&#xff0c;\u56e0\u4e3a\u5b83\u4eec\u4e0d\u5c5e\u4e8e\u5bf9\u8c61\u7684\u5b9e\u4f8b\u72b6\u6001\u3002<\/li>\n<li>\u53ea\u8bfb\u5b57\u6bb5&#xff1a;\u53ea\u8bfb\u5b57\u6bb5&#xff08; \u00a0readonly \u00a0&#xff09;\u4e0d\u4f1a\u88ab\u5e8f\u5217\u5316&#xff0c;\u56e0\u4e3a\u5b83\u4eec\u5728\u6784\u9020\u51fd\u6570\u4e4b\u540e\u4e0d\u80fd\u88ab\u4fee\u6539\u3002<\/li>\n<li>\u7279\u6b8a\u7c7b\u578b\u5b57\u6bb5&#xff1a;\u67d0\u4e9b\u7279\u6b8a\u7c7b\u578b&#xff08;\u5982 \u00a0Dictionary \u00a0\u3001 \u00a0Action \u00a0\u3001 \u00a0Func \u00a0\u7b49&#xff09;\u4e0d\u4f1a\u88ab\u5e8f\u5217\u5316&#xff0c;\u56e0\u4e3a\u5b83\u4eec\u7684\u5e8f\u5217\u5316\u903b\u8f91\u6bd4\u8f83\u590d\u6742\u3002<\/li>\n<\/ul>\n<h3>4. \u4f7f\u7528 \u00a0[SerializeField] \u00a0\u7684\u6ce8\u610f\u4e8b\u9879<\/h3>\n<ul>\n<li>\u4e0d\u8981\u6ee5\u7528&#xff1a;\u867d\u7136 \u00a0[SerializeField] \u00a0\u53ef\u4ee5\u8ba9\u79c1\u6709\u5b57\u6bb5\u5728\u7f16\u8f91\u5668\u4e2d\u663e\u793a&#xff0c;\u4f46\u4e0d\u8981\u8fc7\u5ea6\u4f7f\u7528\u5b83\u3002\u8fc7\u591a\u7684\u5b57\u6bb5\u66b4\u9732\u5728Inspector\u4e2d\u53ef\u80fd\u4f1a\u5bfc\u81f4\u7f16\u8f91\u5668\u754c\u9762\u53d8\u5f97\u6df7\u4e71\u3002<\/li>\n<li>\u6027\u80fd\u5f71\u54cd&#xff1a;\u5e8f\u5217\u5316\u5b57\u6bb5\u53ef\u80fd\u4f1a\u5bf9\u6027\u80fd\u4ea7\u751f\u4e00\u5b9a\u5f71\u54cd&#xff0c;\u5c24\u5176\u662f\u5728\u5927\u91cf\u5bf9\u8c61\u9700\u8981\u5e8f\u5217\u5316\u65f6\u3002\u56e0\u6b64&#xff0c;\u5728\u6027\u80fd\u654f\u611f\u7684\u573a\u666f\u4e2d&#xff0c;\u9700\u8981\u8c28\u614e\u4f7f\u7528\u5e8f\u5217\u5316\u5b57\u6bb5\u3002<\/li>\n<li>\u4e0e \u00a0[HideInInspector] \u00a0\u7684\u533a\u522b&#xff1a; \u00a0[SerializeField] \u00a0\u7528\u4e8e\u5c06\u79c1\u6709\u5b57\u6bb5\u6807\u8bb0\u4e3a\u53ef\u5e8f\u5217\u5316&#xff0c;\u4f7f\u5176\u5728Inspector\u4e2d\u663e\u793a&#xff1b;\u800c \u00a0[HideInInspector] \u00a0\u7528\u4e8e\u9690\u85cf\u516c\u5171\u5b57\u6bb5&#xff0c;\u4f7f\u5176\u4e0d\u5728Inspector\u4e2d\u663e\u793a\u3002<\/li>\n<\/ul>\n<h3>\u00a05. \u5e8f\u5217\u5316\u5b57\u6bb5\u7684\u5e94\u7528\u573a\u666f<\/h3>\n<ul>\n<li>\u7ec4\u4ef6\u5c5e\u6027\u7f16\u8f91&#xff1a;\u5728\u5f00\u53d1\u81ea\u5b9a\u4e49\u7ec4\u4ef6\u65f6&#xff0c;\u4f7f\u7528 \u00a0[SerializeField] \u00a0\u53ef\u4ee5\u8ba9\u5f00\u53d1\u8005\u66f4\u597d\u5730\u5c01\u88c5\u7ec4\u4ef6\u7684\u5b9e\u73b0\u7ec6\u8282&#xff0c;\u540c\u65f6\u4ecd\u7136\u5141\u8bb8\u7528\u6237\u5728\u7f16\u8f91\u5668\u4e2d\u914d\u7f6e\u7ec4\u4ef6\u7684\u5c5e\u6027\u3002<\/li>\n<li>\u4fdd\u5b58\u548c\u52a0\u8f7d\u6e38\u620f\u72b6\u6001&#xff1a;\u901a\u8fc7\u5e8f\u5217\u5316\u5b57\u6bb5&#xff0c;\u53ef\u4ee5\u5c06\u6e38\u620f\u5bf9\u8c61\u7684\u72b6\u6001\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d&#xff0c;\u5e76\u5728\u9700\u8981\u65f6\u91cd\u65b0\u52a0\u8f7d\u8fd9\u4e9b\u72b6\u6001&#xff0c;\u5b9e\u73b0\u6e38\u620f\u7684\u5b58\u6863\u548c\u8bfb\u6863\u529f\u80fd\u3002<\/li>\n<li>\u7f51\u7edc\u540c\u6b65&#xff1a;\u5728\u591a\u4eba\u6e38\u620f\u4e2d&#xff0c;\u53ef\u4ee5\u5c06\u9700\u8981\u540c\u6b65\u7684\u5b57\u6bb5\u6807\u8bb0\u4e3a\u5e8f\u5217\u5316\u5b57\u6bb5&#xff0c;\u7136\u540e\u901a\u8fc7\u7f51\u7edc\u534f\u8bae\u5c06\u8fd9\u4e9b\u5b57\u6bb5\u7684\u72b6\u6001\u53d1\u9001\u5230\u5176\u4ed6\u5ba2\u6237\u7aef\u3002<\/li>\n<\/ul>\n<h3>6. \u6269\u5c55&#xff1a;\u81ea\u5b9a\u4e49\u5e8f\u5217\u5316<\/h3>\n<p>\u5982\u679c\u9ed8\u8ba4\u7684\u5e8f\u5217\u5316\u673a\u5236\u4e0d\u80fd\u6ee1\u8db3\u9700\u6c42&#xff0c;Unity\u8fd8\u63d0\u4f9b\u4e86\u81ea\u5b9a\u4e49\u5e8f\u5217\u5316\u7684\u65b9\u6cd5\u3002\u4f8b\u5982&#xff0c;\u53ef\u4ee5\u901a\u8fc7\u5b9e\u73b0 \u00a0ISerializationCallbackReceiver \u00a0\u63a5\u53e3&#xff0c;\u5728\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u65f6\u6267\u884c\u81ea\u5b9a\u4e49\u903b\u8f91\u3002<\/p>\n<p>using UnityEngine;<\/p>\n<p>public class CustomSerializationExample : MonoBehaviour, ISerializationCallbackReceiver<br \/>\n{<br \/>\n    \/\/ \u8fd9\u662f\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5b57\u6bb5&#xff0c;\u4e0d\u4f1a\u76f4\u63a5\u88ab\u5e8f\u5217\u5316<br \/>\n    private string customField;<\/p>\n<p>    \/\/ \u7528\u4e8e\u5b58\u50a8\u81ea\u5b9a\u4e49\u5b57\u6bb5\u7684\u5e8f\u5217\u5316\u6570\u636e<br \/>\n    [SerializeField] private string serializedData;<\/p>\n<p>    public void OnBeforeSerialize()<br \/>\n    {<br \/>\n        \/\/ \u5728\u5e8f\u5217\u5316\u4e4b\u524d&#xff0c;\u5c06\u81ea\u5b9a\u4e49\u5b57\u6bb5\u7684\u503c\u8f6c\u6362\u4e3a\u53ef\u5e8f\u5217\u5316\u7684\u683c\u5f0f<br \/>\n        serializedData &#061; customField;<br \/>\n    }<\/p>\n<p>    public void OnAfterDeserialize()<br \/>\n    {<br \/>\n        \/\/ \u5728\u53cd\u5e8f\u5217\u5316\u4e4b\u540e&#xff0c;\u5c06\u5e8f\u5217\u5316\u7684\u6570\u636e\u8fd8\u539f\u4e3a\u81ea\u5b9a\u4e49\u5b57\u6bb5\u7684\u503c<br \/>\n        customField &#061; serializedData;<br \/>\n    }<br \/>\n}<\/p>\n<h2>\u4e8c\u3001\u5355\u4f8b\u6a21\u5f0f(Singleton Pattern)\u00a0<\/h2>\n<p>\u5728Unity\u4e2d&#xff0c;\u5355\u4f8b\u6a21\u5f0f\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u521b\u5efa\u578b\u8bbe\u8ba1\u6a21\u5f0f&#xff0c;\u7528\u4e8e\u786e\u4fdd\u67d0\u4e2a\u7c7b\u5728\u7a0b\u5e8f\u8fd0\u884c\u671f\u95f4\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\u5b58\u5728&#xff0c;\u5e76\u63d0\u4f9b\u4e00\u4e2a\u5168\u5c40\u8bbf\u95ee\u70b9\u6765\u83b7\u53d6\u8fd9\u4e2a\u5b9e\u4f8b\u3002<\/p>\n<p>\u5728\u5355\u4f8b\u6a21\u5f0f\u4e2d&#xff0c;\u7c7b\u7684\u6784\u9020\u51fd\u6570\u901a\u5e38\u662f\u79c1\u6709\u7684&#xff0c;\u9632\u6b62\u5916\u90e8\u901a\u8fc7 new \u6765\u521b\u5efa\u5bf9\u8c61&#xff0c;\u7c7b\u5185\u90e8\u7ef4\u62a4\u4e00\u4e2a\u9759\u6001\u5b9e\u4f8b&#xff0c;\u901a\u8fc7\u516c\u5171\u7684\u9759\u6001\u65b9\u6cd5\u63d0\u4f9b\u8bbf\u95ee\u3002<\/p>\n<h3 style=\"background-color:transparent\"><span style=\"color:#4d4d4d\"><span style=\"background-color:#ffffff\">\u5355\u4f8b\u6a21\u5f0f\u7684\u5b9e\u73b0\u5206\u4e3a<\/span><\/span><span style=\"color:red\">\u997f\u6c49\u6a21\u5f0f<\/span><span style=\"color:#4d4d4d\"><span style=\"background-color:#ffffff\">\u4e0e<\/span><\/span><span style=\"color:red\">\u61d2\u6c49\u6a21\u5f0f<\/span><span style=\"color:#4d4d4d\"><span style=\"background-color:#ffffff\">\u3002<\/span><\/span><\/h3>\n<h3 style=\"background-color:transparent\">1.\u997f\u6c49\u5f0f&#xff08;Eager Initialization&#xff09;\u5355\u4f8b\u6a21\u5f0f&#xff1a;<\/h3>\n<p>\u662f\u4e00\u79cd\u5728\u7c7b\u52a0\u8f7d\u65f6\u5c31\u7acb\u5373\u521b\u5efa\u5355\u4f8b\u5bf9\u8c61\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002\u8fd9\u79cd\u6a21\u5f0f\u786e\u4fdd\u4e86\u5355\u4f8b\u5bf9\u8c61\u5728\u7a0b\u5e8f\u542f\u52a8\u65f6\u5c31\u5b58\u5728&#xff0c;\u5e76\u4e14\u53ef\u4ee5\u7acb\u5373\u4f7f\u7528\u3002<\/p>\n<h4>\u9002\u7528\u573a\u666f&#xff1a;<\/h4>\n<p>\u5f53\u5bf9\u8c61\u9700\u8981\u5728\u7a0b\u5e8f\u542f\u52a8\u65f6\u7acb\u5373\u53ef\u7528&#xff0c;\u4e14\u521b\u5efa\u6210\u672c\u8f83\u4f4e\u65f6\u4f7f\u7528&#xff0c;\u9002\u5408\u5e38\u7528\u4e8e\u5fc5\u8981\u7684\u6838\u5fc3\u529f\u80fd\u3002\u4f8b\u5b50&#xff1a;\u6e38\u620f\u8bbe\u7f6e\u3001\u73a9\u5bb6\u63a7\u5236\u3001\u4e3b\u83dc\u5355\u7ba1\u7406\u5668\u3002<\/p>\n<h4 style=\"background-color:transparent\">\u5b9e\u73b0\u65b9\u5f0f&#xff1a;<\/h4>\n<p>1&#xff09;\u5c5e\u6027\u5b9e\u73b0\u5355\u4f8b\u6a21\u5f0f&#xff08;<span style=\"color:red\">\u997f\u6c49\u6a21\u5f0f<\/span>&#xff09;<\/p>\n<p>\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u9759\u6001\u5c5e\u6027\u6765\u5b9e\u73b0\u5355\u4f8b\u6a21\u5f0f&#xff0c;\u5e76\u4e14\u901a\u8fc7\u79c1\u6709\u8bbe\u7f6e\u5668&#xff08; \u00a0private set \u00a0&#xff09;\u786e\u4fdd\u5b9e\u4f8b\u5728\u7b2c\u4e00\u6b21\u521b\u5efa\u65f6\u88ab\u8bbe\u7f6e&#xff0c;\u5e76\u4e14\u4e4b\u540e\u4e0d\u80fd\u88ab\u66f4\u6539\u3002<\/p>\n<p>public class Player : MonoBehaviour<br \/>\n{<br \/>\n    \/\/\u5355\u4f8b\u6a21\u5f0f&#xff0c;\u786e\u4fdd\u4e86Player\u7c7b\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b&#xff0c;\u5e76\u4e14\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5168\u5c40\u8bbf\u95ee\u70b9\u6765\u83b7\u53d6\u8fd9\u4e2a\u5b9e\u4f8b<br \/>\n    \/\/\u901a\u8fc7\u79c1\u6709\u8bbe\u7f6e\u8bbf\u95ee\u5668&#xff0c;\u786e\u4fdd\u4e86Instance\u5c5e\u6027\u7684\u503c\u4e0d\u4f1a\u88ab\u5916\u90e8\u4ee3\u7801\u610f\u5916\u4fee\u6539<br \/>\n    public static Player Instance{ get; private set; }<\/p>\n<p>    \/\/ &#8230; \u5176\u4ed6\u4ee3\u7801 &#8230;<\/p>\n<p>    \/\/Awake\u5728\u6e38\u620f\u5bf9\u8c61\u88ab\u521b\u5efa\u65f6\u81ea\u52a8\u8c03\u7528&#xff0c;\u901a\u5e38\u7528\u4e8e\u521d\u59cb\u5316\u64cd\u4f5c\u3002<br \/>\n    private void Awake()<br \/>\n    {<br \/>\n        \/\/\u68c0\u67e5Instance\u662f\u5426\u5df2\u7ecf\u4e3anull\u3002\u5982\u679cInstance\u4e0d\u4e3anull&#xff0c;\u8bf4\u660e\u5df2\u7ecf\u6709\u4e00\u4e2a\u5b9e\u4f8b\u88ab\u521b\u5efa\u4e86<br \/>\n        if (Instance !&#061; null)<br \/>\n        {<br \/>\n            Debug.Log(&#034;\u5df2\u7ecf\u6709\u4e00\u4e2a\u5b9e\u4f8b\u88ab\u521b\u5efa\u4e86&#034;);<br \/>\n        }<br \/>\n        \/\/\u5c06\u5f53\u524d\u5bf9\u8c61\u5b9e\u4f8b\u8d4b\u503c\u7ed9Instance&#xff0c;\u4f7f\u5176\u6210\u4e3a\u8be5\u7c7b\u7684\u552f\u4e00\u5b9e\u4f8b<br \/>\n        Instance &#061; this;<br \/>\n    }<br \/>\n}<br \/>\n\/\/\u8fd9\u79cd\u5b9e\u73b0\u65b9\u5f0f\u786e\u4fdd\u4e86Player\u7c7b\u5728\u6574\u4e2a\u6e38\u620f\u4f1a\u8bdd\u4e2d\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b&#xff0c;\u5e76\u4e14\u53ef\u4ee5\u901a\u8fc7Player.Instance\u8bbf\u95ee\u8fd9\u4e2a\u5b9e\u4f8b\u3002 <\/p>\n<p>\u6ce8\u610f\u70b9&#xff1a;\u00a0<\/p>\n<p>1.\u7acb\u5373\u521d\u59cb\u5316&#xff1a;\u5728\u7c7b\u88ab\u52a0\u8f7d\u65f6\u7acb\u5373\u521b\u5efa\u5355\u4f8b\u5bf9\u8c61&#xff0c;\u800c\u4e0d\u662f\u5728\u9996\u6b21\u4f7f\u7528\u65f6\u624d\u521b\u5efa\u3002<\/p>\n<p>2. \u7b80\u5355\u5b9e\u73b0&#xff1a;\u5b9e\u73b0\u7b80\u5355&#xff0c;\u4e0d\u9700\u8981\u540c\u6b65\u63a7\u5236&#xff0c;\u56e0\u4e3a\u5bf9\u8c61\u5728\u7b2c\u4e00\u6b21\u52a0\u8f7d\u65f6\u5c31\u88ab\u521b\u5efa\u3002<\/p>\n<p>3. \u8d44\u6e90\u5360\u7528&#xff1a;\u53ef\u80fd\u4f1a\u5728\u4e0d\u9700\u8981\u65f6\u5c31\u5360\u7528\u8d44\u6e90&#xff0c;\u56e0\u4e3a\u5bf9\u8c61\u5728\u7c7b\u52a0\u8f7d\u65f6\u5c31\u88ab\u521b\u5efa\u4e86\u3002<\/p>\n<p>2&#xff09;\u7ebf\u7a0b\u5b89\u5168\u5b9e\u73b0\u5355\u4f8b\u6a21\u5f0f&#xff08;<span style=\"color:red\">\u997f\u6c49\u6a21\u5f0f<\/span>&#xff09;<\/p>\n<p>\u786e\u4fdd\u5373\u4f7f\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d&#xff0c;Player \u7c7b\u4e5f\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b&#xff0c;\u5e76\u4e14\u8fd9\u4e2a\u5b9e\u4f8b\u662f\u5728\u7b2c\u4e00\u6b21\u88ab\u8bbf\u95ee\u65f6\u521b\u5efa\u7684&#xff08;\u61d2\u52a0\u8f7d&#xff09;\u3002<\/p>\n<p>public class Player : MonoBehaviour<br \/>\n{<br \/>\n    \/\/ \u79c1\u6709\u9759\u6001\u5b57\u6bb5&#xff0c;\u7528\u4e8e\u5b58\u50a8\u5355\u4f8b\u5b9e\u4f8b<br \/>\n    private static Player instance;<\/p>\n<p>    \/\/ \u516c\u5171\u9759\u6001\u5c5e\u6027&#xff0c;\u63d0\u4f9b\u5bf9\u5355\u4f8b\u7684\u5168\u5c40\u8bbf\u95ee\u70b9<br \/>\n    public static Player Instance<br \/>\n    {<br \/>\n        \/\/ \u5c5e\u6027\u7684get\u8bbf\u95ee\u5668&#xff0c;\u7528\u4e8e\u83b7\u53d6\u5355\u4f8b\u5b9e\u4f8b<br \/>\n        get<br \/>\n        {<br \/>\n            \/\/ \u5982\u679c\u5b9e\u4f8b\u4e3anull&#xff0c;\u5219\u8fdb\u5165\u9501\u5b9a\u4ee3\u7801\u5757<br \/>\n            if (instance &#061;&#061; null)<br \/>\n            {<br \/>\n                \/\/ \u4f7f\u7528\u9501\u786e\u4fdd\u7ebf\u7a0b\u5b89\u5168&#xff0c;\u9632\u6b62\u591a\u7ebf\u7a0b\u540c\u65f6\u521b\u5efa\u5b9e\u4f8b<br \/>\n                lock (typeof(Player))<br \/>\n                {<br \/>\n                    \/\/ \u518d\u6b21\u68c0\u67e5\u5b9e\u4f8b\u662f\u5426\u4e3anull&#xff0c;\u9632\u6b62\u591a\u7ebf\u7a0b\u521b\u5efa\u591a\u4e2a\u5b9e\u4f8b<br \/>\n                    if (instance &#061;&#061; null)<br \/>\n                    {<br \/>\n                        \/\/ \u521b\u5efaPlayer\u7c7b\u7684\u65b0\u5b9e\u4f8b<br \/>\n                        instance &#061; new Player();<br \/>\n                    }<br \/>\n                }<br \/>\n            }<br \/>\n            \/\/ \u8fd4\u56de\u5355\u4f8b\u5b9e\u4f8b<br \/>\n            return instance;<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ Unity\u751f\u547d\u5468\u671f\u65b9\u6cd5&#xff0c;\u5728\u5bf9\u8c61\u88ab\u521b\u5efa\u65f6\u8c03\u7528<br \/>\n    private void Awake()<br \/>\n    {<br \/>\n        \/\/ \u5982\u679c\u5df2\u7ecf\u5b58\u5728\u4e00\u4e2a\u5b9e\u4f8b&#xff0c;\u5e76\u4e14\u5f53\u524d\u5b9e\u4f8b\u4e0d\u662f\u90a3\u4e2a\u5b9e\u4f8b&#xff0c;\u5219\u9500\u6bc1\u5f53\u524d\u5b9e\u4f8b<br \/>\n        if (Instance !&#061; this)<br \/>\n        {<br \/>\n            Destroy(gameObject);<br \/>\n        }<br \/>\n        \/\/ \u5982\u679c\u5f53\u524d\u5b9e\u4f8b\u662f\u5355\u4f8b&#xff0c;\u5219\u9632\u6b62\u5b83\u5728\u52a0\u8f7d\u65b0\u573a\u666f\u65f6\u88ab\u9500\u6bc1<br \/>\n        else<br \/>\n        {<br \/>\n            DontDestroyOnLoad(gameObject);<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<h3 style=\"background-color:transparent\"><span style=\"color:#4d4d4d\"><span style=\"background-color:#ffffff\">2.\u61d2\u6c49\u5f0f&#xff08;Lazy Initialization&#xff09;\u5355\u4f8b\u6a21\u5f0f&#xff1a;<\/span><\/span><\/h3>\n<p><span style=\"color:#4d4d4d\"><span style=\"background-color:#ffffff\">\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u5355\u4f8b\u5b9e\u73b0\u65b9\u5f0f&#xff0c;\u5b83\u786e\u4fdd\u5355\u4f8b\u5bf9\u8c61\u5728\u9996\u6b21\u88ab\u8bbf\u95ee\u65f6\u624d\u521b\u5efa\u3002\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u8282\u7701\u8d44\u6e90&#xff0c;\u56e0\u4e3a\u5b83\u907f\u514d\u4e86\u5728\u7a0b\u5e8f\u542f\u52a8\u65f6\u7acb\u5373\u521b\u5efa\u5bf9\u8c61&#xff0c;\u800c\u662f\u5728\u771f\u6b63\u9700\u8981\u65f6\u624d\u521b\u5efa\u3002<\/span><\/span><\/p>\n<h4><span style=\"color:#4d4d4d\"><span style=\"background-color:#ffffff\">\u9002\u7528\u573a\u666f&#xff1a;<\/span><\/span><\/h4>\n<p><span style=\"color:#4d4d4d\"><span style=\"background-color:#ffffff\">\u5f53\u5bf9\u8c61\u53ef\u80fd\u4e0d\u88ab\u4f7f\u7528&#xff0c;\u6216\u521b\u5efa\u6210\u672c\u8f83\u9ad8\u65f6\u4f7f\u7528&#xff0c;\u9002\u5408\u4e8e\u53ef\u9009\u6216\u8d44\u6e90\u5bc6\u96c6\u578b\u529f\u80fd\u3002\u4f8b\u5b50&#xff1a;\u9ad8\u7ea7\u56fe\u5f62\u9009\u9879\u3001\u8c03\u8bd5\u5de5\u5177\u3001\u53ef\u9009\u6a21\u5757\u3002<\/span><\/span><\/p>\n<h4 style=\"background-color:transparent\">\u5b9e\u73b0\u65b9\u5f0f&#xff1a;<\/h4>\n<p>1&#xff09;\u5c5e\u6027\u5b9e\u73b0\u5355\u4f8b\u6a21\u5f0f&#xff08;<span style=\"color:#fe2c24\">\u61d2\u6c49<\/span><span style=\"color:red\">\u6a21\u5f0f<\/span>&#xff09;<\/p>\n<p>\u786e\u4fdd\u5355\u4f8b\u5bf9\u8c61\u5728\u9996\u6b21\u88ab\u8bbf\u95ee\u65f6\u624d\u521b\u5efa&#xff0c;\u4ece\u800c\u8282\u7701\u4e86\u8d44\u6e90\u3002\u540c\u65f6&#xff0c;\u7531\u4e8e\u5c5e\u6027\u7684\u8bbf\u95ee\u5668\u662f\u516c\u5171\u7684&#xff0c;\u6240\u4ee5\u53ef\u4ee5\u5728\u4efb\u4f55\u5730\u65b9\u901a\u8fc7\u00a0Singleton.Instance \u8bbf\u95ee\u5355\u4f8b\u3002<\/p>\n<p>using UnityEngine;<\/p>\n<p>public class Player : MonoBehaviour<br \/>\n{<br \/>\n    \/\/ \u79c1\u6709\u9759\u6001\u5b57\u6bb5&#xff0c;\u7528\u4e8e\u5b58\u50a8\u5355\u4f8b\u5b9e\u4f8b<br \/>\n    private static Player instance;<\/p>\n<p>    \/\/ \u516c\u5171\u9759\u6001\u5c5e\u6027&#xff0c;\u63d0\u4f9b\u5168\u5c40\u8bbf\u95ee\u70b9\u6765\u83b7\u53d6\u5355\u4f8b\u5b9e\u4f8b<br \/>\n    public static Player Instance<br \/>\n    {<br \/>\n        get<br \/>\n        {<br \/>\n            \/\/ \u5982\u679c\u5b9e\u4f8b\u4e3anull&#xff0c;\u5219\u521b\u5efa\u5b9e\u4f8b<br \/>\n            if (instance &#061;&#061; null)<br \/>\n            {<br \/>\n                instance &#061; new GameObject(&#034;Player&#034;).AddComponent&lt;Player&gt;();<br \/>\n            }<br \/>\n            return instance;<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ \u79c1\u6709\u6784\u9020\u51fd\u6570&#xff0c;\u9632\u6b62\u5916\u90e8\u5b9e\u4f8b\u5316<br \/>\n    private Player() { }<\/p>\n<p>    \/\/ Unity\u751f\u547d\u5468\u671f\u65b9\u6cd5&#xff0c;\u5728\u6e38\u620f\u5bf9\u8c61\u88ab\u521b\u5efa\u65f6\u81ea\u52a8\u8c03\u7528<br \/>\n    private void Awake()<br \/>\n    {<br \/>\n        \/\/ \u68c0\u67e5\u662f\u5426\u5df2\u7ecf\u5b58\u5728\u5b9e\u4f8b<br \/>\n        if (instance !&#061; null &amp;&amp; instance !&#061; this)<br \/>\n        {<br \/>\n            \/\/ \u5982\u679c\u5b58\u5728\u53e6\u4e00\u4e2a\u5b9e\u4f8b&#xff0c;\u5219\u9500\u6bc1\u5f53\u524d\u5b9e\u4f8b<br \/>\n            Destroy(gameObject);<br \/>\n        }<br \/>\n        else<br \/>\n        {<br \/>\n            \/\/ \u6807\u8bb0\u4e3aDontDestroyOnLoad&#xff0c;\u9632\u6b62\u5728\u52a0\u8f7d\u65b0\u573a\u666f\u65f6\u9500\u6bc1<br \/>\n            DontDestroyOnLoad(gameObject);<br \/>\n            instance &#061; this;<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<p>\u6ce8\u610f\u70b9&#xff1a;<\/p>\n<p>1. \u5ef6\u8fdf\u521d\u59cb\u5316&#xff1a;\u5355\u4f8b\u5bf9\u8c61\u5728\u9996\u6b21\u88ab\u8bbf\u95ee\u65f6\u521b\u5efa&#xff0c;\u800c\u4e0d\u662f\u5728\u7c7b\u52a0\u8f7d\u65f6\u7acb\u5373\u521b\u5efa\u3002<\/p>\n<p>2. \u8bbf\u95ee\u5ef6\u8fdf&#xff1a;\u7b2c\u4e00\u6b21\u8bbf\u95ee\u5355\u4f8b\u5c5e\u6027\u65f6\u53ef\u80fd\u4f1a\u6709\u8f7b\u5fae\u5ef6\u8fdf&#xff0c;\u56e0\u4e3a\u9700\u8981\u521b\u5efa\u5bf9\u8c61\u3002<\/p>\n<p>3. \u8d44\u6e90\u8282\u7701&#xff1a;\u53ea\u6709\u5728\u5b9e\u9645\u9700\u8981\u65f6\u624d\u521b\u5efa\u5bf9\u8c61&#xff0c;\u8282\u7701\u8d44\u6e90\u3002<\/p>\n<p>2&#xff09;\u7ebf\u7a0b\u5b89\u5168\u5b9e\u73b0\u5355\u4f8b\u6a21\u5f0f&#xff08;<span style=\"color:#fe2c24\">\u61d2\u6c49<\/span><span style=\"color:red\">\u6a21\u5f0f<\/span>&#xff09;<\/p>\n<p>\u4e3a\u4e86\u63d0\u9ad8\u7ebf\u7a0b\u5b89\u5168\u6027&#xff0c;\u53ef\u4ee5\u4f7f\u7528\u53cc\u91cd\u68c0\u67e5\u9501\u5b9a&#xff08;Double-Checked Locking&#xff09;\u6a21\u5f0f<\/p>\n<p>using UnityEngine;<\/p>\n<p>public class Player : MonoBehaviour<br \/>\n{<br \/>\n    \/\/ \u79c1\u6709\u9759\u6001\u5b57\u6bb5&#xff0c;\u7528\u4e8e\u5b58\u50a8\u5355\u4f8b\u5b9e\u4f8b<br \/>\n    private static Player instance;<\/p>\n<p>    \/\/ \u7528\u4e8e\u540c\u6b65\u7684\u5bf9\u8c61&#xff0c;\u786e\u4fdd\u7ebf\u7a0b\u5b89\u5168<br \/>\n    private static readonly object lockObject &#061; new object();<\/p>\n<p>    \/\/ \u516c\u5171\u9759\u6001\u5c5e\u6027&#xff0c;\u63d0\u4f9b\u5168\u5c40\u8bbf\u95ee\u70b9\u6765\u83b7\u53d6\u5355\u4f8b\u5b9e\u4f8b<br \/>\n    public static Player Instance<br \/>\n    {<br \/>\n        get<br \/>\n        {<br \/>\n            \/\/ \u5982\u679c\u5b9e\u4f8b\u4e3anull&#xff0c;\u5219\u8fdb\u5165\u9501\u5b9a\u4ee3\u7801\u5757<br \/>\n            if (instance &#061;&#061; null)<br \/>\n            {<br \/>\n                lock (lockObject)<br \/>\n                {<br \/>\n                    \/\/ \u518d\u6b21\u68c0\u67e5\u5b9e\u4f8b\u662f\u5426\u4e3anull&#xff0c;\u9632\u6b62\u591a\u7ebf\u7a0b\u521b\u5efa\u591a\u4e2a\u5b9e\u4f8b<br \/>\n                    if (instance &#061;&#061; null)<br \/>\n                    {<br \/>\n                        \/\/ \u521b\u5efaPlayer\u7c7b\u7684\u65b0\u5b9e\u4f8b<br \/>\n                        instance &#061; new GameObject(&#034;Player&#034;).AddComponent&lt;Player&gt;();<br \/>\n                    }<br \/>\n                }<br \/>\n            }<br \/>\n            \/\/ \u8fd4\u56de\u5355\u4f8b\u5b9e\u4f8b<br \/>\n            return instance;<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ \u79c1\u6709\u6784\u9020\u51fd\u6570&#xff0c;\u9632\u6b62\u5916\u90e8\u5b9e\u4f8b\u5316<br \/>\n    private Player() { }<\/p>\n<p>    \/\/ Unity\u751f\u547d\u5468\u671f\u65b9\u6cd5&#xff0c;\u5728\u6e38\u620f\u5bf9\u8c61\u88ab\u521b\u5efa\u65f6\u81ea\u52a8\u8c03\u7528<br \/>\n    private void Awake()<br \/>\n    {<br \/>\n        \/\/ \u68c0\u67e5\u662f\u5426\u5df2\u7ecf\u5b58\u5728\u5b9e\u4f8b<br \/>\n        if (instance !&#061; null &amp;&amp; instance !&#061; this)<br \/>\n        {<br \/>\n            \/\/ \u5982\u679c\u5b58\u5728\u53e6\u4e00\u4e2a\u5b9e\u4f8b&#xff0c;\u5219\u9500\u6bc1\u5f53\u524d\u5b9e\u4f8b<br \/>\n            Destroy(gameObject);<br \/>\n        }<br \/>\n        else<br \/>\n        {<br \/>\n            \/\/ \u5982\u679c\u4e0d\u5b58\u5728\u5b9e\u4f8b&#xff0c;\u5219\u8c03\u7528DontDestroyOnLoad&#xff0c;\u9632\u6b62\u5728\u52a0\u8f7d\u65b0\u573a\u666f\u65f6\u9500\u6bc1<br \/>\n            DontDestroyOnLoad(gameObject);<br \/>\n            \/\/ \u5c06\u5f53\u524d\u5b9e\u4f8b\u8d4b\u503c\u7ed9\u5355\u4f8b<br \/>\n            instance &#061; this;<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb857\u6b21\uff0c\u70b9\u8d5e19\u6b21\uff0c\u6536\u85cf24\u6b21\u3002\u5728Unity\u4e2d\uff0c\u5e8f\u5217\u5316\u5b57\u6bb5\u548c\u5355\u4f8b\u6a21\u5f0f\u662f\u4e24\u4e2a\u91cd\u8981\u7684\u6982\u5ff5\u3002\u5e8f\u5217\u5316\u5b57\u6bb5\u901a\u8fc7[SerializeField]\u5c5e\u6027\u5141\u8bb8\u79c1\u6709\u6216\u53d7\u4fdd\u62a4\u7684\u5b57\u6bb5\u5728Unity\u7f16\u8f91\u5668\u7684Inspector\u7a97\u53e3\u4e2d\u663e\u793a\u548c\u7f16\u8f91\uff0c\u540c\u65f6\u652f\u6301\u5bf9\u8c61\u72b6\u6001\u7684\u4fdd\u5b58\u548c\u52a0\u8f7d\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u516c\u5171\u5b57\u6bb5\u4f1a\u88ab\u5e8f\u5217\u5316\uff0c\u800c\u79c1\u6709\u5b57\u6bb5\u3001\u9759\u6001\u5b57\u6bb5\u548c\u53ea\u8bfb\u5b57\u6bb5\u5219\u4e0d\u4f1a\u3002\u5e8f\u5217\u5316\u5b57\u6bb5\u9002\u7528\u4e8e\u7ec4\u4ef6\u5c5e\u6027\u7f16\u8f91\u3001\u6e38\u620f\u72b6\u6001\u4fdd\u5b58\u4e0e\u52a0\u8f7d\u4ee5\u53ca\u7f51\u7edc\u540c\u6b65\u7b49\u573a\u666f\u3002\u5355\u4f8b\u6a21\u5f0f\u5219\u786e\u4fdd\u7c7b\u5728\u7a0b\u5e8f\u8fd0\u884c\u671f\u95f4\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u5e76\u63d0\u4f9b\u5168\u5c40\u8bbf\u95ee\u70b9\u3002\u5355\u4f8b\u6a21\u5f0f\u5206\u4e3a\u997f\u6c49\u5f0f\u548c\u61d2\u6c49\u5f0f\uff0c\u997f\u6c49\u5f0f\u5728\u7c7b\u52a0\u8f7d\u65f6\u7acb\u5373\u521b\u5efa\u5b9e\u4f8b\uff0c\u9002\u5408\u6838\u5fc3\u529f\u80fd\uff1b\u61d2\u6c49\u5f0f\u5728\u9996\u6b21\u8bbf\u95ee\u65f6\u521b\u5efa\u5b9e\u4f8b\uff0c<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[55,183,2246],"topic":[],"class_list":["post-38360","post","type-post","status-publish","format-standard","hentry","category-server","tag-c","tag-unity","tag-2246"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Unity\u5e8f\u5217\u5316\u5b57\u6bb5\u3001\u5355\u4f8b\u6a21\u5f0f(Singleton Pattern) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/38360.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Unity\u5e8f\u5217\u5316\u5b57\u6bb5\u3001\u5355\u4f8b\u6a21\u5f0f(Singleton Pattern) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb857\u6b21\uff0c\u70b9\u8d5e19\u6b21\uff0c\u6536\u85cf24\u6b21\u3002\u5728Unity\u4e2d\uff0c\u5e8f\u5217\u5316\u5b57\u6bb5\u548c\u5355\u4f8b\u6a21\u5f0f\u662f\u4e24\u4e2a\u91cd\u8981\u7684\u6982\u5ff5\u3002\u5e8f\u5217\u5316\u5b57\u6bb5\u901a\u8fc7[SerializeField]\u5c5e\u6027\u5141\u8bb8\u79c1\u6709\u6216\u53d7\u4fdd\u62a4\u7684\u5b57\u6bb5\u5728Unity\u7f16\u8f91\u5668\u7684Inspector\u7a97\u53e3\u4e2d\u663e\u793a\u548c\u7f16\u8f91\uff0c\u540c\u65f6\u652f\u6301\u5bf9\u8c61\u72b6\u6001\u7684\u4fdd\u5b58\u548c\u52a0\u8f7d\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u516c\u5171\u5b57\u6bb5\u4f1a\u88ab\u5e8f\u5217\u5316\uff0c\u800c\u79c1\u6709\u5b57\u6bb5\u3001\u9759\u6001\u5b57\u6bb5\u548c\u53ea\u8bfb\u5b57\u6bb5\u5219\u4e0d\u4f1a\u3002\u5e8f\u5217\u5316\u5b57\u6bb5\u9002\u7528\u4e8e\u7ec4\u4ef6\u5c5e\u6027\u7f16\u8f91\u3001\u6e38\u620f\u72b6\u6001\u4fdd\u5b58\u4e0e\u52a0\u8f7d\u4ee5\u53ca\u7f51\u7edc\u540c\u6b65\u7b49\u573a\u666f\u3002\u5355\u4f8b\u6a21\u5f0f\u5219\u786e\u4fdd\u7c7b\u5728\u7a0b\u5e8f\u8fd0\u884c\u671f\u95f4\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u5e76\u63d0\u4f9b\u5168\u5c40\u8bbf\u95ee\u70b9\u3002\u5355\u4f8b\u6a21\u5f0f\u5206\u4e3a\u997f\u6c49\u5f0f\u548c\u61d2\u6c49\u5f0f\uff0c\u997f\u6c49\u5f0f\u5728\u7c7b\u52a0\u8f7d\u65f6\u7acb\u5373\u521b\u5efa\u5b9e\u4f8b\uff0c\u9002\u5408\u6838\u5fc3\u529f\u80fd\uff1b\u61d2\u6c49\u5f0f\u5728\u9996\u6b21\u8bbf\u95ee\u65f6\u521b\u5efa\u5b9e\u4f8b\uff0c\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/38360.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-05-20T01:18:21+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/38360.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/38360.html\",\"name\":\"Unity\u5e8f\u5217\u5316\u5b57\u6bb5\u3001\u5355\u4f8b\u6a21\u5f0f(Singleton Pattern) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-05-20T01:18:21+00:00\",\"dateModified\":\"2025-05-20T01:18:21+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/38360.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/38360.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/38360.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Unity\u5e8f\u5217\u5316\u5b57\u6bb5\u3001\u5355\u4f8b\u6a21\u5f0f(Singleton Pattern)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Unity\u5e8f\u5217\u5316\u5b57\u6bb5\u3001\u5355\u4f8b\u6a21\u5f0f(Singleton Pattern) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/38360.html","og_locale":"zh_CN","og_type":"article","og_title":"Unity\u5e8f\u5217\u5316\u5b57\u6bb5\u3001\u5355\u4f8b\u6a21\u5f0f(Singleton Pattern) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb857\u6b21\uff0c\u70b9\u8d5e19\u6b21\uff0c\u6536\u85cf24\u6b21\u3002\u5728Unity\u4e2d\uff0c\u5e8f\u5217\u5316\u5b57\u6bb5\u548c\u5355\u4f8b\u6a21\u5f0f\u662f\u4e24\u4e2a\u91cd\u8981\u7684\u6982\u5ff5\u3002\u5e8f\u5217\u5316\u5b57\u6bb5\u901a\u8fc7[SerializeField]\u5c5e\u6027\u5141\u8bb8\u79c1\u6709\u6216\u53d7\u4fdd\u62a4\u7684\u5b57\u6bb5\u5728Unity\u7f16\u8f91\u5668\u7684Inspector\u7a97\u53e3\u4e2d\u663e\u793a\u548c\u7f16\u8f91\uff0c\u540c\u65f6\u652f\u6301\u5bf9\u8c61\u72b6\u6001\u7684\u4fdd\u5b58\u548c\u52a0\u8f7d\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u516c\u5171\u5b57\u6bb5\u4f1a\u88ab\u5e8f\u5217\u5316\uff0c\u800c\u79c1\u6709\u5b57\u6bb5\u3001\u9759\u6001\u5b57\u6bb5\u548c\u53ea\u8bfb\u5b57\u6bb5\u5219\u4e0d\u4f1a\u3002\u5e8f\u5217\u5316\u5b57\u6bb5\u9002\u7528\u4e8e\u7ec4\u4ef6\u5c5e\u6027\u7f16\u8f91\u3001\u6e38\u620f\u72b6\u6001\u4fdd\u5b58\u4e0e\u52a0\u8f7d\u4ee5\u53ca\u7f51\u7edc\u540c\u6b65\u7b49\u573a\u666f\u3002\u5355\u4f8b\u6a21\u5f0f\u5219\u786e\u4fdd\u7c7b\u5728\u7a0b\u5e8f\u8fd0\u884c\u671f\u95f4\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u5e76\u63d0\u4f9b\u5168\u5c40\u8bbf\u95ee\u70b9\u3002\u5355\u4f8b\u6a21\u5f0f\u5206\u4e3a\u997f\u6c49\u5f0f\u548c\u61d2\u6c49\u5f0f\uff0c\u997f\u6c49\u5f0f\u5728\u7c7b\u52a0\u8f7d\u65f6\u7acb\u5373\u521b\u5efa\u5b9e\u4f8b\uff0c\u9002\u5408\u6838\u5fc3\u529f\u80fd\uff1b\u61d2\u6c49\u5f0f\u5728\u9996\u6b21\u8bbf\u95ee\u65f6\u521b\u5efa\u5b9e\u4f8b\uff0c","og_url":"https:\/\/www.wsisp.com\/helps\/38360.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-05-20T01:18:21+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"3 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/38360.html","url":"https:\/\/www.wsisp.com\/helps\/38360.html","name":"Unity\u5e8f\u5217\u5316\u5b57\u6bb5\u3001\u5355\u4f8b\u6a21\u5f0f(Singleton Pattern) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-05-20T01:18:21+00:00","dateModified":"2025-05-20T01:18:21+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/38360.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/38360.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/38360.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Unity\u5e8f\u5217\u5316\u5b57\u6bb5\u3001\u5355\u4f8b\u6a21\u5f0f(Singleton Pattern)"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/38360","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=38360"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/38360\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=38360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=38360"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=38360"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=38360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}