Skip to content

Instantly share code, notes, and snippets.

@FadrikAlexander
Last active October 9, 2023 05:03
Show Gist options
  • Save FadrikAlexander/a7cdcb3bd470947ed705e95de71b099e to your computer and use it in GitHub Desktop.
Save FadrikAlexander/a7cdcb3bd470947ed705e95de71b099e to your computer and use it in GitHub Desktop.
A Simple Random Seed System in Unity C#
/*
Created by: Fadrik Alexander
Full Youtube Tutorial on this Script: https://youtu.be/HVueQ2ikOQc
Please note that the seed should be set before any randomization so this has to be called first
*/
using UnityEngine;
public class RandomSeedController : MonoBehaviour
{
string currentSeed; //Current Loaded Seed
public string GetCurrentSeed() { return currentSeed; }
private void Awake() => GenerateRandomSeed();
//Generate Random seed for the system
public void GenerateRandomSeed()
{
int tempSeed = (int)System.DateTime.Now.Ticks;
currentSeed = tempSeed.ToString();
Random.InitState(tempSeed);
}
//Select the Seed for the System
public void SetRandomSeed(string seed = "")
{
currentSeed = seed;
int tempSeed = 0;
if (isNumeric(seed))
tempSeed = System.Int32.Parse(seed);
else
tempSeed = seed.GetHashCode();
Random.InitState(tempSeed);
}
public void SetRandomSeed(int seed)
{
currentSeed = seed.ToString();
int tempSeed = seed;
Random.InitState(tempSeed);
}
//Copy Seed to Clipboard
public void CopySeedToClipboard() => GUIUtility.systemCopyBuffer = currentSeed;
//Check if Seed is All numbers
public static bool isNumeric(string s)
{
return int.TryParse(s, out int n);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment