Created
September 24, 2018 09:16
-
-
Save dimmduh/07fdccfb4dd72d76ea06595f0ab76148 to your computer and use it in GitHub Desktop.
Unity - reset all static properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example
MyUtils.ResetAllStaticsVariables(typeof(MyMonoBehaviour));