Exemple #1
0
        private bool InternalAdd(GuidComponent guidComponent)
        {
            Guid guid = guidComponent.GetGuid();

            GuidInfo info = new GuidInfo(guidComponent);

            if (this.guidToObjectMap.ContainsKey(guid) == false)
            {
                this.guidToObjectMap.Add(guid, info);
                return(true);
            }

            GuidInfo existingInfo = this.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.",
                        this.guidToObjectMap[guid].GO != null ? this.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();

            this.guidToObjectMap[guid] = existingInfo;

            return(true);
        }
        public bool Add(Guid guid, GameObject gameObject)
        {
            if (guid == Guid.Empty)
            {
                throw new ArgumentNullException(nameof(guid), "There was an attempt to add empty Guid.");
            }
            if (gameObject == null)
            {
                throw new ArgumentNullException(nameof(gameObject), "There was an attempt to add NULL GameObject.");
            }

            GuidInfo info = new GuidInfo(gameObject);

            if (!guidToObjectMap.ContainsKey(guid))
            {
                guidToObjectMap.Add(guid, info);
                return(true);
            }

            GuidInfo existingInfo = guidToObjectMap[guid];

            if (existingInfo.gameObject != null && existingInfo.gameObject != 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, gameObject, "Guid Collision Detected between {0} and {1}.\nAssigning new Guid. Consider tracking runtime instances using a direct reference or other method.", (guidToObjectMap[guid].gameObject != null ? guidToObjectMap[guid].gameObject.name : "NULL"), (gameObject != null ? gameObject.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(gameObject, "Guid Collision Detected while creating {0}.\nAssigning new Guid.", (gameObject != null ? gameObject.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.gameObject = info.gameObject;
            existingInfo.HandleAddCallback();
            guidToObjectMap[guid] = existingInfo;
            return(true);
        }