Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.
CloseFor some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.
Closename | The name for the new mode. |
section | The section in which the new mode will be added. This can be an existing or new section. |
CameraMode A CameraMode with the provided name and section.
Add a custom camera mode to the Scene view camera mode list.
When a user-defined mode is selected, the Scene view will render as though the "shaded" mode is selected.
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); } }
The source code for the shader used in the sample above is an example.
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 } } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.