johnhattan

Make Unity button gray its children

Sep 21st, 2017
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.95 KB | None | 0 0
  1.     public static bool IsInteractive(this Selectable Control)
  2.     {
  3.         return Control.interactable;
  4.     }
  5.  
  6.     public const float InteractiveGrayAmount = 0.5f;
  7.  
  8.     public static bool MakeInteractive(this Selectable Control, bool makeInteractive, float amount = InteractiveGrayAmount)
  9.     { // similar to interactable, but it grays out the children of the control too
  10.         float Multiplier = 0.0f;
  11.         if (!makeInteractive && Control.interactable)
  12.             Multiplier = amount; // lower alpha
  13.         else if (makeInteractive && !Control.interactable)
  14.             Multiplier = 1.0f / amount; // raise alpha again
  15.  
  16.         if (Multiplier > 0.0f)
  17.         { // just lower or raise the alpha of all children by amount
  18.             Control.interactable = makeInteractive;
  19.             foreach (var G in Control.transform.GetComponentsInChildren<Graphic>())
  20.                 if (G != Control.targetGraphic)
  21.                     G.CrossFadeAlpha(G.canvasRenderer.GetAlpha() * Multiplier, Control.colors.fadeDuration, true);
  22.         }
  23.         return IsInteractive(Control);
  24.     }
Advertisement
Add Comment
Please, Sign In to add comment