Exemplo n.º 1
0
        void ICloneExplicit.SetupCloneTargets(object targetObj, ICloneTargetSetup setup)
        {
            GameObject target        = targetObj as GameObject;
            bool       isPrefabApply = setup.Context is ApplyPrefabContext;

            if (!isPrefabApply)
            {
                // Destroy additional Components in the target GameObject
                if (target.compMap.Count > 0)
                {
                    List <Type> removeComponentTypes = null;
                    foreach (var pair in target.compMap)
                    {
                        if (!this.compMap.ContainsKey(pair.Key))
                        {
                            if (removeComponentTypes == null)
                            {
                                removeComponentTypes = new List <Type>();
                            }
                            removeComponentTypes.Add(pair.Key);
                        }
                    }
                    if (removeComponentTypes != null)
                    {
                        foreach (Type type in removeComponentTypes)
                        {
                            target.RemoveComponent(type);
                        }
                    }
                }
                // Destroy additional child objects in the target GameObject
                if (target.children != null)
                {
                    int thisChildCount = this.children != null ? this.children.Count : 0;
                    for (int i = target.children.Count - 1; i >= thisChildCount; i--)
                    {
                        target.children[i].Dispose();
                    }
                }
            }

            // Create missing Components in the target GameObject
            foreach (var pair in this.compMap)
            {
                Component targetComponent = target.AddComponent(pair.Key);
                setup.HandleObject(pair.Value, targetComponent, CloneBehavior.ChildObject);
            }
            // Create missing child objects in the target GameObject
            if (this.children != null)
            {
                for (int i = 0; i < this.children.Count; i++)
                {
                    GameObject targetChild;
                    if (target.children != null && target.children.Count > i)
                    {
                        targetChild = target.children[i];
                    }
                    else
                    {
                        targetChild = new GameObject(string.Empty, target);
                    }

                    setup.HandleObject(this.children[i], targetChild, CloneBehavior.ChildObject);
                }
            }

            // Handle referenced and child objects
            setup.HandleObject(this.scene, target.scene, CloneBehavior.WeakReference);
            setup.HandleObject(this.prefabLink, target.prefabLink);
        }