2016-06-01 3 views
0

Unity에서 만들고있는 게임에 대한 재생 시스템이 있습니다. keyFrame 배열에있는 프레임의 프레임 버퍼까지 저장하고 프레임 버퍼를 넘은 후에 프레임을 바꿉니다. "Fire1"버튼 중 하나를 누르면. 프레임 버퍼보다 ​​많은 프레임이 저장되어 있으면 재생이 잘되지만 프레임 버퍼보다 ​​저장된 프레임이 적 으면 다시 재생되고 빈 프레임에 도달하면 프레임이 충분하지 않기 때문에 아무 것도 움직이지 않습니다 (What's Happening GIF)재생 시스템, 링 버퍼, 프레임 버퍼에 도달하기 전에 재생 시작

using UnityEngine; 
using System.Collections; 

public class ReplaySystem : MonoBehaviour 
{ 
    private const int bufferFrames = 500; 
    private MyKeyFrame[] keyFrames = new MyKeyFrame[bufferFrames]; 
    private Rigidbody rigidBody; 

    // Use this for initialization 
    void Start() 
    { 
     rigidBody = GetComponent<Rigidbody>(); 
    } 

    // Update is called once per frame 
    void Update() 
    { 
     if (Input.GetButton("Fire1")) 
      PlayBack(); 
     else 
      Record(); 
    } 

    private void PlayBack() 
    { 
     rigidBody.isKinematic = true; 
     int frame = Time.frameCount % bufferFrames; 
     Debug.Log("Reading frame " + frame); 
     transform.position = keyFrames[frame].pos; 
     transform.rotation = keyFrames[frame].rot; 

    } 

    private void Record() 
    { 
     rigidBody.isKinematic = false; 
     int frame = Time.frameCount % bufferFrames; 
     float time = Time.time; 
     Debug.Log("Writing frame " + frame); 

     keyFrames[frame] = new MyKeyFrame(time, transform.position, transform.rotation); 
    } 
} 
/// <summary> 
/// A Structure for storing time, position, and rotation 
/// </summary> 
public struct MyKeyFrame 
{ 
    public float frameTime; 
    public Vector3 pos; 
    public Quaternion rot; 

    public MyKeyFrame(float time, Vector3 pos, Quaternion rot) 
    { 
     frameTime = time; 
     this.pos = pos; 
     this.rot = rot; 
    } 
} 

은 어떻게 재생 프레임 버퍼보다 ​​저장 프레임이 적은 경우 저장된 키 프레임의 수부터 시작합니까?

은 이미 시도했다 : - 다음 모듈을

답변

0

내 솔루션을 받고 Time.frameCount에서 빼면 bufferFrames - 키 프레임의 계수의 수는 를 저장. 더 많은 조정이 필요할 수 있습니다.

using UnityEngine; 

using System.Collections;

공용 클래스 ReplaySystem : {

private const int bufferFrames = 1000; 
private MyKeyFrame[] keyFrames = new MyKeyFrame[bufferFrames]; 
private Rigidbody rigidBody; 
private GameManager gameManager; 

private bool bufferArrayFull = false; 
private bool setBufferCount = false; 
private int bufferCountOnClick; 

// Use this for initialization 
void Start() { 
    rigidBody = GetComponent<Rigidbody>(); 
    gameManager = GameObject.FindObjectOfType<GameManager>(); 
} 

// Update is called once per frame 
void Update() { 

    if (gameManager.recording){ 
     Record(); 
    } else { 
     Playback(); 
    } 
} 

void Playback(){ 
    rigidBody.isKinematic = true; 
    int frame = Time.frameCount % bufferFrames; 

    if (!bufferArrayFull && !setBufferCount){ 
     bufferCountOnClick = frame; 
     setBufferCount = true; 
    } else if (!bufferArrayFull && setBufferCount){ 
     int newFrame = frame - bufferCountOnClick; 
     frame = newFrame; 
    } 
    Debug.Log("Playback frame " + frame + " current frameCOunt is " + Time.frameCount); 
    transform.position = keyFrames[frame].position; 
    transform.rotation = keyFrames[frame].rotation; 
} 

void Record() 
{ 
    rigidBody.isKinematic = false; 
    float time = Time.time; 
    int frame = Time.frameCount % bufferFrames; 

    if (frame == bufferFrames-1 && !bufferArrayFull){ 
     bufferArrayFull = true; 
     print ("Buffer is full"); 
    } 

    Debug.Log("Recording frame " + frame + " current frameCOunt is " + Time.frameCount); 
    keyFrames [frame] = new MyKeyFrame (time, transform.position, transform.rotation); 
} 

}

공공 구조체 MyKeyFrame {

public float frameTime; 
public Vector3 position; 
public Quaternion rotation; 

public MyKeyFrame (float aTime, Vector3 aPosition, Quaternion aRotation){ 
    frameTime = aTime; 
    position = aPosition; 
    rotation = aRotation; 
} 

}

MonoBehaviour