// All the public API is static so you need not worry about creating an instance public static bool Add(GuidComponent guidComponent) { if (Instance == null) { Instance = new GuidManager(); } return(Instance.InternalAdd(guidComponent)); }
private bool InternalAdd(GuidComponent guidComponent) { Guid guid = guidComponent.GetGuid(); GuidInfo info = new GuidInfo(guidComponent); if (!guidToObjectMap.ContainsKey(guid)) { guidToObjectMap.Add(guid, info); return(true); } GuidInfo existingInfo = guidToObjectMap[guid]; if (existingInfo.go != null && existingInfo.go != guidComponent.gameObject) { // normally, a duplicate GUID is a big problem, means you won't necessarily be referencing what you expect if (Application.isPlaying) { Debug.AssertFormat(false, guidComponent, "Guid Collision Detected between {0} and {1}.\nAssigning new Guid. Consider tracking runtime instances using a direct reference or other method.", (guidToObjectMap[guid].go != null ? guidToObjectMap[guid].go.name : "NULL"), (guidComponent != null ? guidComponent.name : "NULL")); } else { // however, at editor time, copying an object with a GUID will duplicate the GUID resulting in a collision and repair. // we warn about this just for pedantry reasons, and so you can detect if you are unexpectedly copying these components Debug.LogWarningFormat(guidComponent, "Guid Collision Detected while creating {0}.\nAssigning new Guid.", (guidComponent != null ? guidComponent.name : "NULL")); } return(false); } // if we already tried to find this GUID, but haven't set the game object to anything specific, copy any OnAdd callbacks then call them existingInfo.go = info.go; existingInfo.HandleAddCallback(); guidToObjectMap[guid] = existingInfo; return(true); }
public GuidInfo(GuidComponent comp) { go = comp.gameObject; OnRemove = null; OnAdd = null; }