{"id":1574,"date":"2019-12-04T13:59:40","date_gmt":"2019-12-04T13:59:40","guid":{"rendered":"https:\/\/docs.sisus.co\/power-inspector\/?p=1574"},"modified":"2021-03-24T17:48:06","modified_gmt":"2021-03-24T17:48:06","slug":"disableif","status":"publish","type":"post","link":"https:\/\/docs.sisus.co\/power-inspector\/attributes\/disableif\/","title":{"rendered":"DisableIf"},"content":{"rendered":"<p>Attribute that can be used to make class member that follows the attribute be shown as disabled (read-only) when a predicate statement is true.<\/p>\n<h2>DisableIf(string classMemberName, object requiredValue)<\/h2>\n<p>When this constructor is used, the target is disabled if the class member with the given name has the specified value.<\/p>\n<h2>DisableIf(string classMemberName, Is comparison, object requiredValue)<\/h2>\n<p>When this constructor is used, the target is disabled if the class member with the given name passes the comparison check against the specified value.<\/p>\n<h2>DisableIf(string classMemberName, string comparison, object requiredValue)<\/h2>\n<p>When this constructor is used, the target is disabled if the class member with the given name passes the comparison check against the specified value.<br \/>\nAccepted comparison values are: <strong>==<\/strong>, <strong>!=<\/strong>, <strong>&lt;<\/strong>, <strong>&gt;<\/strong>, <strong>&lt;=<\/strong> and <strong>&gt;=<\/strong>. They mean <strong>equal<\/strong>, <strong>not<\/strong> <strong>equal<\/strong>, <strong>smaller than<\/strong>, <strong>larger than<\/strong>, <strong>smaller than or equal to<\/strong> and <strong>larger than or equal to<\/strong> respectively.<\/p>\n<h2>DisableIf(string booleanExpression)<\/h2>\n<p>When this constructor is used, the target is disabled if the boolean expression is true.<br \/>\nWith this constructor it is possible to evaluate expressions containing multiple parts and containing conjunctions and parentheses.<br \/>\nSupported conjunctions are <strong>&amp;<\/strong> (<strong>and<\/strong>) and <strong>|<\/strong> (<strong>or<\/strong>).<\/p>\n<h2>Attribute Target<\/h2>\n<p>Field, property, method or indexer.<\/p>\n<h2>Example<\/h2>\n<pre>using System;\r\nusing UnityEngine;\r\nusing Sisus.Attributes;\r\nusing JetBrains.Annotations;\r\n\r\npublic class InstantiatePrefab : MonoBehaviour\r\n{\r\n\t[DisableIf(nameof(hasValidResourceTarget), true)]\r\n\tpublic GameObject prefab;\r\n\r\n\t[DisableIf(nameof(prefab), Is.Not, null)]\r\n\tpublic string prefabResourceName = \"\";\r\n\r\n\t[HideInInspector, SerializeField]\r\n\tprivate bool hasValidResourceTarget;\r\n\r\n\t[ShowInInspector][NotNull]\r\n\tpublic GameObject Instantiate()\r\n\t{\r\n\t\tif(prefab != null)\r\n\t\t{\r\n\t\t\treturn Instantiate(prefab);\r\n\t\t}\r\n\r\n\t\tif(hasValidResourceTarget)\r\n\t\t{\r\n\t\t\treturn Instantiate(Resources.Load&lt;GameObject&gt;(prefabResourceName));\r\n\t\t}\r\n\r\n\t\tthrow new NullReferenceException(name + \" - Both prefab and resource name missing!\");\r\n\t}\r\n\r\n\t[UsedImplicitly]\r\n\tprivate void OnValidate()\r\n\t{\r\n\t\tif(prefab != null)\r\n\t\t{\r\n\t\t\tvar name = prefab.name;\r\n\t\t\tvar tryLoad = Resources.Load&lt;GameObject&gt;(name);\r\n\t\t\tprefabResourceName = tryLoad != null ? name : \"\";\r\n\t\t\thasValidResourceTarget = false;\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\thasValidResourceTarget = prefabResourceName.Length &gt; 0 &amp;&amp; Resources.Load&lt;GameObject&gt;(prefabResourceName) != null;\r\n\t\t}\r\n\t}\r\n}<\/pre>\n<h2>Example Result<\/h2>\n<p><img loading=\"lazy\" class=\"alignnone size-full wp-image-1579\" src=\"https:\/\/docs.sisus.co\/power-inspector\/wp-content\/uploads\/2019\/12\/disableif-attribute-unity.png\" alt=\"\" width=\"315\" height=\"106\" srcset=\"https:\/\/docs.sisus.co\/power-inspector\/wp-content\/uploads\/sites\/2\/2019\/12\/disableif-attribute-unity.png 315w, https:\/\/docs.sisus.co\/power-inspector\/wp-content\/uploads\/sites\/2\/2019\/12\/disableif-attribute-unity-300x101.png 300w\" sizes=\"(max-width: 315px) 100vw, 315px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Attribute that can be used to make class member that follows the attribute be shown as disabled (read-only) when a predicate statement is true. DisableIf(string classMemberName, object requiredValue) When this constructor is used, the target is disabled if the class member with the given name has the specified value. DisableIf(string classMemberName, Is comparison, object requiredValue) ..<\/p>\n<div class=\"clear-fix\"><\/div>\n<p><a href=\"https:\/\/docs.sisus.co\/power-inspector\/attributes\/disableif\/\" title=\"read more\">Read more<\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[26],"tags":[],"_links":{"self":[{"href":"https:\/\/docs.sisus.co\/power-inspector\/wp-json\/wp\/v2\/posts\/1574"}],"collection":[{"href":"https:\/\/docs.sisus.co\/power-inspector\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.sisus.co\/power-inspector\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.sisus.co\/power-inspector\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.sisus.co\/power-inspector\/wp-json\/wp\/v2\/comments?post=1574"}],"version-history":[{"count":4,"href":"https:\/\/docs.sisus.co\/power-inspector\/wp-json\/wp\/v2\/posts\/1574\/revisions"}],"predecessor-version":[{"id":1829,"href":"https:\/\/docs.sisus.co\/power-inspector\/wp-json\/wp\/v2\/posts\/1574\/revisions\/1829"}],"wp:attachment":[{"href":"https:\/\/docs.sisus.co\/power-inspector\/wp-json\/wp\/v2\/media?parent=1574"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.sisus.co\/power-inspector\/wp-json\/wp\/v2\/categories?post=1574"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.sisus.co\/power-inspector\/wp-json\/wp\/v2\/tags?post=1574"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}