Skip to content

Instantly share code, notes, and snippets.

@joshcamas
Created March 18, 2019 14:44
Show Gist options
  • Save joshcamas/d72622deb2cd59387e714f4d4b77ce1e to your computer and use it in GitHub Desktop.
Save joshcamas/d72622deb2cd59387e714f4d4b77ce1e to your computer and use it in GitHub Desktop.
Simple Utility script for Unity3D that allows access for "IsDirty" method for gameobjects.
using UnityEngine;
using UnityEditor;
using System.Reflection;
using System;
//Special thanks to Shamanim for solution
//https://forum.unity.com/threads/sorta-solved-check-if-gameobject-is-dirty.504894/#post-3967810
public class IsDirtyUtility
{
//Cached Value
private static Func<int, bool> _isDirtyCallback;
private static Func<int, bool> IsDirtyCallback
{
get
{
if(_isDirtyCallback == null)
{
//Reflection
MethodInfo isDirtyMethod = typeof(EditorUtility).GetMethod("IsDirty", BindingFlags.NonPublic | BindingFlags.Static);
_isDirtyCallback = (Func<int, bool>)Delegate.CreateDelegate(typeof(Func<int, bool>), null, isDirtyMethod);
}
return _isDirtyCallback;
}
}
public static bool IsDirty(int instanceID)
{
return IsDirtyCallback(instanceID);
}
public static bool IsDirty(GameObject gameObject)
{
return IsDirtyCallback(gameObject.GetInstanceID());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment