Skip to content

Commit a849d5a

Browse files
committed
feat(Util): Support more type on range
1 parent 6daf669 commit a849d5a

File tree

1 file changed

+78
-4
lines changed

1 file changed

+78
-4
lines changed

Assets/JCSUnity/Scripts/Util/JCS_Random.cs

Lines changed: 78 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ public static class JCS_Random
2929
/// <param name="min"> mininum value </param>
3030
/// <param name="max"> maxinum value </param>
3131
/// <returns> random number </returns>
32+
public static int Range(Vector2Int range)
33+
{
34+
return Random.Range(range.x, range.y);
35+
}
36+
public static float Range(Vector2 range)
37+
{
38+
return Random.Range(range.x, range.y);
39+
}
3240
public static int Range(int min, int max)
3341
{
3442
return Random.Range(min, max);
@@ -48,6 +56,14 @@ public static float Range(float min, float max)
4856
/// <param name="min"> mininum value </param>
4957
/// <param name="max"> maxinum value </param>
5058
/// <returns> random number </returns>
59+
public static int RangeInclude(Vector2Int range)
60+
{
61+
return RangeInclude(range.x, range.y);
62+
}
63+
public static float RangeInclude(Vector2 range)
64+
{
65+
return RangeInclude(range.x, range.y);
66+
}
5167
public static int RangeInclude(int min, int max)
5268
{
5369
return Range(min, max + 1);
@@ -65,6 +81,14 @@ public static float RangeInclude(float min, float max)
6581
/// <summary>
6682
/// Return a random vector 2 int.
6783
/// </summary>
84+
public static Vector2Int Vector2Int(Vector2Int range)
85+
{
86+
return Vector2Int(range.x, range.y);
87+
}
88+
public static Vector2Int Vector2Int(Vector2 range)
89+
{
90+
return Vector2Int((int)range.x, (int)range.y);
91+
}
6892
public static Vector2Int Vector2Int(int min, int max)
6993
{
7094
int x = Range(min, max);
@@ -75,6 +99,14 @@ public static Vector2Int Vector2Int(int min, int max)
7599
/// <summary>
76100
/// Return a random vector 2.
77101
/// </summary>
102+
public static Vector2 Vector2(Vector2Int range)
103+
{
104+
return Vector2(range.x, range.y);
105+
}
106+
public static Vector2 Vector2(Vector2 range)
107+
{
108+
return Vector2(range.x, range.y);
109+
}
78110
public static Vector2 Vector2(float min, float max)
79111
{
80112
float x = Range(min, max);
@@ -85,6 +117,14 @@ public static Vector2 Vector2(float min, float max)
85117
/// <summary>
86118
/// Return a random vector 3 int.
87119
/// </summary>
120+
public static Vector3Int Vector3Int(Vector2Int range)
121+
{
122+
return Vector3Int(range.x, range.y);
123+
}
124+
public static Vector3Int Vector3Int(Vector2 range)
125+
{
126+
return Vector3Int((int)range.x, (int)range.y);
127+
}
88128
public static Vector3Int Vector3Int(int min, int max)
89129
{
90130
int x = Range(min, max);
@@ -96,6 +136,14 @@ public static Vector3Int Vector3Int(int min, int max)
96136
/// <summary>
97137
/// Return a random vector 3.
98138
/// </summary>
139+
public static Vector3 Vector3(Vector2Int range)
140+
{
141+
return Vector3(range.x, range.y);
142+
}
143+
public static Vector3 Vector3(Vector2 range)
144+
{
145+
return Vector3(range.x, range.y);
146+
}
99147
public static Vector3 Vector3(float min, float max)
100148
{
101149
float x = Range(min, max);
@@ -107,6 +155,14 @@ public static Vector3 Vector3(float min, float max)
107155
/// <summary>
108156
/// Return a random Quaternion.
109157
/// </summary>
158+
public static Quaternion Quaternion(Vector2Int range)
159+
{
160+
return Quaternion(range.x, range.y);
161+
}
162+
public static Quaternion Quaternion(Vector2 range)
163+
{
164+
return Quaternion(range.x, range.y);
165+
}
110166
public static Quaternion Quaternion(float min, float max)
111167
{
112168
float x = Range(min, max);
@@ -121,18 +177,36 @@ public static Quaternion Quaternion(float min, float max)
121177
/// <returns> random color object. </returns>
122178
public static Color Color()
123179
{
124-
float a = Range(0.0f, 1.0f);
180+
float a = RangeInclude(0.0f, 1.0f);
125181
return Color(a);
126182
}
127183
public static Color Color(float a = 1.0f)
128184
{
129-
float x = Range(0.0f, 1.0f);
130-
float y = Range(0.0f, 1.0f);
131-
float z = Range(0.0f, 1.0f);
185+
float x = RangeInclude(0.0f, 1.0f);
186+
float y = RangeInclude(0.0f, 1.0f);
187+
float z = RangeInclude(0.0f, 1.0f);
132188

133189
return new Color(x, y, z, a);
134190
}
135191

192+
/// <summary>
193+
/// Return a random color in 32-bit format.
194+
/// </summary>
195+
/// <returns> random color object. </returns>
196+
public static Color32 Color32()
197+
{
198+
byte a = (byte)RangeInclude(0, 255);
199+
return Color32(a);
200+
}
201+
public static Color32 Color32(byte a = 1)
202+
{
203+
byte x = (byte)RangeInclude(0, 255);
204+
byte y = (byte)RangeInclude(0, 255);
205+
byte z = (byte)RangeInclude(0, 255);
206+
207+
return new Color32(x, y, z, a);
208+
}
209+
136210
/// <summary>
137211
/// Find the decimal include.
138212
/// </summary>

0 commit comments

Comments
 (0)