版本:Unity 6 (6000.0)
语言中文(简体)
  • C#

SceneView.AddCameraMode

提出更改建议

成功!

感谢您帮助我们提升 Unity 文档的质量。虽然我们无法接受所有提交,但我们会仔细阅读用户提出的每条更改建议,并在适用时更新文档内容。

关闭

提交失败

由于某些原因,无法提交您的更改建议。请在几分钟后重试。感谢您花时间帮助我们提升 Unity 文档的质量。

关闭

取消

声明

public static SceneView.CameraMode AddCameraMode(string name, string section);

参数

name 新模式的名称。
section 要向其中添加新模式的分区。这可以是现有分区或新分区。

返回值

CameraMode具有所提供的名称和分区的新 CameraMode。

描述

向场景视图摄像机模式列表中添加一个自定义摄像机模式。

当选定用户自定义模式时,场景视图将以“着色”模式选定状态进行渲染。

using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

// Using a ScriptableSingleton lets us initialize and cleanup any managed resources with OnEnable and OnDisable. class CustomCameraMode : ScriptableSingleton<CustomCameraMode> { // Initialize an instance of this class when the editor loads. [InitializeOnLoadMethod] static void Init() => _ = instance;

const string k_Name = "Positions as Colors"; const string k_Section = "Miscellaneous"; Shader m_ReplacementShader; Dictionary<SceneView, bool> m_Active = new Dictionary<SceneView, bool>();

void OnEnable() { // Make sure to only call AddCameraMode once. This adds the camera mode to all existing and future SceneViews. SceneView.AddCameraMode(k_Name, k_Section);

// Custom modes are implemented through replacement shaders. We set the replacement shader during repaint events // in the OnSceneGUI callback. SceneView.duringSceneGui += OnSceneGUI;

// Create a shader in your project with code below this example. m_ReplacementShader = Shader.Find("Unlit/PositionAsColor"); }

// If any managed resources are created in OnEnable, they should be cleaned up in OnDisable. void OnDisable() { SceneView.duringSceneGui -= OnSceneGUI; }

void OnSceneGUI(SceneView view) { if (Event.current.type != EventType.Repaint) return;

// When the active camera mode changes, we need to update the replacement shader. The replacement shader and // tag are serialized for each scene view, and do not need to be re-applied every frame or after domain reloads. // Alternatively, you can make use of the `SceneView.onCameraModeChanged` event to update the replacement shader. if(!m_Active.TryGetValue(view, out var wasActive)) m_Active[view] = wasActive = false;

var isActive = view.cameraMode.drawMode == DrawCameraMode.UserDefined && view.cameraMode.name == k_Name && view.cameraMode.section== k_Section;

if (wasActive == isActive) return;

m_Active[view] = isActive;

if(isActive) view.SetSceneViewShaderReplace(m_ReplacementShader, string.Empty); else view.SetSceneViewShaderReplace(null, null); } }

上述示例中用到的着色器源代码只是一个示例。

          Shader "Unlit/PositionAsColor"
{
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag

#include "UnityCG.cginc"

struct appdata { float4 vertex : POSITION; };

struct v2f { float4 vertex : SV_POSITION; float4 color : COLOR; };

v2f vert (appdata v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); o.color = float4((normalize(v.vertex.xyz)+1)*.5, 1); return o; }

fixed4 frag (v2f i) : SV_Target { return i.color; } ENDCG } } }