Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Change Near Clipping Plane Scene Camera

Discussion in 'Extensions & OnGUI' started by Johannski, Feb 12, 2017.

  1. Johannski

    Johannski

    Joined:
    Jan 25, 2014
    Posts:
    519
    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:
    183
    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:
    519
    RaoulWB, Alverik and jeromeWork like this.
  4. jeromeWork

    jeromeWork

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

    Johannski

    Joined:
    Jan 25, 2014
    Posts:
    519
    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:
    183
    oooh... Mouse wheel click to focus on click point is new to me! thanks again :D