{"id":1320,"date":"2015-03-18T10:29:06","date_gmt":"2015-03-18T10:29:06","guid":{"rendered":"http:\/\/www.clonefactor.com\/wordpress\/?p=1320"},"modified":"2015-03-18T10:29:47","modified_gmt":"2015-03-18T10:29:47","slug":"unity3d-ugui-toggle-button","status":"publish","type":"post","link":"https:\/\/www.clonefactor.com\/wordpress\/program\/unity3d\/1320\/","title":{"rendered":"Unity3D uGUI Toggle button"},"content":{"rendered":"<p>Testing for Unity3D new uGUI system these day,<\/p>\n<p>discover that the toggle button feature not good enough, so I design to write my own.<\/p>\n<pre class=\"brush:csharp\">using UnityEngine;\r\nusing UnityEngine.UI;\r\n\r\n[RequireComponent(typeof(Toggle))]\r\npublic class ToggleEvent : MonoBehaviour\r\n{\r\n    \/\/\/ &lt;summary&gt;Delegate OnValueChanged event&lt;\/summary&gt;\r\n    \/\/\/ &lt;param name=\"value\"&gt;true\/false&lt;\/param&gt;\r\n    \/\/\/ &lt;example&gt;this.OnValueChanged += YouMethod(bool value)&lt;\/example&gt;\r\n    public delegate void ValueChanged(bool value);\r\n    public ValueChanged OnValueChanged;\r\n\r\n    protected Toggle toggle;\r\n    protected virtual void OnEnable()\r\n    {\r\n        toggle = GetComponent&lt;Toggle&gt;();\r\n        toggle.onValueChanged.AddListener(WhenValueChanged);\r\n    }\r\n    protected virtual void OnDisable()\r\n    {\r\n        toggle.onValueChanged.RemoveListener(WhenValueChanged);\r\n    }\r\n    protected virtual void WhenValueChanged(bool value)\r\n    {\r\n        \/\/ Debug.Log(string.Format(\"{0} Toggle :{1}\",GetType().Name.ToString(),value.ToString()));\r\n        if (OnValueChanged != null)\r\n            OnValueChanged(value);\r\n    }\r\n}\r\n<\/pre>\n<p>the concept are simple using reflection to sync the target script component&#8217;s boolean value.<\/p>\n<pre class=\"brush:csharp\">using UnityEngine;\r\nusing UnityEngine.UI;\r\nusing System;\r\nusing System.Reflection;\r\n\r\n[RequireComponent(typeof(Toggle))]\r\npublic class ToggleSync : ToggleEvent\r\n{\r\n    \/\/\/ &lt;summary&gt;\r\n    \/\/\/ For those too lazy to coding\r\n    \/\/\/ &lt;\/summary&gt;\r\n    \/\/\/ &lt;remarks&gt;Make sure all GameObject is allocated, component &amp; property name are correct&lt;\/remarks&gt;\r\n    [Tooltip(\"Target GameObject wanted to call\")]\r\n    public GameObject target;\r\n    [Tooltip(\"Target Component Name on GameObject, throw error when null\")]\r\n    public string componentName;\r\n    \/\/ public Component targetComponent; \/\/ hard to drag component\r\n    [Tooltip(\"Target Property Name(Boolean) in Component, throw error when null\")]\r\n    public string propertyName;\r\n    [Tooltip(\"Use ToggleEvent.OnValueChanged method\")]\r\n    public bool UseEventDelegate = false;\r\n\r\n    protected override void OnEnable()\r\n    {\r\n        base.OnEnable();\r\n        toggle.isOn = GetValue();\r\n    }\r\n    protected override void WhenValueChanged(bool value)\r\n    {\r\n        if (UseEventDelegate)\r\n            base.WhenValueChanged(value);\r\n        SetValue(value);\r\n\t}\r\n    private bool GetValue()\r\n    {\r\n        if (target == null)\r\n            return false;\r\n        Component component = target.GetComponent(componentName);\r\n        Type targetType = (component == null) ? null : component.GetType();\r\n        PropertyInfo propertyInfo = (targetType == null) ? null : targetType.GetProperty(propertyName);\r\n        return (propertyInfo==null)?false:(bool)propertyInfo.GetValue(component, null);\r\n    }\r\n    private bool SetValue(bool value)\r\n    {\r\n        if (target == null)\r\n            return false;\r\n        string msg = string.Empty;\r\n        Component component = target.GetComponent(componentName);\r\n        Type targetType = (component == null) ? null : component.GetType();\r\n        PropertyInfo propertyInfo = (targetType == null) ? null : targetType.GetProperty(propertyName);\r\n        \r\n        if (ReferenceEquals(component, null))\r\n        {\r\n            msg = string.Format(\"{0} cannot find component {1} on {2}\",\r\n                GetType().Name.ToString(),\r\n                componentName,\r\n                target.ToString());\r\n        }\r\n        else if (ReferenceEquals(targetType, null))\r\n        {\r\n            msg = string.Format(\"{0} component&lt;{1}&gt; is invaild {2}\",\r\n                GetType().Name.ToString(),\r\n                componentName,\r\n                target.ToString());\r\n        }\r\n        else if (ReferenceEquals(null, propertyInfo))\r\n        {\r\n            msg = string.Format(\"{0} cannot find property \\\"{1}\\\" on {2}\",\r\n                GetType().Name.ToString(),\r\n                propertyName,\r\n                target.ToString());\r\n        }\r\n        else if (!propertyInfo.CanWrite)\r\n        {\r\n            msg = string.Format(\"{0} cannot assign property value \\\"{1}\\\" on {2}\",\r\n                GetType().Name.ToString(),\r\n                propertyName,\r\n                target.ToString());\r\n        }\r\n        if (msg.Equals(String.Empty))\r\n        {\r\n            propertyInfo.SetValue(component, value, null);\r\n            return true;\r\n        }\r\n        else\r\n        {\r\n            Debug.LogError(msg);\r\n            return false;\r\n        }\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Testing for Unity3D new uGUI system these day, dis &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":[16,67,43],"class_list":["post-1320","post","type-post","status-publish","format-standard","hentry","category-unity3d","tag-c-2","tag-ugui","tag-unity3d-2"],"_links":{"self":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts\/1320","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=1320"}],"version-history":[{"count":0,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts\/1320\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/media?parent=1320"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/categories?post=1320"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/tags?post=1320"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}