yyl
1 天以前 48f67bcd7025dd86525666ef99b8420f363b0901
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
using UnityEngine;
using UnityEngine.UI;
 
[DisallowMultipleComponent]
[RequireComponent(typeof(RectTransform))]
public class SectionImage:Graphic {
 
    [SerializeField]
    [Range(2,100)]
    int m_Section = 3;
    public int section {
        get {
            return m_Section;
        }
    }
 
    [SerializeField]
    [Range(1,200)]
    float m_Width = 2f;
    public float width {
        get {
            return m_Width;
        }
    }
 
    Vector2[] uv0s= new Vector2[4] { Vector2.one,Vector2.one,Vector2.one,Vector2.one };
 
    protected override void OnPopulateMesh(VertexHelper vh) {
        vh.Clear();
 
        var size = new Vector2(rectTransform.rect.width,rectTransform.rect.height);
 
        var squareCount = section - 1;
        var deltaX = (size.x - (section - 1) * width) / section + width;
        var startX = -size.x * 0.5f + deltaX - width;
 
        for(var i = 0;i < squareCount;i++) {
 
            var positions = new Vector3[4];
 
            positions[0] = new Vector2(startX,-size.y * 0.5f);
            positions[1] = new Vector2(startX + width,-size.y * 0.5f);
            positions[2] = new Vector2(startX + width,size.y * 0.5f);
            positions[3] = new Vector2(startX,size.y * 0.5f);
 
            UIUtility.AddQuad(vh,positions,color,uv0s);
            startX += deltaX;
        }
 
    }
}