Skip to content

Instantly share code, notes, and snippets.

@chuwilliamson
Created February 8, 2018 23:40
Show Gist options
  • Save chuwilliamson/c042a6c95f76d7b4040c651453f08a8d to your computer and use it in GitHub Desktop.
Save chuwilliamson/c042a6c95f76d7b4040c651453f08a8d to your computer and use it in GitHub Desktop.
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using System.Linq;
/// <summary>
/// Sometimes, it is useful to be able to run some editor script code in a project as soon as Unity launches without requiring action from the user.
/// You can do this by applying the InitializeOnLoad attribute to a class which has a static constructor.
/// A static constructor is a function with the same name as the class, declared static and without a return type or parameters.
/// </summary>
[InitializeOnLoad]
public class ScriptableObjectHelpers
{
static List<ScriptableObject> objectsToUnload;
static ScriptableObjectHelpers()
{
objectsToUnload = new List<ScriptableObject>();
EditorApplication.playModeStateChanged += EditorApplication_playModeStateChanged;
}
private static void EditorApplication_playModeStateChanged(PlayModeStateChange obj)
{
switch (obj)
{
case PlayModeStateChange.EnteredPlayMode:
objectsToUnload = Resources.LoadAll<ScriptableObject>("").ToList();
break;
case PlayModeStateChange.ExitingPlayMode:
foreach (var so in objectsToUnload)
Resources.UnloadAsset(so);
objectsToUnload.Clear();
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment