2017-07-10 5 views
1

これは通常は望ましいことではありませんが、私は研究プロジェクトに必要です。Unityでスクリーン出力をどのように遅らせることができますか?

シーンを開始して通常どおりに実行するにはどうすればよいですか?画面の出力を固定された秒数だけ遅延させます。私。ゲームは実行され、リアルタイムでユーザー入力に応答しますが、画面への出力はx秒遅れますか?

+2

必ずしも必要ではありません。私はレンダリングターゲット(レンダリングテクスチャ)にQueue(フレームバッファとして)を使用してCamera rendering ** only **を使用します。あなたのカメラで取り込んだテクスチャをキューに押し込み、X秒間の遅れでポップします。私は時間があれば、私はスクリプトを作成しようとします。 – Hellium

+0

"画面出力"という意味を説明するかもしれません。画面を黒くしてから黒を通常のレンダリングに置き換えたいのですか、いくつかの基本シーンを表示してから、入力を受け付けた後に何かを追加または変更したいのですか? –

+0

カメラのフレームバッファのようなものなので、シーンを開始すると2秒間は空白になり、2秒前と同じようにカメラの出力が表示されるので、プレーヤーをシーンを見るとすぐに、2秒後に結果が表示されます。 – ceteri

答えて

4

を私は、このスクリプトを作ったが、それははるかに最適化されているからです!空の/あなたのカメラに次のスクリプトを置き、&をドラッグして、カメラのゲームオブジェクトをインスペクタのrenderCameraフィールドにドロップします。

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

public class DelayedCamera : MonoBehaviour 
{ 
    [SerializeField] 
    private Camera renderCamera; 

    private RenderTexture renderTexture; 

    private Queue<Texture2D> frames; 

    private void Awake() 
    { 
     frames = new Queue<Texture2D>(); 

     // Change the depth value from 24 to 16 may improve performances. And try to specify an image format with better compression. 
     renderTexture = new RenderTexture(Screen.width, Screen.height, 24); 
     renderCamera.targetTexture = renderTexture; 
     StartCoroutine(Render()); 
    } 

    private IEnumerator Render() 
    { 
     WaitForEndOfFrame wait = new WaitForEndOfFrame(); 

     while (true) 
     { 
      yield return wait; 

      // Create new texture of the current frame 
      RenderTexture.active = renderTexture; 

      // Making this variable a class attribute may be more efficient 
      Texture2D texture = new Texture2D(renderTexture.width, renderTexture.height); 
      texture.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0); 
      texture.Apply(); 
      RenderTexture.active = null; 

      // Add it to the queue to "delay" the rendering 
      frames.Enqueue(texture); 

      // If the queue is too big, render the first frame on the screen 
      if (frames.Count > 100) 
      { 
       Graphics.Blit(frames.Dequeue(), null as RenderTexture); 
      } 
     } 
    } 
} 
+0

ありがとう、これは私が探している効果であり、大きな助けになります。パフォーマンスを向上させる方法を見つける必要があります。通常のカメラと同じか近いですが、これは大きな助けになります。 – ceteri

+0

'new Texture2D'でフレームごとにテクスチャを作成しないことで、パフォーマンスを向上させることができます。 'while'ループの外側でそれを行い、それを再利用します。 'RenderTexture'の次元が変更された場合にのみ、新しいテクスチャを作成してください。 – Programmer

+1

はい、それは私が思ったことです。私はOPに「texture」変数をクラス属性にする代わりにコメントを追加しました。 – Hellium

0

あなたはコルーチンとしてすべてのアクションを実行し、待機のx秒間

ドキュメント機能WaitForSecondsを使用することができます。https://docs.unity3d.com/ScriptReference/WaitForSeconds.html

+0

これはまったく同じことではありませんが、ネットワークに関係するものがあれば違いがあります。 –

関連する問題