PreviewRenderUtility的Example

用PreviewRenderUtility就不用自己再去创建摄像机来做模型在Editor下的预览了

 

需要注意,默认是用场景天空盒和场景灯光。参考ShaderForge编辑器的做法,配置自定义灯光

这些Unity都有提供接口,不用自己再去创建

using UnityEngine;
using System.Collections;
using UnityEditor;
using UnityEditorInternal;

public class Foo : EditorWindow
{
    Mesh mPreviewMesh;
    Material mPreviewMaterial;
    PreviewRenderUtility mPreviewRenderUtility;


    [MenuItem("Tools/Foo")]
    static void Setup()
    {
        GetWindow<Foo>();
    }

    void OnGUI()
    {
        if (mPreviewRenderUtility == null)
        {
            mPreviewRenderUtility = new PreviewRenderUtility();
            mPreviewRenderUtility.m_Camera.farClipPlane = 500;
            mPreviewRenderUtility.m_Camera.clearFlags = CameraClearFlags.SolidColor;
            mPreviewRenderUtility.m_Camera.transform.position = new Vector3(0, 0, -10);

            var go = GameObject.CreatePrimitive(PrimitiveType.Cube);
            var meshFilter = go.GetComponent<MeshFilter>();
            mPreviewMesh = meshFilter.sharedMesh;
            mPreviewMaterial = go.GetComponent<MeshRenderer>().sharedMaterial;

            DestroyImmediate(go);
        }

        var drawRect = new Rect(0, 0, 100, 100);
        mPreviewRenderUtility.BeginPreview(drawRect, GUIStyle.none);

        InternalEditorUtility.SetCustomLighting(mPreviewRenderUtility.m_Light, new Color(0.6f, 0.6f, 0.6f, 1f));

        mPreviewRenderUtility.DrawMesh(mPreviewMesh, Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(30, 45, 0), Vector3.one), mPreviewMaterial, 0);

        mPreviewRenderUtility.m_Camera.Render();
        var texture = mPreviewRenderUtility.EndPreview();

        InternalEditorUtility.RemoveCustomLighting();

        GUI.Box(drawRect, texture);
    }
}

 

 

效果:

 

 

天空盒的话可以自行配置一个带Cubemap的shader:

转载于:https://www.cnblogs.com/hont/p/6618528.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值