Search Unity

Question Editor Window object placement in scene

Discussion in 'Scripting' started by jessee03, Nov 28, 2022.

  1. jessee03

    jessee03

    Joined:
    Apr 27, 2011
    Posts:
    729
    I want to be able to use a left mouse click in the scene view while the editor window is open to place an object in the scene. The issue I am having is I can't seem to disable selection in the scene view though. I am trying to use HandleUtility.AddDefaultControl(GUIUtility.GetControlID(FocusType.Passive)); to disable scene selection. Anyone know what I can change to disable object scene selection when I left click, but instead call PlaceObject() ?


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEditor;
    5.  
    6. public class WorldBuilderTool : EditorWindow
    7. {
    8.     GameObject objectPrefab;
    9.  
    10.     RaycastHit hit;
    11.     float yOffset;
    12.  
    13.     [MenuItem("Tools/WorldBuilder")]
    14.     public static void ShowWindow()
    15.     {
    16.         GetWindow<WorldBuilderTool>(false, "World Builder", true);
    17.  
    18.         //HandleUtility.AddDefaultControl(GUIUtility.GetControlID(FocusType.Passive));
    19.     }
    20.  
    21.     void OnGUI()
    22.     {
    23.         GUILayout.Label("Object Prefab", EditorStyles.boldLabel);
    24.         objectPrefab = (GameObject)EditorGUILayout.ObjectField(objectPrefab, typeof(GameObject), true);
    25.     }
    26.  
    27.     void PlaceObject()
    28.     {
    29.         if(objectPrefab != null)
    30.         {
    31.             Ray worldRay = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
    32.             RaycastHit hitInfo;
    33.  
    34.             if (Physics.Raycast(worldRay, out hitInfo))
    35.             {
    36.                 Debug.Log("Left-Mouse Down");
    37.                 GameObject myInstance = (GameObject)Instantiate(objectPrefab, new Vector3(0, 0, 0), objectPrefab.transform.rotation);
    38.                 myInstance.transform.position = hitInfo.point;
    39.             }
    40.         }
    41.     }
    42. }
     
  2. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,005
    You should take a look at the Tools class. For custom sceneview tools you may want to set the current tool to None. This is what the terrain editor does as well.

    Also note that there's the HandleUtility.PlaceObject method which would do all those raycasting for you. Also this uses the editor specific object picking methods. Physics.Raycast only works against colliders and they have to be synced. There's also the RaySnap method. Sceneview interactions should be done in one of the SceneView events like duringSceneGui.
     
  3. jessee03

    jessee03

    Joined:
    Apr 27, 2011
    Posts:
    729
    Is there a way to combine this with EditorWindow? The system I'm trying to make is going to allow for a bunch of different settings and the tools should only be active when the window is open
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,934
    You can subscribe and unsubscribe to scene view drawing in the EditorWindow's OnEnable/Disable: https://docs.unity3d.com/ScriptReference/SceneView-duringSceneGui.html
     
    Bunny83 likes this.