Skip to content

Instantly share code, notes, and snippets.

@dimmduh
Created September 24, 2018 09:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dimmduh/07fdccfb4dd72d76ea06595f0ab76148 to your computer and use it in GitHub Desktop.
Save dimmduh/07fdccfb4dd72d76ea06595f0ab76148 to your computer and use it in GitHub Desktop.
Unity - reset all static properties
public class MyUtils
{
public static void ResetAllStaticsVariables(Type type)
{
var fields = type.GetFields(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy | BindingFlags.Public);
foreach (var fieldInfo in fields)
{
fieldInfo.SetValue(null, GetDefault(type));
}
}
public static object GetDefault(Type type)
{
if (type.IsValueType)
{
return Activator.CreateInstance(type);
}
return null;
}
}
@dimmduh
Copy link
Author

dimmduh commented Sep 24, 2018

Example
MyUtils.ResetAllStaticsVariables(typeof(MyMonoBehaviour));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment