yyl
23 小时以前 5d3366f2e0f687995eb7ad2107c4379fe7acd4e8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
 
[DisallowMultipleComponent]
[ExecuteAlways]
 
public class SmoothMask:UIBehaviour,ICanvasRaycastFilter {
 
    [Range(0,1)]
    [SerializeField]
    private float m_Horizontal = 0f;
    public float horizontal { get { return m_Horizontal; } }
 
    [Range(0,1)]
    [SerializeField]
    private float m_Vertical = 0f;
    public float vertical { get { return m_Vertical; } }
 
    [SerializeField]
    private Material[] m_ImageMaterials;
    public Material[] imageMaterials { get { return m_ImageMaterials; } }
 
    [SerializeField]
    private Material[] m_TextMaterials;
    public Material[] textMaterials { get { return m_TextMaterials; } }
 
    [SerializeField]
    private CanvasScaler m_CanvasScaler;
    public CanvasScaler canvasScaler {
        get {
            return m_CanvasScaler ?? (m_CanvasScaler = GetComponentInParent<CanvasScaler>());
        }
    }
 
    public RectTransform rectTransform {
        get {
            return this.transform as RectTransform;
        }
    }
    private Vector2 leftBottom;
    private Vector2 rightTop;
 
    public bool IsRaycastLocationValid(Vector2 sp,Camera eventCamera) {
        return RectTransformUtility.RectangleContainsScreenPoint(rectTransform,sp,eventCamera);
    }
 
    void LateUpdate() {
        UpdateChildMaterialSmoothMask();
    }
 
    [ContextMenu("SetMaterial")]
    public void UpdateChildrenMaterial() {
        SetChildrenImageMaterial(this.transform);
        SetChildrenTextMaterial(this.transform);
    }
 
    private void SetChildrenImageMaterial(Transform _transform) {
        Image[] images = _transform.GetComponentsInChildren<Image>(true);
        ImageEx[] imageexs = _transform.GetComponentsInChildren<ImageEx>(true);
 
        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[] 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() {
        var scaleX = canvasScaler.transform.localScale.x;
        var scaleY = canvasScaler.transform.localScale.y;
 
        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++) {
                SetSmoothMask(imageMaterials[i],leftBottom,rightTop,new Vector2(horizontal,vertical));
            }
        }
 
        if(textMaterials != null) {
            for(int i = 0;i < textMaterials.Length;i++) {
                SetSmoothMask(textMaterials[i],leftBottom,rightTop,new Vector2(horizontal,vertical));
            }
        }
    }
 
#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);
            }
        }
    }
}