Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Bug Tools.current not working as expected / different in OnSceneGUI() and OnInspectorGUI()

Discussion in 'Editor & General Support' started by blibsproj, May 20, 2021.

  1. blibsproj

    blibsproj

    Joined:
    Jan 23, 2021
    Posts:
    4
    I have a different behaviour of a function that stores the actual Tool, display a custom Tool and revert back to the stored Tool wether this function is called in OnSceneGUI() or OnInspectorGUI(). Unity version is 2019.4.16f1

    Code (CSharp):
    1. public class MyEditor_Editor : Editor
    2. {
    3.     private Tool LastTool;
    4.     private bool ToolActive;
    5.  
    6.     public override void OnInspectorGUI()
    7.     {
    8.         if (GUILayout.Button(...) SwitchTool();
    9.     }
    10.  
    11.     protected virtual void OnSceneGUI()
    12.     {
    13.          if (Event.current.keyCode == KeyCode.Q) SwitchTool();
    14.  
    15.          if (ToolActive)
    16.          {
    17.               Tools.current = Tool.None;
    18.            
    19.               // do own Tool magic
    20.          }
    21.     }
    22.  
    23.     void SwitchTool()
    24.     {
    25.         ToolActive != ToolActive;
    26.         if(ToolActive)
    27.         {
    28.             LastTool = Tools.current;
    29.             Tools.current = Tool.None;
    30.         }
    31.         else
    32.         {
    33.             Tools.current = LastTool;
    34.         }
    35.     }
    36. }
    If I click the button my own tool handle is displayed and if I click the button again the displayed own tool is reverted to LastTool, the former selected tool e.g. the transform tool.
    If I press "Q" my own tool handle is displayed and if I press "Q" again the displayed own tool vanishes but not the LastTool is displayed but always the view tool.

    If I activate the own Tool by Button and deactivate it by key the problem occurs too.
    Debugging has shown, that when Tools.current is set back to previous Tool, LastTool contains the previous correctly.

    What do I miss here? Anything to consider?
     
    Last edited: May 20, 2021