Skip to content

Commit

Permalink
feat: Add random enum value
Browse files Browse the repository at this point in the history
  • Loading branch information
jcs090218 committed Sep 26, 2024
1 parent d012e26 commit b787172
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Assets/JCSUnity/Editor/JCSUnity_EditorWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ private static void CreateSlidePanel()
Image panelImage = slidePanel.GetComponent<Image>();
if (panelImage != null)
{
panelImage.color = JCS_Random.RandomColor();
panelImage.color = JCS_Random.PickColor();
}

// assign to slide panel holder.
Expand Down
15 changes: 14 additions & 1 deletion Assets/JCSUnity/Scripts/Util/JCS_Random.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Copyright (c) 2017 by Shen, Jen-Chieh $
*/
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

namespace JCSUnity
Expand Down Expand Up @@ -59,7 +60,7 @@ public static float RangeInclude(float min, float max)
/// Return a random color.
/// </summary>
/// <returns> random color object. </returns>
public static Color RandomColor(float a = 1.0f)
public static Color PickColor(float a = 1.0f)
{
return new Color(Range(0.0f, 1.0f), Range(0.0f, 1.0f), Range(0.0f, 1.0f), a);
}
Expand Down Expand Up @@ -103,5 +104,17 @@ public static T ChooseOneE<T>(params T[] args) // Ellipsis
int index = Range(0, args.Length);
return args[index];
}

/// <summary>
/// Return a random enum value.
/// </summary>
/// <typeparam name="T"> The enum type. </typeparam>
/// <returns> The enum value that has been randomly chosen. </returns>
public static T EnumValue<T>()
{
var r = new System.Random();
var v = System.Enum.GetValues(typeof(T));
return (T)v.GetValue(r.Next(v.Length));
}
}
}
3 changes: 2 additions & 1 deletion docs/ScriptReference/Util/JCS_Random.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Random library class.
|:-------------|:--------------------------------------------|
| Range | Default random range wrapper. |
| RaneeInclude | Include the maxinum number. |
| RandomColor | Returns a random color. |
| PickColor | Returns a random color. |
| ChooseOne | Choose one object from the list. |
| ChooseOneE | Choose one object from the list. (Ellipsis) |
| EnumValue | Return a random enum value. |

0 comments on commit b787172

Please sign in to comment.