using UnityEngine; using System.Collections; using UnityEngine.UI; /// <summary> /// User interface alpha ctrl. /// 统一控制图片的透明度 /// </summary> public class UIAlphaCtrl : MonoBehaviour { Image[] images; Text[] texts; Color[] c_Images; Color[] c_Texts; void Awake () { GetChildren (); } void GetChildren(){ images = GetComponentsInChildren<Image>(); texts = GetComponentsInChildren<Text> (); c_Images = new Color[images.Length]; c_Texts = new Color[texts.Length]; for (int i = 0; i < images.Length; i++) { c_Images [i] = images [i].color; } for (int i = 0; i < texts.Length; i++) { c_Texts [i] = texts [i].color; } } public void ChangeAlpha(float alpha){ for (int i = 0; i < images.Length; i++) { c_Images [i].a = alpha; images [i].color = c_Images [i]; } for (int i = 0; i < texts.Length; i++) { c_Texts [i].a =alpha; texts [i].color = c_Texts [i]; } } }
注意:
将目标UI归纳到一个对象下,并绑该脚本