现在网络上的方案一般都是直接使用unity的preview,但是preview一般也要写比较多的代码,文档也没多少,看来看去也就那几篇,但是如果被策划逼急了, 想要快速成型一个窗口, 可以考虑在ongui里面刷新截图的想法才实现,preview实际上也就是去对scene面板进行快照然后刷新。
代码非常简单粗暴。
public class ScreenShotPreview:EditorWindow
{
[MenuItem("Tools/截图预览窗口")]
static void Open()
{
CreateWindow<ScreenShotPreview>();
}
void OnGUI()
{
if (Event.current.type == EventType.Repaint)
Repaint(); // 保证窗口实时刷新
Camera cam = SceneView.GetAllSceneCameras()[0];
int pixWidth = cam.pixelWidth;
int pixHeight = cam.pixelHeight;
RenderTexture tex = new RenderTexture(pixWidth,pixHeight,1,RenderTextureFormat.DefaultHDR);
RenderTexture lastTex = cam.targetTexture;
cam.targetTexture = tex;
cam.Render();
cam.targetTexture = lastTex;
float width = pixWidth;
float height = pixHeight;
Rect rect = new Rect(0, 0, width, height);
GUI.DrawTexture(rect, tex);
}
}