unity场景overdraw查看脚本

转载篇在game视图下查看overdraw: https://blog.csdn.net/complicatedcc/article/details/70214681

c#代码,要挂在摄像机上:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
/// 
/// 这个脚本挂在摄像机上才能正确运行
/// 
[RequireComponent(typeof(Camera))]
public class DebugOverdrawMode : MonoBehaviour {
 
    public Shader m_OverdrawShader;
 
    private Camera m_Camera;
    private bool m_SceneFogSettings = false;
    private CameraClearFlags m_ClearFlagSetting;
    private Color m_BackGroundColor;
 
    void Awake()
    {
        m_Camera = GetComponent<Camera>();
        StoreParam();
    }
 
    //void OnLevelWasLoaded()
    //{
    //    //每次场景加载取消雾效,缓存并在OnDisable后恢复
    //    m_SceneFogSettings = RenderSettings.fog;
    //    RenderSettings.fog = false;
    //}
 
    void StoreParam()
    {
        m_SceneFogSettings = RenderSettings.fog;
        RenderSettings.fog = false;
 
        m_ClearFlagSetting = m_Camera.clearFlags;
        m_BackGroundColor = m_Camera.backgroundColor;
    }
 
    void OnEnable()
    {
        if (m_OverdrawShader == null)
        {
            m_OverdrawShader = Shader.Find("Custom/Overdraw");
            //m_OverdrawShader = UnityEditor.EditorGUIUtility.LoadRequired("SceneView/SceneViewShowOverdraw.shader") as Shader; //应用unity自带shader即可达到相同效果
        }
 
        if (m_OverdrawShader != null && m_Camera != null)
        {
            RenderSettings.fog = false;
            m_Camera.clearFlags = CameraClearFlags.Color;
            m_Camera.backgroundColor = Color.black;
            m_Camera.SetReplacementShader(m_OverdrawShader, "");
            bChanged = true;
        }
    }
 
    void OnDisable()
    {
        if (m_Camera != null)
        {
            RestoreParam();
        }
    }
 
    void RestoreParam()
    {
        RenderSettings.fog = m_SceneFogSettings;
        //m_Camera.SetReplacementShader(null, ""); //和下面效果相同
        m_Camera.ResetReplacementShader();
        m_Camera.backgroundColor = m_BackGroundColor;
        m_Camera.clearFlags = m_ClearFlagSetting;
    }
 
    //测试方法 为了方便切换  可在非运行模式下测试
    bool bChanged;
    bool bInited;
    [ContextMenu("ChangeMode")]
    public void ChangeMode()
    {
        if (bChanged)
        {
            RestoreParam();
        }
        else
        {
            if (!bInited)
            {
                m_Camera = GetComponent<Camera>();
                StoreParam();
                m_OverdrawShader = Shader.Find("Custom/Overdraw");
                bInited = true;
            }
 
            RenderSettings.fog = false;
            m_Camera.clearFlags = CameraClearFlags.Color;
            m_Camera.backgroundColor = Color.black;
            m_Camera.SetReplacementShader(m_OverdrawShader, "");
        }
        bChanged = !bChanged;
    }
}

shader代码:

Shader "Custom/Overdraw"
{
    SubShader
    {
        Tags { "RenderType" = "Transparent" "Queue" = "Transparent" }
        LOD 100
        Fog { Mode Off }
        ZWrite Off
        ZTest Always
        Blend One One
 
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
 
            #include "UnityCG.cginc"
 
            struct appdata
            {
                float4 vertex : POSITION;
            };
 
            struct v2f
            {
                float4 vertex : SV_POSITION;
            };
 
            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                return o;
            }
 
            fixed4 frag (v2f i) : SV_Target
            {
                return fixed4(0.1, 0.04, 0.02, 0);
            }
            ENDCG
        }
    }
}

效果图:
unity场景overdraw查看脚本_第1张图片

你可能感兴趣的:(unity,游戏引擎)