Skip to content

Instantly share code, notes, and snippets.

@zhenlinyang
Created April 4, 2017 12:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhenlinyang/5608fa760c1254ad02ae41e8abf98b94 to your computer and use it in GitHub Desktop.
Save zhenlinyang/5608fa760c1254ad02ae41e8abf98b94 to your computer and use it in GitHub Desktop.
UnityEngine Simulation Object
namespace UnityEngine.Simulation
{
public class Object
{
private static int s_IdCount = 0;
private int m_InstanceID;
private bool m_Alive = true;
private string m_Name = "";
public Object()
{
do
{
m_InstanceID = ++s_IdCount;
} while (0 == m_InstanceID);
}
public static void Destroy(Object obj)
{
obj.m_Alive = false;
}
public override bool Equals(object other)
{
Object obj = other as Object;
return (!(obj == null) || other == null || other is Object) && Compare(this, obj);
}
public override int GetHashCode()
{
return m_InstanceID;
}
public static bool operator ==(Object x, Object y)
{
return Compare(x, y);
}
public static bool operator !=(Object x, Object y)
{
return !Compare(x, y);
}
public static implicit operator bool(Object exists)
{
return !Compare(exists, null);
}
private static bool Compare(Object x, Object y)
{
bool flagX = !(x is Object);
bool flagY = !(y is Object);
bool result;
if (flagX && flagY)
{
return true;
}
else if (flagY)
{
result = !x.m_Alive;
}
else if (flagX)
{
result = !y.m_Alive;
}
else
{
result = x.m_InstanceID == y.m_InstanceID;
}
return result;
}
public string Name
{
get
{
if (m_Alive)
{
return m_Name;
}
else
{
throw new UnityEngine.MissingReferenceException(string.Format(
"The object of type '{0}' has been destroyed but you are still trying to access it.\nYour script should either check if it is null or you should not destroy the object.",
GetType()));
}
}
set
{
if (m_Alive)
{
m_Name = value;
}
else
{
throw new UnityEngine.MissingReferenceException(string.Format(
"The object of type '{0}' has been destroyed but you are still trying to access it.\nYour script should either check if it is null or you should not destroy the object.",
GetType()));
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment