Ejemplo n.º 1
0
        /// <summary>
        ///     Instantiate the Doppeganger recursively. Makes a fully copy of all visual
        ///     components of the object, discarding non-visual components.
        /// </summary>
        /// <param name="currentLevel">Current level to target</param>
        private void InstantiateDoppleganger(Transform currentLevel)
        {
            var other = SKSGeneralUtils.FindAnalogousTransform(currentLevel, Doppleganger.transform, Root, true);

            //Remove the MainCamera tag if it's been erroniously copied.
            if (currentLevel.tag.Equals("MainCamera"))
            {
                currentLevel.tag = Keywords.Tags.Untagged;
            }

            currentLevel.gameObject.name = currentLevel.gameObject.name;
            foreach (var component in currentLevel.GetComponents <Component>())
            {
                if (component is Teleportable)
                {
                    component.SafeDestroyComponent();
                }
                //Copies Transforms for later updating
                else if (component is Transform)
                {
                    if (other)
                    {
                        if (!Transforms.ContainsKey(other))
                        {
                            Transforms.Add(other, (Transform)component);
                        }
                    }
                    else
                    {
                        Destroy(currentLevel.gameObject);
                        break;
                    }
                }
                else if (component is Renderer)
                {
                    if (component is SkinnedMeshRenderer)
                    {
                        SkinnedRenderers.Add(component as SkinnedMeshRenderer);
                    }

                    var meshRenderer = component as MeshRenderer;
                    if (meshRenderer != null)
                    {
                        var otherRend = other.GetComponent <MeshRenderer>();
                        if (!Renderers.ContainsKey(otherRend))
                        {
                            Renderers[otherRend] = meshRenderer;
                        }
                        //Adds colliders to list for collision ignoring upon Portal entry
                    }
                    else
                    {
                        var otherRend = other.GetComponent <Renderer>();
                        if (!Renderers.ContainsKey(otherRend))
                        {
                            Renderers[otherRend] = (Renderer)component;
                        }
                        //Adds colliders to list for collision ignoring upon Portal entry
                    }
                }
                else if (component is Collider)
                {
                    if (!component.GetComponent <TeleportablePhysExclude>())
                    {
                        var c = other.GetComponent <Collider>();
                        if (!_colliders.ContainsKey(c.GetInstanceID()))
                        {
                            _colliders.Add(c.GetInstanceID(), c);
                        }
#if SKS_VR
                        else
                        {
                            //Fix for VRTK double-genning body colliders for no reason

                            currentLevel.gameObject.transform.SetParent(null);
                            c.enabled = false;
                            int key = c.GetInstanceID();
                            _colliders[key].enabled   = false;
                            c.isTrigger               = true;
                            _colliders[key].isTrigger = true;
                            DestroyImmediate(_colliders[key].gameObject);
                            DestroyImmediate(currentLevel.gameObject);
                            continue;
                        }
#endif
                    }

                    if (StripColliders && component)
                    {
                        component.SafeDestroyComponent();
                    }
                }
                else if (component is Rigidbody)
                {
                    if (StripRigidbodies)
                    {
                        component.SafeDestroyComponent();
                    }
                }
                else if (component is Joint)
                {
                    if (StripJoints)
                    {
                        component.SafeDestroyComponent();
                    }
                }
                else if (component is MonoBehaviour)
                {
                    //Handling of teleportable scripts
                    if (component is TeleportableScript)
                    {
                        TeleportableScripts.Add(other.GetComponent <TeleportableScript>());
                    }

                    if (!StripScripts)
                    {
                        ((MonoBehaviour)component).enabled = true;
                    }
                    else
                    {
                        component.SafeDestroyComponent();
                    }
                    //Nonspecific setup copying
                }
                else
                {
                    var system = component as ParticleSystem;
                    if (system != null)
                    {
                        var otherSystem = other.GetComponent <ParticleSystem>();
                        system.randomSeed = otherSystem.randomSeed;
                        system.time       = otherSystem.time;
                    }
                    else if (component is MeshFilter || component is Light)
                    {
                        //nothin to do
                    }
                    else
                    {
                        component.SafeDestroyComponent();
                    }
                }
            }

            if (other)
            {
                currentLevel.gameObject.SetActive(other.gameObject.activeSelf);
            }

            foreach (Transform t in currentLevel)
            {
                InstantiateDoppleganger(t);
            }
        }
