Skip to content

Instantly share code, notes, and snippets.

@lynxelia
lynxelia / EditorSceneMemoryManager.cs
Last active April 21, 2022 10:28
A Unity3d editor script that automatically forces garbage collection when opening a scene
using System;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine.SceneManagement;
[InitializeOnLoad]
public static class EditorSceneMemoryManager
{
static EditorSceneMemoryManager()
{
@lynxelia
lynxelia / ReferenceHolder.cs
Last active February 10, 2018 02:20
Example of a class that has an UnityEngine.Object field reference that will not be garbage collected
public class ReferenceHolder : MonoBehaviour
{
public Sprite m_SpriteValue;
}
@lynxelia
lynxelia / ReferenceHolder.cs
Created February 10, 2018 02:21
Example of a class that has an UnityEngine.Object field reference that will be garbage collected
public class ReferenceHolder : MonoBehaviour
{
public Sprite m_SpriteValue;
void OnDestroy()
{
m_SpriteValue = null;
}
}
@lynxelia
lynxelia / BaseMonoBehaviour.cs
Created February 10, 2018 02:24
A replacement base class to use instead of MonoBehaviour. Inheriting from this class will ensure all fields in your class will be garbage collected
public class BaseMonoBehaviour : MonoBehaviour
{
void OnDestroy()
{
foreach (FieldInfo field in GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
{
Type fieldType = field.FieldType;
if (typeof(IList).IsAssignableFrom(fieldType))
{
@lynxelia
lynxelia / Example.cs
Created February 10, 2018 02:47
Example for garbage collection bug when setting a component field during OnValidate
[RequireComponent(typeof(Rigidbody2D))]
public class Example : MonoBehaviour
{
void OnValidate()
{
GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Dynamic;
}
}
@lynxelia
lynxelia / BaseMonoBehaviour.cs
Last active February 11, 2018 06:29
A replacement base class to use instead of MonoBehaviour. Inheriting from this class will allow you to validate properties without encountering the OnValidate garbage collection bug. OnValidateProperty is also guaranteed to only execute when a change is made in the Inspector Window. It doesn't run when loading an asset into memory or loading a s…
public class BaseMonoBehaviour : MonoBehaviour
{
public virtual bool OnValidateProperty(string propertyName)
{
return false;
}
// Remember to include the OnDestroy code from Step 3 as well
}
@lynxelia
lynxelia / BaseMonoBehaviourEditor.cs
Last active February 11, 2018 06:29
A replacement base class to use instead of Editor. Inheriting from this editor class will allow you to validate properties without encountering the OnValidate garbage collection bug. OnValidateProperty is also guaranteed to only execute when a change is made in the Inspector Window. It doesn't run when loading an asset into memory or loading a s…
[CanEditMultipleObjects, CustomEditor(typeof(BaseMonoBehaviour), true)]
public class BaseMonoBehaviourEditor : Editor
{
public override void OnInspectorGUI()
{
serializedObject.Update();
SerializedProperty iterator = serializedObject.GetIterator();
bool enterChildren = true;
while (iterator.NextVisible(enterChildren))
@lynxelia
lynxelia / MyCustomClass.cs
Last active February 10, 2018 02:56
Example of a class that uses OnValidateProperty to set a component field
[RequireComponent(typeof(Rigidbody2D))]
public class MyCustomClass : BaseMonoBehaviour
{
public bool m_CanMove;
public override bool OnValidateProperty(string propertyName)
{
if (propertyName == "m_CanMove")
{
if (!m_CanMove)
@lynxelia
lynxelia / MyCustomEditor.cs
Created February 10, 2018 02:54
An example of a custom editor class that utilizes DrawProperty to override the look of a property field
[CanEditMultipleObjects, CustomEditor(typeof(MyCustomClass), true)]
public class MyCustomEditor : BaseMonoBehaviourEditor
{
protected override void DrawProperty(Rect position, SerializedProperty property, GUIContent label)
{
if (property.name == "m_CanMove")
{
// insert custom editor code for this property
}
else
@lynxelia
lynxelia / IsPlayingOrWillChangePlaymode.cs
Last active February 10, 2018 02:58
Example of how to use EditorApplication.isPlayingOrWillChangePlaymode to skip time intensive code you don’t want executed when entering play mode
#if UNITY_EDITOR
if (!EditorApplication.isPlayingOrWillChangePlaymode)
{
// time intensive code you don’t want executed when entering play mode
}
#endif