Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. We're looking for your feedback on the platforms you use and how you use them. Let us know!
    Dismiss Notice
  4. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  6. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  7. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  8. 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:
    499
    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:
    159
    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:
    499
    RaoulWB, Alverik and jeromeWork like this.
  4. jeromeWork

    jeromeWork

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

    Johannski

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