{"id":186,"date":"2020-12-15T16:22:40","date_gmt":"2020-12-15T08:22:40","guid":{"rendered":"https:\/\/www.blinkedu.cn\/?p=186"},"modified":"2020-12-15T16:22:40","modified_gmt":"2020-12-15T08:22:40","slug":"unity%e4%b8%ad%e5%ae%9e%e7%8e%b0%e9%9b%b7%e8%be%be%e5%b1%9e%e6%80%a7%e5%9b%be","status":"publish","type":"post","link":"https:\/\/www.blinkedu.cn\/index.php\/2020\/12\/15\/unity%e4%b8%ad%e5%ae%9e%e7%8e%b0%e9%9b%b7%e8%be%be%e5%b1%9e%e6%80%a7%e5%9b%be\/","title":{"rendered":"Unity\u4e2d\u5b9e\u73b0\u96f7\u8fbe\u5c5e\u6027\u56fe"},"content":{"rendered":"<h2>\u6700\u7ec8\u6548\u679c<\/h2>\n<figure id=\"attachment_mmd_187\" class=\"wp-block-image \"><img width=\"1703\" height=\"603\" src=\"https:\/\/www.blinkedu.cn\/wp-content\/uploads\/2020\/12\/GIF.gif\" class=\"attachment-full size-full\" alt=\"\u300aUnity\u4e2d\u5b9e\u73b0\u96f7\u8fbe\u5c5e\u6027\u56fe\u300b\" decoding=\"async\" loading=\"lazy\" \/><\/figure>\n<h2>\u7ec4\u4ef6\u811a\u672c\u4ee3\u7801<\/h2>\n<pre><code class=\"language-csharp line-numbers\">\/*******************************************************************\n * \u6587\u4ef6\u540d\uff1a Radar.cs\n * \u65f6  \u95f4\uff1a 2020-12-15\n * \u4f5c  \u8005\uff1a Blink\n * \u63cf  \u8ff0\uff1a \u96f7\u8fbe\u56fe\u7ec4\u4ef6\n *******************************************************************\/\n\nnamespace UnityEngine.UI\n{\n    [AddComponentMenu(&quot;UI\/Radar&quot;, 40)]\n    public class Radar : MaskableGraphic\n    {\n        public float radius = 200f;\n        [SerializeField]\n        [Range(0f, 1f)]\n        private float[] values = new float[5];\n\n        \/\/\/ &lt;summary&gt;\n        \/\/\/ \u8bbe\u7f6e\u8fb9\u6570\n        \/\/\/ &lt;\/summary&gt;\n        \/\/\/ &lt;param name=&quot;count&quot;&gt;\u9ed8\u8ba4\u4e3a5\u8fb9\u5f62\uff0c\u6700\u5c0f\u8fb9\u6570\u4e3a3&lt;\/param&gt;\n        public void SetEdges(int count = 5)\n        {\n            values = new float[count];\n            SetVerticesDirty();\n        }\n\n        \/\/\/ &lt;summary&gt;\n        \/\/\/ \u66f4\u65b0\u503c\n        \/\/\/ &lt;\/summary&gt;\n        \/\/\/ &lt;param name=&quot;index&quot;&gt;\u7d22\u5f15&lt;\/param&gt;\n        \/\/\/ &lt;param name=&quot;value&quot;&gt;\u65b0\u503c\uff08\u8303\u56f4: [0f,1f] \uff09&lt;\/param&gt;\n        public void UpdateValue(int index, float value)\n        {\n            if (CheckIndexValidity(index))\n            {\n                value = Mathf.Clamp(value, 0f, 1f);\n                values[index] = value;\n                SetVerticesDirty();\n            }\n        }\n\n        \/\/\/ &lt;summary&gt;\n        \/\/\/ \u83b7\u53d6\u503c\n        \/\/\/ &lt;\/summary&gt;\n        \/\/\/ &lt;param name=&quot;index&quot;&gt;\u7d22\u5f15&lt;\/param&gt;\n        \/\/\/ &lt;returns&gt;\u5982\u679c\u6ca1\u6709\u83b7\u53d6\u5230\u5219\u8fd4\u56de-1&lt;\/returns&gt;\n        public float GetValue(int index)\n        {\n            if (CheckIndexValidity(index))\n            {\n                return values[index];\n            }\n            return -1f;\n        }\n\n        \/\/\/ &lt;summary&gt;\n        \/\/\/ \u7d22\u5f15\u5b89\u5168\u68c0\u67e5\n        \/\/\/ &lt;\/summary&gt;\n        \/\/\/ &lt;param name=&quot;index&quot;&gt;&lt;\/param&gt;\n        \/\/\/ &lt;returns&gt;\u662f\u5426\u5b89\u5168&lt;\/returns&gt;\n        private bool CheckIndexValidity(int index)\n        {\n            if (index &gt;= values.Length)\n                throw new System.IndexOutOfRangeException();\n            else\n                return true;\n        }\n\n        protected override void OnPopulateMesh(VertexHelper toFill)\n        {\n            toFill.Clear();\n            float radian = 2 * Mathf.PI \/ values.Length;\n            toFill.AddVert(new Vector3(0, 0, 0), color, Vector2.zero);\n            for (int i = 0; i &lt; values.Length; i++)\n            {\n                float y = radius * values[i] * Mathf.Cos(radian * i);\n                float x = radius * values[i] * Mathf.Sin(radian * i);\n                toFill.AddVert(new Vector3(x, y, 0), color, Vector2.zero);\n                if (i &lt; values.Length - 1)\n                {\n                    toFill.AddTriangle(i + 1, 0, i + 2);\n                }\n                else\n                {\n                    toFill.AddTriangle(i + 1, 0, 1);\n                }\n            }\n        }\n    }\n}\n<\/code><\/pre>\n<h2>\u7f16\u8f91\u5668\u811a\u672c\u4ee3\u7801<\/h2>\n<pre><code class=\"language-csharp line-numbers\">\/*******************************************************************\n * \u6587\u4ef6\u540d\uff1a RadarEditor.cs\n * \u65f6  \u95f4\uff1a 2020-12-15\n * \u4f5c  \u8005\uff1a Blink\n * \u63cf  \u8ff0\uff1a \u7528\u4e8e\u7f16\u8f91\u5668\u4e2d\u7f16\u8f91\u96f7\u8fbe\u56fe\u7ec4\u4ef6\n *******************************************************************\/\n\nusing UnityEngine.UI;\n\nnamespace UnityEditor.UI\n{\n    [CustomEditor(typeof(Radar), true)]\n    public class RadarEditor : GraphicEditor\n    {\n        SerializedProperty m_Radius;\n        SerializedProperty m_Values;\n\n        protected override void OnEnable()\n        {\n            base.OnEnable();\n            m_Radius = serializedObject.FindProperty(&quot;radius&quot;);\n            m_Values = serializedObject.FindProperty(&quot;values&quot;);\n        }\n\n        public override void OnInspectorGUI()\n        {\n            base.OnInspectorGUI();\n            \/\/serializedObject.Update();\n            EditorGUILayout.PropertyField(m_Radius);\n            EditorGUI.BeginChangeCheck();\n            EditorGUILayout.PropertyField(m_Values);\n            if (EditorGUI.EndChangeCheck())\n            {\n                if (m_Values.arraySize &lt;= 3)\n                {\n                    m_Values.arraySize = 3;\n                }\n            }\n            serializedObject.ApplyModifiedProperties();\n        }\n    }\n}\n\n<\/code><\/pre>\n<h2>\u4f7f\u7528<\/h2>\n<ol>\n<li>\u521b\u5efa\u4e00\u4e2a\u7a7a\u7269\u4f53\uff0c\u6302\u8f7dRadar\u811a\u672c<\/li>\n<li>Radius\u4e3a\u96f7\u8fbe\u56fe\u7684\u534a\u5f84<\/li>\n<li>\u5728Values\u4e2d\u53ef\u4ee5\u8c03\u6574\u5404\u4e2a\u9876\u70b9\u7684\u503c<\/li>\n<\/ol>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/www.blinkedu.cn\/wp-content\/uploads\/2020\/12\/wp_editor_md_93bd12e43e7af8f99d04e563e20412d8.jpg\"><img decoding=\"async\" layer-src=\"https:\/\/www.blinkedu.cn\/wp-content\/uploads\/2020\/12\/wp_editor_md_93bd12e43e7af8f99d04e563e20412d8.jpg\" src=\"https:\/\/www.blinkedu.cn\/wp-content\/uploads\/2020\/12\/wp_editor_md_93bd12e43e7af8f99d04e563e20412d8.jpg\" alt=\"\u300aUnity\u4e2d\u5b9e\u73b0\u96f7\u8fbe\u5c5e\u6027\u56fe\u300b\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u7ec8\u6548\u679c \u7ec4\u4ef6\u811a\u672c\u4ee3\u7801 \/<strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong>*<strong><strong><strong><strong><strong><strong><strong><strong><strong>**<\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong> <em> \u6587\u4ef6\u540d\uff1a Radar.cs <\/em> \u65f6 \u95f4\uff1a 2020-12-15 <em> \u4f5c \u8005\uff1a Blink <\/em> \u63cf \u8ff0\uff1a \u96f7\u8fbe\u56fe\u7ec4\u4ef6 *<\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong>\u2026\u2026<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-186","post","type-post","status-publish","format-standard","hentry","category-unity"],"_links":{"self":[{"href":"https:\/\/www.blinkedu.cn\/index.php\/wp-json\/wp\/v2\/posts\/186"}],"collection":[{"href":"https:\/\/www.blinkedu.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.blinkedu.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.blinkedu.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.blinkedu.cn\/index.php\/wp-json\/wp\/v2\/comments?post=186"}],"version-history":[{"count":0,"href":"https:\/\/www.blinkedu.cn\/index.php\/wp-json\/wp\/v2\/posts\/186\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.blinkedu.cn\/index.php\/wp-json\/wp\/v2\/media?parent=186"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.blinkedu.cn\/index.php\/wp-json\/wp\/v2\/categories?post=186"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.blinkedu.cn\/index.php\/wp-json\/wp\/v2\/tags?post=186"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}