Ejemplo n.º 2
0
        private void InstantiateDoppleganger(Transform currentLevel)
        {
            Transform other = SKSGeneralUtils.FindAnalogousTransform(currentLevel, Doppleganger.transform, root, true);

            if (currentLevel.tag.Equals("MainCamera"))
            {
                currentLevel.tag = "Untagged";
            }
            currentLevel.gameObject.name = currentLevel.gameObject.name;
            foreach (Component component in currentLevel.GetComponents <Component>())
            {
                if (component is Teleportable)
                {
                    component.SafeDestroyComponent();
                }
                //Copies Transforms for later updating
                else if (component is Transform)
                {
                    if (other)
                    {
                        if (!Transforms.ContainsKey(other))
                        {
                            Transforms.Add(other, (Transform)component);
                        }
                    }
                    else
                    {
                        Destroy(currentLevel.gameObject);
                        break;
                    }
                }
                else if (component is Renderer)
                {
                    if (component is SkinnedMeshRenderer)
                    {
                        SkinnedRenderers.Add(component as SkinnedMeshRenderer);
                    }
                    if (component is Renderer)
                    {
                        if (!Renderers.ContainsKey((Renderer)component))
                        {
                            Renderers[other.GetComponent <Renderer>()] = (Renderer)component;
                        }
                        //Adds colliders to list for collision ignoring upon Portal entry
                    }
                }
                else if (component is Collider)
                {
                    if (!component.GetComponent <TeleportablePhysExclude>())
                    {
                        Collider c = other.GetComponent <Collider>();
                        if (!Colliders.ContainsKey(c.GetInstanceID()))
                        {
                            Colliders.Add(c.GetInstanceID(), c);
                        }
                        else
                        {
                            //Fix for VRTK double-genning body colliders for no reason
                            currentLevel.gameObject.transform.SetParent(null);
                            c.enabled = false;
                            Colliders[c.GetInstanceID()].enabled = false;
                            c.isTrigger = true;
                            Colliders[c.GetInstanceID()].isTrigger = true;
                            DestroyImmediate(Colliders[c.GetInstanceID()].gameObject);
                            DestroyImmediate(currentLevel.gameObject);
                            return;
                        }
                    }
                    if (StripColliders && component)
                    {
                        component.SafeDestroyComponent();
                    }
                }
                else if (component is Rigidbody)
                {
                    if (StripRigidbodies)
                    {
                        component.SafeDestroyComponent();
                    }
                }
                else if (component is Joint)
                {
                    if (StripJoints)
                    {
                        component.SafeDestroyComponent();
                    }
                }
                else if (component is MonoBehaviour)
                {
                    //Handling of teleportable scripts
                    if (component is TeleportableScript)
                    {
                        TeleportableScripts.Add(other.GetComponent <TeleportableScript>());
                    }

                    if (!StripScripts)
                    {
                        if (component != null)
                        {
                            ((MonoBehaviour)component).enabled = true;
                        }
                    }
                    else
                    {
                        component.SafeDestroyComponent();
                    }
                    //Nonspecific setup copying
                }
                else if (component is MeshFilter || component is ParticleSystem || component is Light)
                {
                    //nothin to do
                }
                else
                {
                    component.SafeDestroyComponent();
                }
            }

            if (other)
            {
                currentLevel.gameObject.SetActive(other.gameObject.activeSelf);
            }

            foreach (Transform t in currentLevel)
            {
                InstantiateDoppleganger(t);
            }
        }