Search Unity

(Solved)Editor script Event.current.Use() not consume click event.

Discussion in 'Scripting' started by joemane22, Mar 8, 2020.

  1. joemane22

    joemane22

    Joined:
    Dec 16, 2013
    Posts:
    4
    Ok so I posted this in answers however no answers were provided. I am stuck on this.

    I have a custom map editor and one of its functions is to paint terrain materials on blocks inside the scene view. It pretty much works except the fact that it selects the game object when I click on it which resets the state of the map editor and prevents further painting. Clicking and dragging also makes the default selection rectangle instead of painting in a line.

    I try to consume the event when I call Event.current.Use() however the editor still performs that click event when I want it to be blocked while I am painting on the map.
    Here is the code for detecting a click and painting
    Code (CSharp):
    1. private void OnSceneGUI()
    2.      {
    3.          if (paintingActive)
    4.          {
    5.              //Debug.Log(Event.current);
    6.              if ((Event.current.type == EventType.MouseDown || Event.current.type == EventType.MouseDrag) && Event.current.button == 0)
    7.              {
    8.                  Debug.LogError("Mouse down or drag");
    9.                  if (currentBrushMaterial != null)
    10.                  {
    11.                      var hits = Physics.RaycastAll(HandleUtility.GUIPointToWorldRay(Event.current.mousePosition));
    12.                      foreach (var hit in hits)
    13.                      {
    14.                          if (hit.collider.gameObject.tag == Tags.terrainBlock)
    15.                          {
    16.                              hit.collider.gameObject.GetComponent<MeshRenderer>().material = currentBrushMaterial;
    17.                              hit.collider.gameObject.GetComponent<EditorMapBlock>().data.terrainType = currentTerrainSelected;
    18.                              Event.current.Use();
    19.                              Debug.Log("Event used");
    20.                              HandleUtility.AddDefaultControl(0);
    21.                              break;
    22.                          }
    23.                      }
    24.                  }
    25.                
    26.              }
    27.          }
    28.      }
    I tried to use HandleUtility.AddDefaultControl(0) as well as I have seen that in my research however I am not exactly sure what it does and it doesn't help or hurt.

    I get the logs "Mouse down or drag" and "Event used" exactly once for each mouse click.

    In short I need to block all default mouse clicks actions I side of unity while painting and clicked inside the unity scene view.

    Help would be very much appreciate as I cant move forward with my game too much without the ability to make and test maps. I have ways around it however they are slower and more time consuming.
     
  2. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,113
    this should be
    HandleUtility.AddDefaultControl(GUIUtility.GetControlID(FocusType.Passive));


    and I think you're supposed to consume the event if the condition was satisfied
    that means instead of
    Code (csharp):
    1. void OnSceneGUI() {
    2.  if((Event.current.type == EventType.MouseDown || Event.current.type == EventType.MouseDrag) && Event.current.button == 0) {
    3.     blabla {
    4.       blablabbla {
    5.         Event.current.Use();
    6.       }
    7.     }
    8.   }
    9. }
    do
    Code (csharp):
    1. void OnSceneGUI() {
    2.  if((Event.current.type == EventType.MouseDown || Event.current.type == EventType.MouseDrag) && Event.current.button == 0) {
    3.     blabla {
    4.       blablabbla { }
    5.     }
    6.     Event.current.Use();
    7.   }
    8. }
    though I'm also using
    SceneView.onSceneGUIDelegate += UpdateSceneView

    and I do these kinds of things in my UpdateSceneView method

    I'm hardly an expert on Unity editors, but this was my take on some type of a painter.
    It worked really well.
     
    joemane22 likes this.
  3. joemane22

    joemane22

    Joined:
    Dec 16, 2013
    Posts:
    4
    Thanks for your reply, I do appreciate your time.

    Once I get home I will try that modified Default control code and try to use that event though I believe mine is doing the same thing since it is in a class inherited by Editor. I dont think where the Event.current.Use() is at is the problem because that code should is called when all the conditions are met when a cube to paint is found but I do want to expand its scope to include all so it is better where you told me to set it.

    I'll see where I am at later on!
     
  4. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,113
    I also inherit from an Editor, that's a vanilla code endorsed by Unity
    tell me where you're at
     
    inSight01 likes this.
  5. joemane22

    joemane22

    Joined:
    Dec 16, 2013
    Posts:
    4
    It works, thank you very much I appreciate it! This was driving me mad all day yesterday lol.

    To clarify the solution was to use the event SceneView.beforeSceneGui event to handle all the events I need to use before the sceneview uses it.
     
    Last edited: Mar 9, 2020
    dytrychrafal, eddiere and cjacobwade like this.
  6. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,113