Search Unity

Change Near Clipping Plane Scene Camera

Discussion in 'Immediate Mode GUI (IMGUI)' started by Johannski, Feb 12, 2017.

  1. Johannski

    Johannski

    Joined:
    Jan 25, 2014
    Posts:
    826
    Hello there,

    Is it possible to change the near clipping plane of the scene camera without focusing an object. I tried to change the near clipping plane via script, but when used as a handle it just clips gizmos (See gif). I also tried to set it via an own window, but in that case the change of the near clipping plane had no effect at all.



    Here is the window script:
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4.  
    5. public class NearClippingEditorWindow : EditorWindow
    6. {
    7.   private static float nearClipping;
    8.  
    9.   [MenuItem("Window/Scene Camera clipping")]
    10.   static void Init()
    11.   {
    12.   NearClippingEditorWindow window = (NearClippingEditorWindow)EditorWindow.GetWindow(typeof(NearClippingEditorWindow));
    13.   window.Show();
    14.   }
    15.  
    16.   void OnFocus()
    17.   {
    18.   var sceneCamera = SceneView.lastActiveSceneView.camera;
    19.   if (sceneCamera == null)
    20.   {
    21.   Debug.Log("Scene Camera not found");
    22.   return;
    23.   }
    24.   nearClipping = sceneCamera.nearClipPlane;
    25.   }
    26.  
    27.   void OnGUI()
    28.   {
    29.   GUILayout.Label("Scene Camera", EditorStyles.boldLabel);
    30.   var sceneCamera = SceneView.lastActiveSceneView.camera;
    31.   if (sceneCamera == null)
    32.   {
    33.   Debug.Log("Scene Camera not found");
    34.   return;
    35.   }
    36.   nearClipping = EditorGUILayout.Slider("Near clipping", nearClipping, 0.01f, 100f);
    37.   sceneCamera.nearClipPlane = nearClipping;
    38.   SceneView.lastActiveSceneView.Repaint();
    39.   }
    40. }
    41.  
    I think I am getting the correct camera, but my changes seem to have no effect. Any ideas what went wrong? I also attached the project to the post.
     

    Attached Files:

    Last edited: Jun 8, 2018
    Alverik likes this.
  2. jeromeWork

    jeromeWork

    Joined:
    Sep 1, 2015
    Posts:
    429
    Did you find an answer to this? I hate the way the clipping plane goes all wrong after using the mouse wheel to zoom
     
  3. Johannski

    Johannski

    Joined:
    Jan 25, 2014
    Posts:
    826
  4. jeromeWork

    jeromeWork

    Joined:
    Sep 1, 2015
    Posts:
    429
    A super little tool. Works brilliantly. Thank you @Johannski ! :)
     
  5. Johannski

    Johannski

    Joined:
    Jan 25, 2014
    Posts:
    826
    Happy it helps :)
    Btw, just if you missed it, there are also some handy builtin shortcuts:
    - f to focus on the current object (Also used in the video, not always the result you might want).
    - Mouse wheel click to focus on the point the mouse clicked (kind of like the focus near button in my script, but more flexible).
     
  6. jeromeWork

    jeromeWork

    Joined:
    Sep 1, 2015
    Posts:
    429
    oooh... Mouse wheel click to focus on click point is new to me! thanks again :D
     
  7. a436t4ataf

    a436t4ataf

    Joined:
    May 19, 2013
    Posts:
    1,933
    Sadly ... doesn't work with 2019.x (seems Unity changed SceneView class in incompatible ways again :(, removing essential functionality)