Search Unity

Is there any Component/Assets to change children's color, Like CavansGroup change children's alpha.

Discussion in '2D' started by GamerST, Apr 2, 2018.

  1. GamerST

    GamerST

    Joined:
    Oct 14, 2013
    Posts:
    4
    Hierarchy like:
    Code (CSharp):
    1.  
    2. Panel
    3.     Image
    4.     Image1
    5.     Image2
    6.  
    I want to control some property attached on {Panel},So the children's color can blend with a color from {Panel} or somewhere.
    I think it's like the CavansGroup, but not only modify Alpha.
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Put this on your parent :
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class CanvasTint : MonoBehaviour
    4. {
    5.     [SerializeField] private Color color = Color.white;
    6.  
    7.     public Color Color
    8.     {
    9.         get { return this.color; }
    10.         set
    11.         {
    12.             this.color = value;
    13.             ApplyTint();
    14.         }
    15.     }
    16.  
    17.     private void ApplyTint()
    18.     {
    19.         foreach(CanvasRenderer rend in GetComponentsInChildren<CanvasRenderer>())
    20.         {
    21.             rend.SetColor(Color);
    22.         }
    23.     }
    24.  
    25.     private void OnValidate()
    26.     {
    27.         ApplyTint();
    28.     }
    29. }