Search Unity

[SOLVED] OnSceneGUI <-> OnInspectorUpdate Two-Way Communication Troubles.

Discussion in 'Immediate Mode GUI (IMGUI)' started by FuguFirecracker, Dec 21, 2016.

  1. FuguFirecracker

    FuguFirecracker

    Joined:
    Sep 20, 2011
    Posts:
    419
    Hi
    Here's the issue:
    I have a FreeMoveHandle in the Scene
    I have a Vector3Field in the Inspector.

    The trick is getting one to drive the other and not override each other.
    When I update the scene handle, the inspector field updates with the values of the handle's position.
    When I change the field values, the position of the handle changes accordingly.

    I have this MOSTLY working with the exception of one terrible detail.
    I cannot enter new values in the Vector3Field.

    I can drag the field values and the handle will update along with the values.
    I can move the handle and the fields will update.

    But I cannot enter the numeric values in the Vector3Field
    If I manually enter a value and press ENTER... the value will not change.
    What am I missing here?

    And... Am I doing this correctly?
    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3.  
    4. public class TestWindow : EditorWindow
    5. {
    6.     private Vector3 _inspectorVector3;
    7.     private Vector3 _sceneVector3;
    8.     private Vector3 _interimVector3;
    9.  
    10.     [MenuItem("Tools/Test")]
    11.     public static void OpenWindow()
    12.     {
    13.         var window = GetWindow<TestWindow>();
    14.         window.titleContent = new GUIContent("Test");
    15.     }
    16.  
    17.     protected void OnEnable()
    18.     {
    19.         SceneView.onSceneGUIDelegate += OnSceneGUI;
    20.     }
    21.  
    22.     protected void OnDisable()
    23.     {
    24.  
    25.         SceneView.onSceneGUIDelegate -= OnSceneGUI;
    26.     }
    27.  
    28.  
    29.     protected void OnGUI()
    30.     {
    31.         _inspectorVector3 = EditorGUILayout.Vector3Field("Position", _inspectorVector3);
    32.  
    33.         if (GUILayout.Button("Reset"))
    34.         {
    35.             _inspectorVector3 = Vector3.zero;
    36.         }
    37.     }
    38.  
    39.     protected void OnInspectorUpdate()
    40.     {
    41.         if (SceneView.lastActiveSceneView != null)
    42.         {
    43.             SceneView.lastActiveSceneView.Repaint();
    44.         }
    45.  
    46.         _interimVector3 = _inspectorVector3;
    47.  
    48.         if (_sceneVector3 != _inspectorVector3)
    49.         {
    50.             _sceneVector3 = _interimVector3;
    51.         }
    52.  
    53.         Repaint();
    54.     }
    55.  
    56.     private void OnSceneGUI(SceneView sceneview)
    57.     {
    58.         _sceneVector3 = Handles.FreeMoveHandle(_sceneVector3, Quaternion.identity, 0.2f, Vector3.one, Handles.CubeCap);
    59.  
    60.         _interimVector3 = _sceneVector3;
    61.  
    62.         if (_inspectorVector3 != _sceneVector3)
    63.         {
    64.             _inspectorVector3 = _interimVector3;
    65.         }
    66.  
    67.     }
    68.  
    69. }
    70.  
    71.  
    72.  
     
  2. FuguFirecracker

    FuguFirecracker

    Joined:
    Sep 20, 2011
    Posts:
    419
    Sorted it out with
    Code (CSharp):
    1.  EditorGUI.BeginChangeCheck();