少年修仙传客户端代码仓库
hch
2 天以前 600733c8f592cb9e65f2b7a3e110ac1d686e6bfe
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
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 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;
                }
            }
 
            if(transform.childCount > 0) {
                SetChildrenImageMaterial(transform);
            }
        }
    }
 
    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);
            }
        }
    }
 
    private void UpdateChildMaterialSmoothMask() {
        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;
 
        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));
            }
        }
    }
 
    static void SetSmoothMask(Material material,Vector2 leftBottom,Vector2 rightTop,Vector2 smoothRect) {
        material.SetVector("_LeftBottom",leftBottom);
        material.SetVector("_RightTop",rightTop);
        material.SetVector("_SmoothRect",smoothRect);
    }
}