Skip to content

Instantly share code, notes, and snippets.

@TsubameUnity
Created February 12, 2020 07:29
Show Gist options
  • Save TsubameUnity/da4d8e02f0e364b83fd95b163fe5643b to your computer and use it in GitHub Desktop.
Save TsubameUnity/da4d8e02f0e364b83fd95b163fe5643b to your computer and use it in GitHub Desktop.
UnityEditor上で、Camera.Renderを使用するパターンでの、マルチディスプレイ対応スクリプト
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEngine;
public class EditorMultiDisplayDrawer
{
#if UNITY_EDITOR
Type type;
FieldInfo targetTextureField;
FieldInfo targetDisplayField;
List<UnityEditor.EditorWindow> gameWindows;
public EditorMultiDisplayDrawer()
{
gameWindows = new List<UnityEditor.EditorWindow>();
// 表示中のウィンドウ一覧を取得するので、GameViewのウィンドウを抽出する
gameWindows.AddRange(Resources.FindObjectsOfTypeAll<UnityEditor.EditorWindow>()
.Where(x => x.titleContent.text == "Game"));
type = type ?? typeof(UnityEditor.EditorWindow).Assembly.GetType("UnityEditor.GameView");
// TargetDisplayの番号と、画面表示用のRenderTextureを取得
#if UNITY_2019_3_OR_NEWER
targetTextureField = type.GetField("m_RenderTexture", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
#else
targetTextureField = type.GetField("m_TargetTexture", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
#endif
targetDisplayField = type.GetField("m_TargetDisplay", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
}
public void Draw(Camera camera, int targetDisplay)
{
// 同一TargetDisplayのウィンドウを探す
var gameWindow = gameWindows.FirstOrDefault(x => targetDisplay == (int)targetDisplayField.GetValue(x));
if (!gameWindow)
return;
// GameView描画用のRenderTextureを取得
var gameviewRenderTexture = (RenderTexture)targetTextureField.GetValue(gameWindow);
if (!gameviewRenderTexture)
return;
// 画面左下が基準の場合はひっくり返す
if (SystemInfo.graphicsUVStartsAtTop)
{
camera.projectionMatrix = camera.projectionMatrix * Matrix4x4.Scale(new Vector3(1, -1, 1));
}
camera.targetTexture = gameviewRenderTexture;
camera.Render();
camera.ResetProjectionMatrix();
}
#else
public void Draw(Camera camera, int targetDisplay){}
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment