Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using UnityEngine;
- public class ObjectTracker
- {
- private class TrackedObject
- {
- private object m_Obj;
- private bool m_Initialized = false;
- private List<System.Action<object>> m_References = new List<System.Action<object>>();
- public void AddReference(System.Action<object> aRef)
- {
- if (!m_Initialized)
- m_References.Add(aRef);
- else
- aRef(m_Obj);
- }
- public void Initialize(object aObj)
- {
- if (!m_Initialized)
- {
- m_Initialized = true;
- m_Obj = aObj;
- foreach(var r in m_References)
- {
- r(m_Obj);
- }
- m_References = null;
- }
- }
- }
- // singleton
- private static ObjectTracker m_Inst;
- public static ObjectTracker Instance
- {
- get
- {
- if (m_Inst == null)
- m_Inst = new ObjectTracker();
- return m_Inst;
- }
- }
- private Dictionary<string, TrackedObject> m_Objs = new Dictionary<string, TrackedObject>();
- private TrackedObject GetTrackedObject(string aID)
- {
- TrackedObject to;
- if (!m_Objs.TryGetValue(aID, out to))
- {
- to = new TrackedObject();
- m_Objs.Add(aID, to);
- }
- return to;
- }
- public void AddObject(string aID, object aObj)
- {
- GetTrackedObject(aID).Initialize(aObj);
- }
- public void GetObject(string aID, System.Action<object> aRef)
- {
- GetTrackedObject(aID).AddReference(aRef);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment