{"id":4974,"date":"2023-07-20T14:42:16","date_gmt":"2023-07-20T06:42:16","guid":{"rendered":"http:\/\/blog.coolcoding.cn\/?p=4974"},"modified":"2023-07-20T14:43:19","modified_gmt":"2023-07-20T06:43:19","slug":"unity%e4%b8%admonobehaviour%e7%9a%84%e6%9e%84%e9%80%a0%e4%b8%8e%e6%9e%90%e6%9e%84%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/blog.coolcoding.cn\/?p=4974","title":{"rendered":"Unity\u4e2dMonoBehaviour\u7684\u6784\u9020\u4e0e\u6790\u6784\u95ee\u9898"},"content":{"rendered":"<p>\u4ee5\u4e0b\u4e3a\u793a\u4f8b\u4ee3\u7801<\/p>\n<pre><code class=\"language-c#\">public class MyClass: MonoBehaviour\n{\n  MyClass() {}\n  ~MyClass() {}\n\n  void Start() {}\n  void OnDestroy() {}\n\n  void OnEnable() {}\n  void OnDisable() {}\n}<\/code><\/pre>\n<p>\u5751\uff1a<br \/>\n1\u3001MyClass()\u548c~MyClass()\u7684\u8c03\u7528\u5e76\u4e0d\u662f\u5728\u4e3b\u7ebf\u7a0b\u4e2d\uff0c\u4e0d\u80fd\u8c03\u7528\u4efb\u4f55UnityAPI<br \/>\n2\u3001~MyClass()\u7684\u8c03\u7528\u65f6\u673a\u662f\u4e0d\u786e\u5b9a\u7684\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u8c03\u7528\u4e86UnityEngine.Object.Destroy(obj) \u4e4b\u540e\uff0c\u5e76\u4e0d\u4f1a\u7acb\u5373\u89e6\u53d1 ~MyClass(), \u6709\u53ef\u80fd\u4e00\u76f4\u4e0d\u89e6\u53d1<br \/>\n3\u3001\u82e5\u6302\u8f7d\u7684GameObject\u72b6\u6001\u4e3aUnactive\uff0c\u5219\u8c03\u7528\u4e86UnityEngine.Object.Destroy(obj) \u4e4b\u540e\uff0c\u4e0d\u4f1a\u89e6\u53d1OnDestroy<br \/>\n4\u3001\u6240\u4ee5\uff0c\u5982\u679c\u8981\u6210\u5bf9\u4f7f\u7528\uff0c\u552f\u4e00\u63a8\u8350\u7684\u5339\u914d\u5bf9\u4e3a\uff1aOnEnale \/ OnDisale<br \/>\n5\u3001OnEnable \u548c OnDisale \u53ef\u80fd\u4f1a\u88ab\u591a\u6b21\u89e6\u53d1\uff0c\u6240\u4ee5\u8981\u5c0f\u5fc3\uff0c\u53c8\u662f\u5751<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ee5\u4e0b\u4e3a\u793a\u4f8b\u4ee3\u7801 public class MyClass: MonoBehaviour { MyClass() [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/4974"}],"collection":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4974"}],"version-history":[{"count":2,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/4974\/revisions"}],"predecessor-version":[{"id":4976,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/4974\/revisions\/4976"}],"wp:attachment":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4974"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4974"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4974"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}