Skip to content

Instantly share code, notes, and snippets.

@yongkangchen
Created May 14, 2015 09:49
Show Gist options
  • Save yongkangchen/2b048c7490733ee050f0 to your computer and use it in GitHub Desktop.
Save yongkangchen/2b048c7490733ee050f0 to your computer and use it in GitHub Desktop.

Revisions

  1. yongkangchen created this gist May 14, 2015.
    58 changes: 58 additions & 0 deletions test.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,58 @@
    using UnityEngine;

    public class Main : MonoBehaviour {
    void Start ()
    {
    FakeObject obj = new FakeObject ();
    Debug.LogError (obj); // [FakeObject]
    Debug.LogError (obj == null); // false
    Debug.LogError (obj.num); //100

    FakeObject.DestroyImmediate (obj);

    Debug.LogError (obj); // null
    Debug.LogError (obj == null);//true
    Debug.LogError (obj.num); //100
    }
    }


    class FakeObject
    {
    public int num = 100;
    private bool destroyed = false;
    public static void DestroyImmediate(FakeObject obj)
    {
    obj.destroyed = true;
    }

    public override string ToString ()
    {
    if (destroyed)
    {
    return "null";
    }
    return string.Format ("[FakeObject]");
    }

    private static bool CompareBaseObjects (FakeObject x, FakeObject y)
    {
    if ((object)x != null && x.destroyed) {
    x = null;
    }
    if ((object)y != null && y.destroyed) {
    y = null;
    }
    return object.Equals (x, y);
    }

    public static bool operator == (FakeObject x, FakeObject y)
    {
    return CompareBaseObjects (x, y);
    }

    public static bool operator != (FakeObject x, FakeObject y)
    {
    return !CompareBaseObjects (x, y);
    }
    }