{"id":7136,"date":"2024-04-13T21:01:01","date_gmt":"2024-04-13T13:01:01","guid":{"rendered":""},"modified":"2024-04-13T21:01:01","modified_gmt":"2024-04-13T13:01:01","slug":"Unity\u7684RuntimeInitializeOnLoadMethod\u5c5e\u6027\u521d\u63a2","status":"publish","type":"post","link":"https:\/\/mushiming.com\/7136.html","title":{"rendered":"Unity\u7684RuntimeInitializeOnLoadMethod\u5c5e\u6027\u521d\u63a2"},"content":{"rendered":"

Unity\u7684RuntimeInitializeOnLoadMethod\u5c5e\u6027\u521d\u63a2<\/h2>\n

Unity 5.0\u5f00\u59cb\u589e\u52a0\u4e86RuntimeInitializeOnLoadMethodAttribute\uff0c\u8fd9\u6837\u5c31\u5f88\u65b9\u4fbf\u5728\u6e38\u620f\u521d\u59cb\u5316\u4e4b\u524d\u505a\u4e00\u4e9b\u989d\u5916\u7684\u521d\u59cb\u5316\u5de5\u4f5c\uff0c\u6bd4\u5982\uff1aBulgy\u53c2\u6570\u8bbe\u7f6e\u3001SDK\u521d\u59cb\u7b49\u5de5\u4f5c\u3002<\/p>\n

 <\/p>\n

\u5148\u6765\u770b\u4e00\u4e0b\u5b83\u7684\u751f\u547d\u5468\u671f<\/p>\n

\"Unity\u7684RuntimeInitializeOnLoadMethod\u5c5e\u6027\u521d\u63a2<\/p>\n

sing UnityEngine;\n\npublic class ExampleClass : MonoBehaviour\n{\n    private void Awake()\n    {\n        Debug.Log( \"Awake\" );\n    }\n\n    private void OnEnable()\n    {\n        Debug.Log( \"OnEnable\" );\n    }\n\n    private void Start()\n    {\n        Debug.Log( \"Start\" );\n    }\n\n    [RuntimeInitializeOnLoadMethod]\n    private static void OnRuntimeMethodLoad()\n    {\n        Debug.Log( \"RuntimeInitializeOnLoadMethod\" );\n    }\n}<\/pre>\n

\"Unity\u7684RuntimeInitializeOnLoadMethod\u5c5e\u6027\u521d\u63a2<\/p>\n

\u8f93\u51fa\u7ed3\u679c<\/p>\n

\"Unity\u7684RuntimeInitializeOnLoadMethod\u5c5e\u6027\u521d\u63a2<\/p>\n

 <\/p>\n

\u800c\u5b83\u8fd8\u6709\u4e24\u4e2a\u5c5e\u6027\uff1aRuntimeInitializeLoadType.BeforeSceneLoad  | RuntimeInitializeLoadType.AfterSceneLoad<\/p>\n

\"Unity\u7684RuntimeInitializeOnLoadMethod\u5c5e\u6027\u521d\u63a2<\/p>\n

using UnityEngine;\n\npublic class InitTest  : MonoBehaviour\n{\n    [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]\n    static void OnBeforeSceneLoadRuntimeMethod ()\n    {\n        Debug.Log(\"Before scene loaded\");\n    }\n\n    void Awake()\n    {\n        Debug.Log(\"Awake\");\n    }\n    void OnEnable()\n    {\n        Debug.Log(\"OnEnable\");\n    }\n\n    [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]\n    static void OnAfterSceneLoadRuntimeMethod()\n    {\n        Debug.Log(\"After scene loaded\");\n    }\n\n    [RuntimeInitializeOnLoadMethod]\n    static void OnRuntimeMethodLoad()\n    {\n        Debug.Log(\"RuntimeMethodLoad: After scene loaded\");\n    }\n\n    void Start()\n    {\n        Debug.Log(\"Start\");\n    }\n\n}<\/pre>\n

\"Unity\u7684RuntimeInitializeOnLoadMethod\u5c5e\u6027\u521d\u63a2<\/p>\n

\u8f93\u51fa\u7ed3\u679c<\/p>\n

\"Unity\u7684RuntimeInitializeOnLoadMethod\u5c5e\u6027\u521d\u63a2<\/p>\n

 <\/p>\n

Before \u2013> Awake \u2013> OnEnable \u2013> After \u2013> RuntimeMethodLoad \u2013> Start\u3002<\/p>\n

 <\/p>\n

\u9644\u4e00\u5f20\u811a\u672c\u7684\u751f\u547d\u5468\u671f\u56fe\uff08\u6765\u6e90\uff1a\u5b98\u65b9\u7f51\u7ad9\uff09<\/p>\n

\"Unity\u7684RuntimeInitializeOnLoadMethod\u5c5e\u6027\u521d\u63a2<\/p>\n

 <\/p>\n

 <\/p>\n

Unity\u4e2d\u63d0\u4f9b\u7684Attribute\u6709\u5f88\u591a\uff0cRuntimeInitializeOnLoadMethodAttribute\u53ea\u662f\u5176\u4e2d\u7684\u4e00\u79cd\u3002\u5982\u679c\u81ea\u5df1\u5199\u7a0b\u5e8f\u6269\u5c55\u7f16\u8f91\u5668\u7684\u529f\u80fd\uff0c\u5c31\u9700\u8981\u4e86\u89e3\u8fd9\u4e9b\u5c5e\u6027\u3002\u5e38\u7528\u7684\u6709\uff1a<\/p>\n

1\u3001AddComponentMenu \u5bfc\u822a\u680f\u83dc\u5355<\/p>\n

2\u3001ContextMenu \u53f3\u952e\u83dc\u5355<\/p>\n

3\u3001HeaderAttribute<\/p>\n

4\u3001HideInInspector \u53ef\u4ee5\u8ba9public\u53d8\u91cf\u5728Inspector\u4e0a\u9690\u85cf\uff0c\u65e0\u6cd5\u5728Editor\u4e2d\u8fdb\u884c\u7f16\u8f91<\/p>\n

5\u3001MultilineAttribute \u652f\u6301\u8f93\u5165\u591a\u884c\u6587\u672c<\/p>\n

6\u3001RangeAttribute \u9650\u5b9a\u8f93\u5165\u503c\u7684\u8303\u56f4<\/p>\n

7\u3001RequireComponent \u7ec4\u4ef6\u4f9d\u8d56\uff0c\u4f7f\u7528\u8be5\u7ec4\u4ef6\u540e\u81ea\u52a8\u6dfb\u52a0\u4f9d\u8d56\u7ec4\u4ef6<\/p>\n

8\u3001RuntimeInitializeOnLoadMethodAttribute<\/p>\n

9\u3001SerializeField \u5f3a\u5236\u5bf9\u53d8\u91cf\u8fdb\u884c\u5e8f\u5217\u5316\uff0c\u5373\u4f7f\u53d8\u91cf\u662fprivate<\/p>\n

10\u3001SpaceAttribute \u589e\u52a0\u7a7a\u4f4d<\/p>\n

11\u3001TooltipAttribute \u63d0\u793a\u4fe1\u606f\uff0c\u5f53\u9f20\u6807\u79fb\u5230Inspector\u4e0a\u65f6\u663e\u793a\u76f8\u5e94\u7684\u63d0\u793a<\/p>\n

12\u3001InitializeOnLoadAttribute<\/p>\n

13\u3001InitializeOnLoadMethodAttribute<\/p>\n

14\u3001MenuItem \u5bfc\u822a\u680f\u7684\u83dc\u5355\u9879<\/p>\n

 <\/p>\n","protected":false},"excerpt":{"rendered":"Unity\u7684RuntimeInitializeOnLoadMethod\u5c5e\u6027\u521d\u63a2Unity\u7684RuntimeInitializeOnLoadMethod\u5c5e\u6027\u521d\u63a2Unity5.0\u5f00\u59cb\u589e\u52a0\u4e86RuntimeInit...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/7136"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=7136"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/7136\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=7136"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=7136"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=7136"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}