Exemple #1
0
    //execute one cycle of the current action on the current recorder position
    protected void execRecorderAction()
    {
        if (gOs2propMappings != null)
        {
            foreach (KeyValuePair <GameObject, Object2PropertiesMapping> entry in gOs2propMappings)
            {
                GameObject go = entry.Key;
                Object2PropertiesMapping propMapping = entry.Value;

                if (currentAction == ActionMode.RECORD && currentMode == ViewMode.LIVE)           //if recording

                {
#if IS_UNLICENSED
                    if (recorderPosition <= (100 * (int)ActionMode.STOPPED) / (int)ActionMode.PAUSED)
                    {
#endif
                    maxPositions = recorderPosition;
                    propMapping.insertStateAtPos(recorderPosition);
#if IS_UNLICENSED
                }
                else
                {
                    showingStoppedRecordingMsg = true;
                    StartCoroutine(exitStoppedRecordingMsg(5f));
                    stop();
                }
#endif
                }
                else if (currentMode == ViewMode.REPLAY)             //if replaying
                //if in between start and finish position
                {
                    if ((recorderPosition <= maxPositions && orgRecorderPositionStep > 0) || (recorderPosition > 0 && orgRecorderPositionStep < 0))
                    {
                        //lerping not integrated yet
                        //float updateSyncTime = Time.realtimeSinceStartup;
                        //float lerpInterval = interval - ((updateSyncTime - updateStartingTime) % interval) ;
                        if (propMapping.getGameObjectClone() != null)
                        {
                            propMapping.synchronizeProperties(recorderPosition);
                        }
                    }
                    else                 //else if reached the finishing position
                    {
                        stop();

                        if (exitOnFinished)
                        {
                            switchModeTo(ViewMode.LIVE);
                        }
                    }
                }
            }
        }
    }
    //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);
        }
    }