{"id":1742,"date":"2017-04-15T16:19:13","date_gmt":"2017-04-15T08:19:13","guid":{"rendered":"http:\/\/www.clonefactor.com\/wordpress\/?p=1742"},"modified":"2017-04-15T16:19:13","modified_gmt":"2017-04-15T08:19:13","slug":"unity3d-onvalidate-reset-%e8%b3%87%e6%ba%90%e6%aa%a2%e6%b8%ac","status":"publish","type":"post","link":"https:\/\/www.clonefactor.com\/wordpress\/program\/unity3d\/1742\/","title":{"rendered":"Unity3D, OnValidate &#038; Reset \u8cc7\u6e90\u6aa2\u6e2c"},"content":{"rendered":"<p>\u5728\u958b\u767c\u6642\u7d93\u5e38\u9700\u8981\u9032\u884c\u5927\u91cf\u8cc7\u6e90\u7ba1\u7406, \u5728\u7de8\u5beb\u65b0\u7a0b\u5e8f\u53ca\u9032\u884c Prefab \u5efa\u69cb\u6642\u9019\u554f\u984c\u4ee4\u958b\u767c\u8005\u975e\u5e38\u82e6\u60f1.<\/p>\n<p>e.g. \u5efa\u7acb\u4e00\u500b\u591a\u5c64\u67b6\u69cb\u7684 Prefab, \u628a\u6240\u6709\u5728 ScrollView\u00a0\u5e95\u4e0b\u7684 MyListElement \u90fd\u653e\u5230\u00a0Array\/List \u7684\u52d5\u4f5c..<\/p>\n<p>\u9019\u4e9b\u5927\u91cf Drag &amp; Drop \u7684\u52d5\u4f5c\u591a\u505a\u6578\u6b21\u6703\u4ee4\u4eba\u767c\u760b.<br \/>\n\u9019\u6642\u5019 OnValidate \u5c31\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u6e1b\u5c11\u9019\u4e9b\u7121\u8b02\u7684 setup \u52d5\u4f5c.<br \/>\n\u4ee5 UGUI \u7684 Button \u8209\u4f8b.<\/p>\n<p>ButtonLabel.cs<\/p>\n<pre class=\"brush:csharp\">using UnityEngine;\r\nusing UnityEngine.UI;\r\npublic class ButtonLabel : MonoBehaviour\r\n{\r\n\tpublic Text m_Label;\r\n\tpublic Button m_Button;\r\n\r\n\tprivate void OnValidate()\r\n\t{\r\n\t\tif (m_Label == null)\r\n\t\t\tm_Label = GetComponentInChildren&lt;Text&gt;(true);\r\n\r\n\t\tif (m_Button == null)\r\n\t\t\tm_Button = GetComponentInChildren&lt;Button&gt;(true);\r\n\t}\r\n}<\/pre>\n<p>\u7576\u9019\u500b ButtonLabel \u52a0\u5165\u5230 Button \u4e0a\u6642\u5019, \u6211\u5011\u53ea\u8981\u628a\u5176\u4e2d\u4e00\u500b field \u66f4\u52d5\u4e00\u4e0b { \u653e\u5165 Text } \u90a3\u9ebc Button + Label \u6703\u4e00\u8d77\u81ea\u52d5\u627e\u53d6\u5e95\u4e0b\u7b2c\u4e00\u9806\u4f4d\u7684 Text \u8ddf Button.<br \/>\n\u4f46\u9019\u500b\u5c0d\u65bc\u61f6\u6563\u6163\u4e86\u7684\u7a0b\u5e8f\u54e1\u662f\u4e0d\u8db3\u5920\u7684.<br \/>\n\u6211\u5011\u5e0c\u671b\u9019\u500b\u52d5\u4f5c\u5728 ButtonLabel \u4e00\u958b\u59cb\u653e\u5230 GameObject \u8eab\u4e0a\u6642\u5c31\u81ea\u52d5\u57f7\u884c\u4e00\u6b21.<br \/>\n\u5728 Component \u653e\u5230 GameObject \u8eab\u4e0a\u6642\u6703\u81ea\u52d5\u8dd1\u7684 method \u662f Reset();<br \/>\n\u6240\u4ee5\u6211\u5011\u4fee\u6539\u5982\u4e0b:<\/p>\n<pre class=\"brush:csharp\">public class ButtonLabel : MonoBehaviour\r\n{\r\n\tpublic Text m_Label;\r\n\tpublic Button m_Button;\r\n\r\n\tprivate void Reset()\r\n\t{\r\n\t\tAutoConfig();\r\n\t}\r\n\r\n\tprivate void OnValidate()\r\n\t{\r\n\t\tAutoConfig();\r\n\t}\r\n\r\n\tprivate void AutoConfig()\r\n\t{\r\n\t\tif (m_Label == null)\r\n\t\t\tm_Label = GetComponentInChildren&lt;Text&gt;(true);\r\n\r\n\t\tif (m_Button == null)\r\n\t\t\tm_Button = GetComponentInChildren&lt;Button&gt;(true);\r\n\t}\r\n}<\/pre>\n<p>\u9996\u5148 Reset() \u5728\u958b\u767c\u8005\u91cd\u8a2d Component \u6642\u6703\u88ab\u547c\u53eb,<br \/>\n\u800c OnValidate() \u5728 &#8220;<strong>\u4efb\u4f55<\/strong>&#8221; \u66f4\u6539\u6642\u4e5f\u6703\u88ab\u547c\u53eb\u4e00\u6b21, \u9019\u5305\u62ec\u7de8\u8b6f\u968e\u6bb5. \u9019\u9593\u63a5\u5c0e\u81f4\u00a0AutoConfig \u5728\u6bcf\u6b21\u7de8\u8b6f\u6642\u6642\u4e5f\u88ab\u547c\u53eb.<br \/>\n\u55ae\u55ae\u4e00\u5169\u500b OnValidate \u6c92\u554f\u592a\u5927\u8ca0\u64d4\u4f46\u60f3\u50cf\u4e00\u4e0b\u4f60\u7684\u5c08\u6848\u6709\u6578\u5343\u500b OnValidate \u7684\u5143\u4ef6\u6bcf\u4e00\u500b\u5728 \u7de8\u8b6f\u6642\u4e5f\u88ab\u57f7\u884c&#8230;&#8230;..<br \/>\n\u9019\u500b\u6e2c\u8a66\u6703\u4ee4 CPU \u6d6a\u8cbb\u5f88\u591a\u975e\u5fc5\u8981\u7684\u6aa2\u6e2c\u8cc7\u6e90.<\/p>\n<p>\u9019\u500b\u6642\u5019\u5c31\u9700\u8981\u7528\u5230\u53e6\u4e00\u500b\u6aa2\u6e2c\u624b\u6bb5.<\/p>\n<pre class=\"brush:csharp\">UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode<\/pre>\n<p>\u7528\u4f86\u5f97\u77e5\u662f\u5426\u6b63\u5728\u7de8\u8b6f, \u6b63\u5728\u7de8\u8b6f\u5c31\u5225\u9032\u884c\u4e86, \u56e0\u70ba\u6211\u5011\u5047\u8a2d\u5728 Reset \u968e\u6bb5\u5df2\u7d93\u6aa2\u6e2c\u904e\u4e86\u53ca\u8a2d\u5b9a\u597d\u4e86.<br \/>\n\u5c31\u5225\u6d6a\u8cbb\u6bcf\u6b21\u5c0d\u884c Playmode \u6e2c\u8a66\u7684\u7de8\u8b6f\u8cc7\u6e90.<\/p>\n<pre class=\"brush:csharp\">using UnityEngine;\r\nusing UnityEngine.UI;\r\n\r\npublic class ButtonLabel : MonoBehaviour\r\n{\r\n\tpublic Text m_Label;\r\n\tpublic Button m_Button;\r\n\r\n\tprivate void Reset()\r\n\t{\r\n\t\tAutoConfig();\r\n\t}\r\n\r\n\tprivate void OnValidate()\r\n\t{\r\n#if UNITY_EDITOR\r\n\t\tif (!UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode)\r\n\t\t\treturn;\r\n#endif\r\n\t\tif (!Application.isPlaying)\r\n\t\t\tAutoConfig();\r\n\t}\r\n\r\n\tprivate void AutoConfig()\r\n\t{\r\n\t\tif (m_Label == null)\r\n\t\t\tm_Label = GetComponentInChildren&lt;Text&gt;(true);\r\n\r\n\t\tif (m_Button == null)\r\n\t\t\tm_Button = GetComponentInChildren&lt;Button&gt;(true);\r\n\t}\r\n}<\/pre>\n<p>\u56e0\u70ba EditorApplication \u53ea\u80fd\u5728 UnityEditor \u4e0b\u624d\u80fd\u53d6\u5f97. \u6240\u4ee5\u7a0b\u5e8f\u53c8\u8b8a\u5f97\u64c1\u64e0\u4e86.<br \/>\n\u7576\u7136\u6709\u5f88\u591a\u624b\u6cd5\u4ee4\u9019\u4e9b\u6aa2\u6e2c\u8b8a\u5f97\u7d71\u4e00\u53ca\u7c21\u5316, \u4f46\u5404\u4eba\u6709\u4e0d\u540c\u65b9\u5f0f, \u9019\u4e0d\u5728\u672c\u7bc7\u8a0e\u8ad6\u7bc4\u570d.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u958b\u767c\u6642\u7d93\u5e38\u9700\u8981\u9032\u884c\u5927\u91cf\u8cc7\u6e90\u7ba1\u7406, \u5728\u7de8\u5beb\u65b0\u7a0b\u5e8f\u53ca\u9032\u884c Prefab \u5efa\u69cb\u6642\u9019\u554f\u984c\u4ee4\u958b\u767c\u8005\u975e\u5e38\u82e6\u60f1. &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[20,43],"class_list":["post-1742","post","type-post","status-publish","format-standard","hentry","category-unity3d","tag-editor","tag-unity3d-2"],"_links":{"self":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts\/1742","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/comments?post=1742"}],"version-history":[{"count":0,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts\/1742\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/media?parent=1742"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/categories?post=1742"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/tags?post=1742"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}