Unity之编辑器类将摄像机视图(targetTexture)绘制在窗口中
在菜单来创建按钮 “Camera Viewer”
点击 “Camera Viewer”创建并显示一个窗口,将主摄像机视野内的视图,绘制在该窗口中
上方为摄像机视野,下方为重新创建一个窗口,然后重新画摄像机视野内的东西
在Editor文件夹下创建脚本
using UnityEngine;
using System.Collections;
using UnityEditor;
public class CameraView : EditorWindow
{
Camera myCamera = Camera.main; //获取摄像机
RenderTexture renderTexture;
[MenuItem("Examples/Camera Viewer")] //在菜单栏创建按钮
static void Init()
{
EditorWindow editorWindows = GetWindow(typeof(CameraView)); //创建新窗口
editorWindows.autoRepaintOnSceneChange = true;
editorWindows.Show();
}
public void Awake() //当跳出窗口时首先调用该方法
{
renderTexture = new RenderTexture((int)position.width,
(int)position.height, 1); //获取renderTexture
}
public void Update() //跳出窗口后每帧调用该方法
{
if (myCamera != null)
{
myCamera.targetTexture = renderTexture;
}
}
void OnGUI()
{
if (renderTexture != null)
{
GUI.DrawTexture(new Rect(0.0f, 0.0f, position.width, position.height), renderTexture, ScaleMode.ScaleAndCrop, true);
}
}
}