Skip to content

Commit e0ea083

Browse files
authored
Added logic to reset Random.state in ColorSettings to avoid polutting the global rng state with a seed of 0. (#404)
1 parent 7717150 commit e0ea083

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

Plugins/Editor/Scripts/Data/Settings/ColorSettings.cs

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -454,25 +454,33 @@ public static void Update()
454454
maxOutlines2D = CopyValue(pointInnerStateColor[0], invertedColor2);
455455

456456
outlines2D = new Color[13];
457-
UnityEngine.Random.InitState(0);
458-
for (int i=0;i<outlines2D.Length;i++)
459-
{
460-
var color = new Color(
461-
Mathf.Lerp(minOutlines2D.r, maxOutlines2D.r, UnityEngine.Random.value),
462-
Mathf.Lerp(minOutlines2D.g, maxOutlines2D.g, UnityEngine.Random.value),
463-
Mathf.Lerp(minOutlines2D.b, maxOutlines2D.b, UnityEngine.Random.value),
464-
1.0f
465-
);
466-
float h, s, v;
467-
Color.RGBToHSV(color, out h, out s, out v);
468-
469-
s *= 0.5f;
470-
v *= 1.1f;
471-
472-
outlines2D[i] = Color.HSVToRGB(h, s, v);
473-
}
474-
475-
gridColorW = parsedCenterAxis;
457+
var cache = UnityEngine.Random.state; //cache current state to avoid throwing off global rng.
458+
try
459+
{
460+
UnityEngine.Random.InitState(0);
461+
for (int i = 0; i < outlines2D.Length; i++)
462+
{
463+
var color = new Color(
464+
Mathf.Lerp(minOutlines2D.r, maxOutlines2D.r, UnityEngine.Random.value),
465+
Mathf.Lerp(minOutlines2D.g, maxOutlines2D.g, UnityEngine.Random.value),
466+
Mathf.Lerp(minOutlines2D.b, maxOutlines2D.b, UnityEngine.Random.value),
467+
1.0f
468+
);
469+
float h, s, v;
470+
Color.RGBToHSV(color, out h, out s, out v);
471+
472+
s *= 0.5f;
473+
v *= 1.1f;
474+
475+
outlines2D[i] = Color.HSVToRGB(h, s, v);
476+
}
477+
}
478+
finally
479+
{
480+
UnityEngine.Random.state = cache;
481+
}
482+
483+
gridColorW = parsedCenterAxis;
476484

477485
parsedGrid.a = Mathf.Max(0.85f, parsedGrid.a);
478486

0 commit comments

Comments
 (0)