yyl
2025-05-30 d18d4a9dacd9557f2bc0b4cf141e99374406b4dc
Main/Component/UI/Common/SmoothMask.cs
@@ -56,54 +56,42 @@
    }
    private void SetChildrenImageMaterial(Transform _transform) {
        Image image;
        Transform transform;
        for(int i = 0;i < _transform.childCount;i++) {
            transform = _transform.GetChild(i);
            image = transform.GetComponent<Image>();
            if(image != null) {
                image.material = imageMaterials.Length > 0 ? imageMaterials[0] : null;
            }
            else
            {
                var imageex = transform.GetComponent<ImageEx>();
                if (imageex != null)
                {
                    imageex.material = imageMaterials.Length > 0 ? imageMaterials[0] : null;
                }
            }
        Image[] images = _transform.GetComponentsInChildren<Image>(true);
        ImageEx[] imageexs = _transform.GetComponentsInChildren<ImageEx>(true);
            if(transform.childCount > 0) {
                SetChildrenImageMaterial(transform);
        if (null != images)
        {
            for(int i = 0;i < images.Length;i++) {
                images[i].material = imageMaterials.Length > 0? imageMaterials[0] : null;
            }
        }
        if (null!= imageexs)
        {
            for(int i = 0;i < imageexs.Length;i++) {
                imageexs[i].material = imageMaterials.Length > 0? imageMaterials[0] : null;
            }
        }
    }
    private void SetChildrenTextMaterial(Transform _parent) {
        Text text;
        Transform transform;
        for(int i = 0;i < _parent.childCount;i++) {
            transform = _parent.GetChild(i);
            text = transform.GetComponent<Text>();
            if(text != null) {
                text.material = textMaterials.Length > 0 ? textMaterials[0] : null;
            }
            if(transform.childCount > 0) {
                SetChildrenTextMaterial(transform);
        Text[] texts = _parent.GetComponentsInChildren<Text>(true);
        if(texts!= null) {
            for(int i = 0;i < texts.Length;i++) {
                texts[i].material = textMaterials.Length > 0? textMaterials[0] : null;
            }
        }
    }
    private void UpdateChildMaterialSmoothMask() {
        if (null == canvasScaler)
            return;
        var scaleX = canvasScaler.transform.localScale.x;
        var scaleY = canvasScaler.transform.localScale.y;
        leftBottom = scaleX == 0f ? Vector2.zero : rectTransform.GetMinWorldPosition() / scaleX;
        rightTop = scaleY == 0f ? Vector2.zero : rectTransform.GetMaxWorldPosition() / scaleY;
        Vector3[] corners = new Vector3[4];
        rectTransform.GetWorldCorners(corners);
        leftBottom = new Vector2(corners[0].x, corners[0].y);
        rightTop = new Vector2(corners[2].x, corners[2].y);
        if(imageMaterials != null) {
            for(int i = 0;i < imageMaterials.Length;i++) {
@@ -118,10 +106,55 @@
        }
    }
    static void SetSmoothMask(Material material,Vector2 leftBottom,Vector2 rightTop,Vector2 smoothRect) {
        material.SetVector("_LeftBottom",leftBottom);
        material.SetVector("_RightTop",rightTop);
        material.SetVector("_SmoothRect",smoothRect);
#if UNITY_EDITOR
    void OnDrawGizmos() {
        Vector3[] corners = new Vector3[4];
        rectTransform.GetWorldCorners(corners);
        Gizmos.color = Color.red; // 设置线框颜色
        // 绘制矩形边界
        Gizmos.DrawLine(corners[0], corners[1]); // 左下→左上
        Gizmos.DrawLine(corners[1], corners[2]); // 左上→右上
        Gizmos.DrawLine(corners[2], corners[3]); // 右上→右下
        Gizmos.DrawLine(corners[3], corners[0]); // 右下→左下
    }
#endif
    void SetSmoothMask(Material material,Vector2 leftBottom,Vector2 rightTop,Vector2 smoothRect) {
        Image[] images = GetComponentsInChildren<Image>(true);
        ImageEx[] imageexs = GetComponentsInChildren<ImageEx>(true);
        Text[] texts = GetComponentsInChildren<Text>(true);
        if (null != images)
        {
            foreach (var graphic in images)
            {
                graphic.material.SetVector("_LeftBottom",leftBottom);
                graphic.material.SetVector("_RightTop",rightTop);
                graphic.material.SetVector("_SmoothRect",smoothRect);
            }
        }
        if (null != imageexs)
        {
            foreach (var graphic in imageexs)
            {
                graphic.material.SetVector("_LeftBottom",leftBottom);
                graphic.material.SetVector("_RightTop",rightTop);
                graphic.material.SetVector("_SmoothRect",smoothRect);
            }
        }
        if (null != texts)
        {
            foreach (var graphic in texts)
            {
                graphic.material.SetVector("_LeftBottom",leftBottom);
                graphic.material.SetVector("_RightTop",rightTop);
                graphic.material.SetVector("_SmoothRect",smoothRect);
            }
        }
    }
}