Tanoda
UIGridRenderer.cs
Go to the documentation of this file.
1
3
4
6{
7 [AddComponentMenu("UI/Extensions/Primitives/UIGridRenderer")]
9 {
10 [SerializeField]
11 private int m_GridColumns = 10;
12 [SerializeField]
13 private int m_GridRows = 10;
14
18 public int GridColumns
19 {
20 get
21 {
22 return m_GridColumns;
23 }
24
25 set
26 {
27 if (m_GridColumns == value)
28 return;
29 m_GridColumns = value;
30 SetAllDirty();
31 }
32 }
33
37 public int GridRows
38 {
39 get
40 {
41 return m_GridRows;
42 }
43
44 set
45 {
46 if (m_GridRows == value)
47 return;
48 m_GridRows = value;
49 SetAllDirty();
50 }
51 }
52
53 protected override void OnPopulateMesh(VertexHelper vh)
54 {
55 relativeSize = true;
56
57 int ArraySize = (GridRows * 3) + 1;
58 if(GridRows % 2 == 0)
59 ++ArraySize; // needs one more line
60
61 ArraySize += (GridColumns * 3) + 1;
62
63 m_points = new Vector2[ArraySize];
64
65 int Index = 0;
66 for(int i = 0; i < GridRows; ++i)
67 {
68 float xFrom = 1;
69 float xTo = 0;
70 if(i % 2 == 0)
71 {
72 // reach left instead
73 xFrom = 0;
74 xTo = 1;
75 }
76
77 float y = ((float)i) / GridRows;
78 m_points[Index].x = xFrom;
79 m_points[Index].y = y;
80 ++Index;
81 m_points[Index].x = xTo;
82 m_points[Index].y = y;
83 ++Index;
84 m_points[Index].x = xTo;
85 m_points[Index].y = (float)(i + 1) / GridRows;
86 ++Index;
87 }
88
89 if(GridRows % 2 == 0)
90 {
91 // two lines to get to 0, 1
92 m_points[Index].x = 1;
93 m_points[Index].y = 1;
94 ++Index;
95 }
96
97 m_points[Index].x = 0;
98 m_points[Index].y = 1;
99 ++Index;
100
101 // line is now at 0,1, so we can draw the columns
102 for(int i = 0; i < GridColumns; ++i)
103 {
104 float yFrom = 1;
105 float yTo = 0;
106 if(i % 2 == 0)
107 {
108 // reach up instead
109 yFrom = 0;
110 yTo = 1;
111 }
112
113 float x = ((float)i) / GridColumns;
114 m_points[Index].x = x;
115 m_points[Index].y = yFrom;
116 ++Index;
117 m_points[Index].x = x;
118 m_points[Index].y = yTo;
119 ++Index;
120 m_points[Index].x = (float)(i + 1) / GridColumns;
121 m_points[Index].y = yTo;
122 ++Index;
123 }
124
125 if(GridColumns % 2 == 0)
126 {
127 // one more line to get to 1, 1
128 m_points[Index].x = 1;
129 m_points[Index].y = 1;
130 }
131 else
132 {
133 // one more line to get to 1, 0
134 m_points[Index].x = 1;
135 m_points[Index].y = 0;
136 }
137
138 base.OnPopulateMesh(vh);
139 }
140 }
141}
override void OnPopulateMesh(VertexHelper vh)
int GridRows
Number of rows in the grid.
int GridColumns
Number of columns in the Grid
Credit Erdener Gonenc - @PixelEnvision.