| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 | using System;using TMPro;using UnityEngine;using UnityEngine.UI;namespace Fort23.Mono{    public static class UIHelper    {        public static void IsGray(this Transform transform, bool isGray = true)        {            if (isGray)            {                Gray(transform);            }            else            {                RecoverColor(transform);            }        }        public static void Gray(this Transform transform, bool enableButton = true)        {            if (transform.GetComponent<Button>() != null)            {                transform.GetComponent<Button>().enabled = enableButton;                if (transform.GetComponent<Button>().image?.GetComponent<MyImage>() != null)                    transform.GetComponent<Button>().image.GetComponent<MyImage>().IsGray = true;            }            else if (transform.GetComponent<MyImage>() != null)            {                transform.GetComponent<MyImage>().IsGray = true;            }            else if (transform.GetComponent<Text>() != null)            {                transform.GetComponent<Text>().material = UIManager.Instance.uiGray;            }            // else if (transform.GetComponent<MyTextMeshProUGUI>() != null)            // {            //     transform.GetComponent<MyTextMeshProUGUI>().Gray();            // }            if (transform.childCount > 0)            {                for (int i = 0; i < transform.childCount; i++)                {                    Gray(transform.GetChild(i).transform, enableButton);                }            }        }        public static void RecoverColor(this Transform transform)        {            // transform.Opaque();            if (transform.GetComponent<Button>() != null)            {                transform.GetComponent<Button>().enabled = true;                if (transform.GetComponent<Button>().image?.GetComponent<MyImage>() != null)                    transform.GetComponent<Button>().image.GetComponent<MyImage>().IsGray = false;            }            else if (transform.GetComponent<MyImage>() != null)            {                transform.GetComponent<MyImage>().IsGray = false;            }            else if (transform.GetComponent<Text>() != null)            {                transform.GetComponent<Text>().material = null;            }            // else if (transform.GetComponent<MyTextMeshProUGUI>() != null)            // {            //     transform.GetComponent<MyTextMeshProUGUI>().Recover();            // }            if (transform.childCount > 0)            {                for (int i = 0; i < transform.childCount; i++)                {                    RecoverColor(transform.GetChild(i).transform);                }            }        }        private static Color GrayedColor(Color color)        {            float gray = Vector3.Dot(new Vector3(color.r, color.g, color.b), new Vector3(0.299f, 0.587f, 0.114f));            return new Color(gray, gray, gray, color.a);        }        /// <summary>        /// 半透明        /// </summary>        /// <param name="transform"></param>        public static void Translucency(this Transform transform)        {            RecoverColor(transform);            Color color = new Color(0.34f, 0.34f, 0.34f);            if (transform.GetComponent<Button>() != null)            {                if (transform.GetComponent<Button>().image != null &&                    transform.GetComponent<Button>().image.GetComponent<MyImage>().icon_name != string.Empty)                    transform.GetComponent<Button>().image.color = color;            }            else if (transform.GetComponent<MyImage>() != null)            {                if (transform.GetComponent<MyImage>().icon_name != String.Empty)                    transform.GetComponent<MyImage>().color = color;            }            // else if (transform.GetComponent<Text>() != null)            // {            //     // transform.GetComponent<Text>().color = color;            //     transform.GetComponent<Text>().Translucency();            // }            if (transform.childCount > 0)            {                for (int i = 0; i < transform.childCount; i++)                {                    Translucency(transform.GetChild(i).transform);                }            }        }        /// <summary>        /// 从半透明中恢复默认        /// </summary>        /// <param name="transform"></param>        public static void Opaque(this Transform transform)        {            Color color = new Color(1, 1, 1);            if (transform.GetComponent<Button>() != null)            {                if (transform.GetComponent<Button>().image != null)                {                    MyImage myImage = transform.GetComponent<Button>().image.GetComponent<MyImage>();                    if (myImage != null && myImage.icon_name != string.Empty)                        transform.GetComponent<Button>().image.color = color;                }            }            else if (transform.GetComponent<MyImage>() != null)            {                if (transform.GetComponent<MyImage>().icon_name != String.Empty)                {                    transform.GetComponent<MyImage>().color = color;                }            }            // else if (transform.GetComponent<Text>() != null)            // {            //     // transform.GetComponent<Text>().color = color;            //     transform.GetComponent<Text>().Recover();            // }            if (transform.childCount > 0)            {                for (int i = 0; i < transform.childCount; i++)                {                    Opaque(transform.GetChild(i).transform);                }            }        }    }}
 |