{"id":114,"date":"2014-12-13T02:49:57","date_gmt":"2014-12-13T02:49:57","guid":{"rendered":"http:\/\/www.voidbred.com\/blog\/?p=114"},"modified":"2014-12-13T02:49:57","modified_gmt":"2014-12-13T02:49:57","slug":"auto-updating-custom-inspectors-for-unity","status":"publish","type":"post","link":"http:\/\/www.voidbred.com\/blog\/2014\/12\/auto-updating-custom-inspectors-for-unity\/","title":{"rendered":"Auto Updating Custom Inspectors for Unity"},"content":{"rendered":"<p><strong>The Problem:<\/strong>\u00a0 You wrote a custom inspector for Unity, subclassing the Editor class, after using the inspector you found that it is only updating when you interact with it.\u00a0 Values are changing continuously during Update() calls from the MonoBehaviour so there is no other event with which you can tie your inspector Repaint().<\/p>\n<p><strong>Solution:<\/strong>\u00a0 I&#8217;d recommend tying your Editor.Repaint() to some event if possible, but if not I&#8217;d recommend putting a lambda on your MonoBehaviour which gets conditionally called if you are running your code in the Unity Editor inside your Update method.\u00a0 Inside your inspectors OnInspectorGUI just set your targets lambda to something that will call your Editor&#8217;s Repaint Method.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"toolbar:1 lang:default decode:true \">using System;\r\nusing UnityEditor;\r\nusing UnityEngine;\r\n\r\npublic class Foo : MonoBehaviour\r\n{\r\n    public Action EditorUpdater {get;set;}\r\n\r\n    [System.Diagnostics.Conditional(\"UNITY_EDITOR\")]\r\n    void CallEditorUpdater(){\r\n        if (null != this.EditorUpdater) {\r\n            this.EditorUpdater();\r\n        }\r\n    }\r\n\r\n    void Update() {\r\n        CallEditorUpdater();\r\n    }\r\n}\r\n\r\n[CustomEditor(typeof(Foo))]\r\npublic class FooInspector : Editor\r\n{\r\n    private void OnInspectorGUI () {\r\n        var foo = target as Foo;\r\n        foo.EditorUpdater = ()=&gt;this.Repaint();\r\n    }\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Problem:\u00a0 You wrote a custom inspector for Unity, subclassing the Editor class, after using the inspector you found that it is only updating when you interact with it.\u00a0 Values are changing continuously during Update() calls from the MonoBehaviour so there is no other event with which you can tie your inspector Repaint(). Solution:\u00a0 I&#8217;d&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[11],"_links":{"self":[{"href":"http:\/\/www.voidbred.com\/blog\/wp-json\/wp\/v2\/posts\/114"}],"collection":[{"href":"http:\/\/www.voidbred.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.voidbred.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.voidbred.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.voidbred.com\/blog\/wp-json\/wp\/v2\/comments?post=114"}],"version-history":[{"count":2,"href":"http:\/\/www.voidbred.com\/blog\/wp-json\/wp\/v2\/posts\/114\/revisions"}],"predecessor-version":[{"id":116,"href":"http:\/\/www.voidbred.com\/blog\/wp-json\/wp\/v2\/posts\/114\/revisions\/116"}],"wp:attachment":[{"href":"http:\/\/www.voidbred.com\/blog\/wp-json\/wp\/v2\/media?parent=114"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.voidbred.com\/blog\/wp-json\/wp\/v2\/categories?post=114"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.voidbred.com\/blog\/wp-json\/wp\/v2\/tags?post=114"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}