Unity回放系统

前言

用了一些视屏录制插件,不过文件读比较大,后来改成记录事件状态,文件就比较小了,在使用Probuff来传输数据,回放的时候就不会有卡顿。

ReTenderPlay.cs

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using GGTPlugin;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using Google.Protobuf;
using Tenders.Protobuf;

/// 
/// 回放
/// 
public class ReTenderPlay : MonoBehaviour
{

    public List ReparenetGameObjects;
    public List RePlayGameObjects;

    public List putlab;

    //    public Hashtable ht;
    //    public Hashtable ht1;
    //    public Hashtable reht;
    //    public Hashtable reht1;
    //    public int maxFrame;
    //    public int reMaxFrame;
    public DateTime date;
    public DateTime reDate;
    public string SGYQ;
    public string filepath;
    public static ReTenderPlay instance;
    public float time;
    public int step;
    public bool flag = false;
    public bool putflag;
    public ShowTianqi st;
    public ReTenderplayWnd rw;
    public Image zhezhao;
    public ReTenderplayData Hd = new ReTenderplayData();                      //记录回放数据
    public ReTenderplayData rd = new ReTenderplayData();                      //加载回放数据

    public bool flag_cun = false;
    public int zengliang;                           //每一个回放的郑亮
    public int ZhenSum;                             //每一个回放的总帧数
    public int CurrentmaxZhen;                      //播放开始帧数
    public static ReTenderPlay _instance;
    void Awake()
    {
        _instance = this;
    }
    public void Start()
    {
        instance = this;

        // ht = new Hashtable();
        foreach (var VARIABLE1 in ReparenetGameObjects.SelectMany(VARIABLE => VARIABLE.GetComponentsInChildren(true)))
        {
            RePlayGameObjects.Add(VARIABLE1.gameObject);
        }
        foreach (var VARIABLE1 in ReparenetGameObjects.SelectMany(VARIABLE => VARIABLE.GetComponentsInChildren(true)))
        {
            RePlayGameObjects.Add(VARIABLE1.gameObject);
        }
        foreach (var VARIABLE1 in ReparenetGameObjects.SelectMany(VARIABLE => VARIABLE.GetComponentsInChildren

TenderClickTrigger.cs

using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class TenderClickTrigger : MonoBehaviour, IPointerClickHandler, IPointerUpHandler
{

    void Update()
    {

    }

    public void OnPointerClick(PointerEventData eventData)
    {
        if(UIControl._instance.IsPlayBack&&ReTenderPlay._instance.flag==false)
        {
            //某些button不需要触发点击事件
            if (gameObject.GetComponent

cameraFlyYJJ.cs

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.EventSystems;

public class cameraFlyYJJ : MonoBehaviour
{

    enum Axes { MouseXandY, MouseX, MouseY }
    Axes Axis = Axes.MouseXandY;
    [Tooltip("摄像机旋转速度")]
    public float sensitivityX = 15.0f;               //旋转速度
    public float sensitivityY = 15.0f;

    float minimumX = -360.0f;
    float maximumX = 360.0f;

    float minimumY = -150.0f;
    float maximumY = 60.0f;
    public float rotationX = 0.0f;
    public float rotationY = 0.0f;

    float traY = 0.0f;
    float traX = 0.0f;

    [Tooltip("中键移动速度")]
    public float speed = 200.0f;
    //public float traXSpeed = 250.0f;               //按中建之后摄像机的移动速度
    //public float traYSpeed = 100.0f;               //按中建之后摄像机的移动速度

    [Tooltip("方向键速度")]
    public float tXSpeed = 1.0f;                   //方向键速度
    public float tZSpeed = 1.0f;

    public GameObject cameraReference;
    [HideInInspector]
    public TerrainData td;
    [HideInInspector]
    public Terrain ter;
    Vector3 Pos;
    public static cameraFlyYJJ _insance;
    void Awake()
    {
        _insance = this;
    }
    void Update()
    {
        if (UIControl._instance.IsPlayBack && ReTenderPlay._instance.flag == false)
        {
            //当摄像机的坐标和旋转发生改变时记录
            if (cameraReference.transform.localPosition != Pos)
            {
                ReTenderPlay.instance.Hd.Ht.Add(ReTenderPlay._instance.ZhenSum + "_" + ReTenderPlay.instance.zengliang++, "[p]" + cameraReference.transform.localPosition.ToString()
                    + "|" + "[r]" + cameraReference.transform.localEulerAngles);
                Pos = cameraReference.transform.localPosition;
            }
        }

        if (EventSystem.current.IsPointerOverGameObject())
        {
            return;
        }

        traY = 0.0f;
        traX = 0.0f;
        if (Input.GetAxis("Mouse ScrollWheel") != 0)
        {
            float distance;
            distance = Input.GetAxis("Mouse ScrollWheel") * speed;
            float height = TerrainControl._instance.TaVector3(cameraReference.transform.localPosition);
            Pos = cameraReference.transform.localPosition;
            if (Pos.y <= height + 40)
            {
                if (distance < 0)
                {
                    cameraReference.transform.localPosition += cameraReference.transform.localRotation * new Vector3(0, 0, distance * 0.5f);
                }
            }
            else
            {
                cameraReference.transform.localPosition += cameraReference.transform.localRotation * new Vector3(0, 0, distance * 0.5f);
            }
        }
        if (Input.GetMouseButton(1))
        {
            if (UIControl._instance.CameraRig != null)
            {
                UIControl._instance.CameraRig.isKinematic = false;
            }
            float translationZ = Input.GetAxis("Vertical");
            cameraReference.transform.localPosition += cameraReference.transform.localRotation * new Vector3(0, 0, translationZ * 0.5f * tZSpeed);
            float translationX = Input.GetAxis("Horizontal");
            cameraReference.transform.localPosition += cameraReference.transform.localRotation * new Vector3(translationX * 0.5f * tXSpeed, 0, 0);
            if (UIControl._instance.IsPlayBack && ReTenderPlay._instance.flag == false)
            {
                ReTenderPlay.instance.Hd.Ht.Add(ReTenderPlay._instance.ZhenSum + "_" + ReTenderPlay.instance.zengliang++, "[p]" + cameraReference.transform.localPosition.ToString()
                + "|" + "[r]" + cameraReference.transform.localEulerAngles);
            }
        }
        if (Input.GetMouseButton(1))
        {
            if (Axis == Axes.MouseXandY)
            {

                rotationX += Input.GetAxis("Mouse X") * sensitivityX;
                rotationY += Input.GetAxis("Mouse Y") * sensitivityY;

                Adjust360andClamp();

                KeyLookAround();

                KeyLookUp();
            }
            else if (Axis == Axes.MouseX)
            {
                rotationX += Input.GetAxis("Mouse X") * sensitivityX;

                Adjust360andClamp();

                KeyLookAround();

                KeyLookUp();
            }
            else
            {
                rotationY += Input.GetAxis("Mouse Y") * sensitivityY;

                Adjust360andClamp();

                KeyLookAround();

                KeyLookUp();
            }
            if (UIControl._instance.IsPlayBack && ReTenderPlay._instance.flag == false)
            {
                ReTenderPlay.instance.Hd.Ht.Add(ReTenderPlay._instance.ZhenSum + "_" + ReTenderPlay.instance.zengliang++, "[p]" + cameraReference.transform.localPosition.ToString()
                + "|" + "[r]" + cameraReference.transform.localEulerAngles);
            }
        }
        if (td != null)
        {

            float h = ter.SampleHeight(transform.position) + 1.8f;
            if (transform.position.y < h)
            {
                transform.position = new Vector3(transform.position.x, h, transform.position.z);
            }
        }
    }

    void KeyLookAround()
    {
        Adjust360andClamp();

        transform.localRotation = Quaternion.AngleAxis(rotationX, Vector3.up);
    }

    void KeyLookUp()
    {
        Adjust360andClamp();

        transform.localRotation *= Quaternion.AngleAxis(rotationY, Vector3.left);
    }

    void Adjust360andClamp()
    {

        if (rotationX < -360)
        {
            rotationX += 360;
        }
        else if (rotationX > 360)
        {
            rotationX -= 360;
        }

        if (rotationY < -360)
        {
            rotationY += 360;
        }
        else if (rotationY > 360)
        {
            rotationY -= 360;
        }

        rotationX = Mathf.Clamp(rotationX, minimumX, maximumX);
        rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
    }

    void Start()
    {
        Time.timeScale = 1.0f;

        if (GetComponent())
        {
            GetComponent().freezeRotation = true;
        }
        if (ter != null)
        {
            td = ter.terrainData;
        }
    }
}

TenderInputAdlistener.cd

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class TenderInputAdlistener : MonoBehaviour {

    void Start()
    {
        if(gameObject.GetComponent()!=null)
        {
            gameObject.GetComponent().onEndEdit.AddListener(delegate
                {
                    if(UIControl._instance.IsPlayBack&&ReTenderPlay._instance.flag==false)
                    {
                        ReTenderPlay.instance.Hd.Ht.Add(ReTenderPlay._instance.ZhenSum + "_" + ReTenderPlay.instance.zengliang++, "[i]" + gameObject.GetComponent().ID + "|" + "[n]" + gameObject.GetComponent().text);
                    //Debug.Log("InputField="+ gameObject.GetInstanceID().ToString());

                    }
                });
        }
    
        if(gameObject.GetComponent()!=null)
        {
            gameObject.GetComponent().onValueChanged.AddListener(delegate
                {
                    if(UIControl._instance.IsPlayBack&&ReTenderPlay._instance.flag==false)
                    {
                        ReTenderPlay.instance.Hd.Ht.Add(ReTenderPlay._instance.ZhenSum + "_" + ReTenderPlay.instance.zengliang++, "[SC]" + gameObject.GetComponent().ID + "|" + "[SV]" + (float) gameObject.GetComponent().value);

                    //Debug.Log("Scrollbar="+ gameObject.GetComponent().value.ToString());

                    }
                });
        }
        if(gameObject.GetComponent()!=null)
        {
            gameObject.GetComponent().onValueChanged.AddListener(delegate
                {
                    if(UIControl._instance.IsPlayBack&&ReTenderPlay._instance.flag==false)
                    {
                        ReTenderPlay.instance.Hd.Ht.Add(ReTenderPlay._instance.ZhenSum + "_" + ReTenderPlay.instance.zengliang++, "[DC]" + gameObject.GetComponent().ID + "|" + "[DV]" + (int) gameObject.GetComponent().value);
                        Debug.Log("Dropdown  =  "+gameObject.GetComponent().value.ToString());
                        //Debug.Log("Scrollbar="+ gameObject.GetComponent().value.ToString());

                    }
                });
        }
    }
}

proto生成:链接:http://pan.baidu.com/s/1slnP5Pr 密码:a56f

你可能感兴趣的:(Unity回放系统)