unity编辑器预览窗口怎么做

现在网络上的方案一般都是直接使用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);

        }
    }

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JustEasyCode

谢谢您

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值