Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Editor Scripting

Discussion in 'Scripting' started by Nims, Aug 25, 2014.

  1. Nims

    Nims

    Joined:
    Nov 11, 2013
    Posts:
    86
    I am trying to write a menu which will give the option draw the lines around the edge of the main camera view border/bounds.
    I have managed to get the lines drawn, I don't know how to erase them, and I am also having problems with actually having them drawn immediately (and not only when scene window is focused).
    Help would be appreciated on how to do this.
    This is the code so far:

    Code (csharp):
    1. public class CameraGuids : Editor
    2. {
    3.  
    4.   private static bool guides;
    5.  
    6.   private static float _leftBorder;
    7.   private static float _rightBorder;
    8.   private static float _topBorder;
    9.   private static float _bottomBorder;
    10.  
    11.   private static Vector3 topLeft;
    12.   private static Vector3 bottomLeft;
    13.   private static Vector3 topRight;
    14.   private static Vector3 bottomRight;
    15.  
    16.  
    17.   [MenuItem("MyTools/Toggle Camera Guides")]
    18.   static void ToggleGuides()
    19.   {
    20.  
    21.   guides = !guides;
    22.   if (guides)
    23.   {
    24.   GetBorderValues();
    25.   SetCorners();
    26.   DrawCameraGuides();
    27.   }
    28.   else
    29.   {
    30.   ClearGuides();
    31.   }
    32.   Debug.Log("Guides: " + guides);
    33.   }
    34.  
    35.   private static void GetBorderValues()
    36.   {
    37.   _leftBorder = Camera.main.ScreenToWorldPoint(new Vector3(0f, 0f, 0f)).x;
    38.   _rightBorder = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, 0f, 0f)).x;
    39.   _topBorder = Camera.main.ScreenToWorldPoint(new Vector3(0f, Screen.height, 0f)).y;
    40.   _bottomBorder = Camera.main.ScreenToWorldPoint(new Vector3(0f, 0f, 0f)).y;
    41.   }
    42.  
    43.   private static void SetCorners()
    44.   {
    45.   topLeft = new Vector3(_leftBorder, _topBorder, 0);
    46.   bottomLeft = new Vector3(_leftBorder, _bottomBorder, 0);
    47.   topRight = new Vector3(_rightBorder, _topBorder, 0);
    48.   bottomRight = new Vector3(_rightBorder, _bottomBorder, 0);
    49.   }
    50.  
    51.   private static void DrawCameraGuides()
    52.   {
    53.  
    54.   Debug.DrawLine(topLeft, bottomLeft, Color.red);
    55.   Debug.DrawLine(bottomLeft, bottomRight, Color.red);
    56.   Debug.DrawLine(bottomRight, topRight, Color.red);
    57.   Debug.DrawLine(topRight, topLeft, Color.red);
    58.   }
    59.  
    60.   private static void ClearGuides()
    61.   {
    62.   // How to clear guides?
    63.   }
    64.  
    65. }
    Edit:
    Another option is using Handles and hooking into the SceneView.onSceneGUIDelegate, but I still don't see how I can "Erase" the lines I have drawn. But it does solve the problem of the lines disappearing when selecting another game object.

    Another question regarding when OnEnable() is called for this script (As it is dervied from scriptable object)? I am not getting any calls to OnEnable() - is this because I am not creating a window?
     
    Last edited: Aug 25, 2014