A couple of attributes that help when Domain Reloading is disabled, which significantly decreases the time it takes for Unity to play a scene. By default Unity provides RuntimeInitializeOnLoadMethod attribute to assist but it can be a little cumbersome. Here are a few helpful additions!
Use the ClearOnReload
attribute on static fields that you wish to reset on playmode. You can either "clear" the field (set the value to default), set it to a specified value, or make it assign itself a new instance of its type using a default constructor.
Use the ExecuteOnReload
attribute on static methods that you want to execute when entering play mode with domain reloading disabled.
public class CharacterManager : MonoBehaviour
{
// Will set value to default (null).
[ClearOnReload]
static CharacterManager instance;
// Will set variable to given value (10).
[ClearOnReload(valueToAssign=10)]
static int startsAsTen;
// Will reset value, creating a new instance using default constructor.
[ClearOnReload(assignNewTypeInstance=true)]
static CharacterManager myNeverNullManager;
// Will execute this method.
[ExecuteOnReload]
static void RunThis()
{
Debug.Log("Clean up here.")
}
// Does not work on properties!
// [ClearOnReload]
static int number { get; set; }
// Does not work on events!
// [ClearOnReload]
static event Action onDoSomething;
// However, one can use ExecuteOnReload to do their own clean up.
[ExecuteOnReload]
static void CleanUpEvents()
{
foreach(Delegate d in onDoSomething.GetInvocationList())
onDoSomething -= d;
}
}
TypeCache makes finding attributes fast; however, it only supports finding fields, methods, types, and derived types.
This project is released under the MIT license.
This project is written by Josh Steinhauer with contributions from Yevhen Bondarenko and Shane Celis.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。