Пример #1
0
    //EZReplayManager callback for start of replay-mode:
    public void __EZR_replay_ready()
    {
        if (positions.Count < 1)
        {
            EZR_Clone cloneScript = GetComponent <EZR_Clone>();

            GameObject go = null;
            if (cloneScript.origInstanceID > -1)
            {
                go = EZReplayManager.get.instanceIDtoGO[cloneScript.origInstanceID];
            }

            if (go != null)
            {
                positions = go.GetComponent <DrawLines>().positions;
            }
        }
    }
    //executed before each replay
    public void prepareObjectForReplay()
    {
        //spawn super object which gets all replay manager objects as children
        GameObject superParent = GameObject.Find(EZReplayManager.S_PARENT_NAME);

        //create super parent if has not happened. The super parent keeps the scene clean
        if (superParent == null)
        {
            superParent = new GameObject(EZReplayManager.S_PARENT_NAME);
            superParent.transform.position   = Vector3.zero;
            superParent.transform.rotation   = Quaternion.identity;
            superParent.transform.localScale = Vector3.one;
        }

        if (isParentObj)           //if is a parent gameObject mapping

        {
            if (prefabLoadPath == "")
            {
                gameObjectClone = (GameObject)GameObject.Instantiate(gameObject, gameObject.transform.position, gameObject.transform.rotation);
            }
            else
            {
                try {
                    gameObjectClone = (GameObject)GameObject.Instantiate(Resources.Load(prefabLoadPath));
                } catch (ArgumentException) {
                }
            }

            gameObjectClone.transform.parent = superParent.transform;
        }
        else             // if is a child (can also be a parent in game scene hierachy but "EZReplayManager.mark4recording()" has not been called for this object specifically, so we handle it as a child

        {
            GameObject  myParentClone = parentMapping.getGameObjectClone();
            Transform[] allChildren   = myParentClone.GetComponentsInChildren <Transform>(true);

            for (int i = 0; i < allChildren.Length; i++)
            {
                GameObject child = allChildren[i].gameObject;
                //map child to order number or go-name
                if ((childIdentificationMode == ChildIdentificationMode.IDENTIFY_BY_ORDER && i == childNo) ||
                    (childIdentificationMode == ChildIdentificationMode.IDENTIFY_BY_NAME && gameObjectName == child.name))
                {
                    gameObjectClone = child;
                    break;
                }
            }

            if (gameObjectClone == null)               //child was destroyed along the way while recording
            {
                if (EZReplayManager.get.precacheGameobjects)
                {
                    gameObjectClone = (GameObject)GameObject.Instantiate(Resources.Load(EZReplayManager.get.generateCachePath(gameObjectName, "")));
                }
            }
        }

        gameObjectClone.name = gameObjectInstanceID + "_" + gameObjectClone.GetInstanceID() + "_" + gameObjectClone.name;

        if (gameObjectInstanceID > -1)         // can happen when file was loaded. obviously this doesn't work with loaded files yet.
        {
            EZReplayManager.get.instanceIDtoGO.Add(gameObjectInstanceID, gameObject);
        }

        // kill all unneccessary scripts on gameObjectClone
        Component[] allComps = gameObjectClone.GetComponentsInChildren <Component>(true);

        List <Component> componentsToKill = new List <Component>();

        foreach (Component comp in allComps)
        {
            //Exclude scripts and components from removal: (this is done to preserve basic functionality and renderers)
            if (comp != comp.GetComponent <Transform>() &&
                comp != comp.GetComponent <MeshFilter>() &&
                comp != comp.GetComponent <MeshRenderer>() &&
                comp != comp.GetComponent <SkinnedMeshRenderer>() &&
                comp != comp.GetComponent <Camera>() &&
                comp != comp.GetComponent <GUILayer>() &&
                comp != comp.GetComponent <AudioListener>() &&
                comp != comp.GetComponent <SpriteRenderer>() &&
                comp != comp.GetComponent("FlareLayer")
                )
            {
                bool found = false;
                // take exceptions from public array "EZReplayManager.componentsAndScriptsToKeepAtReplay"
                for (int i = 0; i < EZReplayManager.get.componentsAndScriptsToKeepAtReplay.Count; i++)
                {
                    if (comp == comp.GetComponent(EZReplayManager.get.componentsAndScriptsToKeepAtReplay[i]))
                    {
                        found = true;
                        break;
                    }
                }

                if (!found)
                {
                    componentsToKill.Add(comp);
                }
            }
        }
        //uses multiple cycles to kill components which are required by others
        int cycles = 0;

        do
        {
            List <Component> componentsToKillNew = componentsToKill;
            for (int i = 0; i < componentsToKill.Count; i++)
            {
                Component comp = componentsToKill[i];

                try {
                    GameObject.DestroyImmediate(comp);
                } finally {
                    if (comp == null)
                    {
                        componentsToKillNew.RemoveAt(i);
                    }
                    else                         //change order
                    {
                        componentsToKillNew.Remove(comp);
                        componentsToKillNew.Add(comp);
                    }
                }
            }

            componentsToKill = componentsToKillNew;
            cycles++;
        } while (componentsToKill.Count > 0 && cycles <= 10);

        EZR_Clone thisCloneScript = gameObjectClone.AddComponent <EZR_Clone>();

        thisCloneScript.origInstanceID  = gameObjectInstanceID;
        thisCloneScript.cloneInstanceID = gameObjectClone.GetInstanceID();

        if (EZReplayManager.get.autoDeactivateLiveObjectsOnReplay && gameObject != null)
        {
            gameObject.SetActive(false);
        }
    }