johnhattan icon

Make Unity button gray its children

johnhattan | PRO | 09/21/17 02:16:48 PM UTC | 0 ⭐ | 270 👁️ | Never ⏰ | []
C# |

950 B

|

None

|

0 👍

/

0 👎

    public static bool IsInteractive(this Selectable Control)
    {
        return Control.interactable;
    }
 
    public const float InteractiveGrayAmount = 0.5f;
 
    public static bool MakeInteractive(this Selectable Control, bool makeInteractive, float amount = InteractiveGrayAmount)
    { // similar to interactable, but it grays out the children of the control too
        float Multiplier = 0.0f;
        if (!makeInteractive && Control.interactable)
            Multiplier = amount; // lower alpha
        else if (makeInteractive && !Control.interactable)
            Multiplier = 1.0f / amount; // raise alpha again
 
        if (Multiplier > 0.0f)
        { // just lower or raise the alpha of all children by amount
            Control.interactable = makeInteractive;
            foreach (var G in Control.transform.GetComponentsInChildren<Graphic>())
                if (G != Control.targetGraphic)
                    G.CrossFadeAlpha(G.canvasRenderer.GetAlpha() * Multiplier, Control.colors.fadeDuration, true);
        }
        return IsInteractive(Control);
    }

Comments