Search Unity

Simple Editor Script

Discussion in 'Scripting' started by giraffe1, Apr 6, 2019.

  1. giraffe1

    giraffe1

    Joined:
    Nov 1, 2014
    Posts:
    179
    Does anyone know how I can execute the AddWaypoint() from line 37 in the PathTool script? I can execute the CreateNewPath() on line 14 easily. But can't figure out how to execute from inside the OnScene() method.

    My spawner script:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Spawner : MonoBehaviour
    4. {
    5.     public void CreateNewPath() { // Do stuff }
    6.  
    7.     public void AddWaypoint(Vector3 position, Quaternion rotation) { // Do more stuff }
    8. }
    My editor script to add 2 buttons to my spawner script:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. [CustomEditor(typeof(Spawner))]
    5. public class PathTool : Editor
    6. {
    7.     public override void OnInspectorGUI()
    8.     {
    9.         DrawDefaultInspector();
    10.         Spawner spawner = (Spawner)target;
    11.  
    12.         if (GUILayout.Button("Create New Path"))
    13.         {
    14.             spawner.CreateNewPath();
    15.  
    16.             SceneView.duringSceneGui -= OnScene;
    17.             SceneView.duringSceneGui += OnScene;
    18.         }
    19.  
    20.         if (GUILayout.Button("Finish Adding Waypoints"))
    21.         {
    22.             SceneView.duringSceneGui -= OnScene;
    23.         }
    24.     }
    25.  
    26.     private static void OnScene(SceneView sceneview)
    27.     {
    28.         HandleUtility.AddDefaultControl(GUIUtility.GetControlID(FocusType.Passive));
    29.         if (Event.current.type == EventType.MouseDown)
    30.         {
    31.             Vector2 mousePosition = Event.current.mousePosition;
    32.             Ray ray = HandleUtility.GUIPointToWorldRay(mousePosition);
    33.  
    34.             RaycastHit hit;
    35.             if (Physics.Raycast(ray, out hit, 10000f))
    36.             {
    37.                 // How to reach AddWaypoint() from here?
    38.             }
    39.         }
    40.     }
    41. }
     
  2. hasanbayat

    hasanbayat

    Joined:
    Oct 18, 2016
    Posts:
    542
    You can use Static variable to do that, just store the spawner in a static variable, something like this:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. [CustomEditor(typeof(Spawner))]
    4. public class PathTool : Editor
    5. {
    6.    
    7.     private static Spawner spawner;
    8.    
    9.     public override void OnInspectorGUI()
    10.     {
    11.         DrawDefaultInspector();
    12.         spawner = (Spawner)target;
    13.         if (GUILayout.Button("Create New Path"))
    14.         {
    15.             spawner.CreateNewPath();
    16.             SceneView.duringSceneGui -= OnScene;
    17.             SceneView.duringSceneGui += OnScene;
    18.         }
    19.         if (GUILayout.Button("Finish Adding Waypoints"))
    20.         {
    21.             SceneView.duringSceneGui -= OnScene;
    22.         }
    23.     }
    24.     private static void OnScene(SceneView sceneview)
    25.     {
    26.         HandleUtility.AddDefaultControl(GUIUtility.GetControlID(FocusType.Passive));
    27.         if (Event.current.type == EventType.MouseDown)
    28.         {
    29.             Vector2 mousePosition = Event.current.mousePosition;
    30.             Ray ray = HandleUtility.GUIPointToWorldRay(mousePosition);
    31.             RaycastHit hit;
    32.             if (Physics.Raycast(ray, out hit, 10000f))
    33.             {
    34.                 // How to reach AddWaypoint() from here?
    35.                 // Here is how to reach AddWaypoint()
    36.                 if (spawner != null) {
    37.                     // Here you have full access to Spawner methods and properties.
    38.                 }
    39.             }
    40.         }
    41.     }
    42.    
    43. }
    Hope this helps.
    Regards.
     
    giraffe1 likes this.
  3. giraffe1

    giraffe1

    Joined:
    Nov 1, 2014
    Posts:
    179
    Perfect thanks. It worked!