Search Unity

Question GraphView: Dragging a Blackboard causes it to get stuck offscreen

Discussion in 'UI Toolkit' started by MasonWheeler, Dec 16, 2020.

  1. MasonWheeler

    MasonWheeler

    Joined:
    Apr 2, 2016
    Posts:
    219
    I'm playing around with learning GraphView, and running into some strange problems with the Blackboard component. Here's a very simple repro:

    Code (CSharp):
    1.  
    2. using UnityEditor.Experimental.GraphView;
    3. using UnityEngine.UIElements;
    4.  
    5. public class MyGraphView : GraphView
    6. {
    7.     public override Blackboard GetBlackboard()
    8.     {
    9.         var result = new Blackboard(this);
    10.         /* COMMENTED OUT BLOCK #1
    11.         var c = result.capabilities;
    12.         result.capabilities = c & ~Capabilities.Movable;
    13.         */
    14.         /* COMMENTED OUT BLOCK #2
    15.         result.RegisterCallback<MouseDownEvent>(OnMouseDown);
    16.         result.RegisterCallback<MouseUpEvent>(OnMouseUp);
    17.         result.RegisterCallback<MouseMoveEvent>(OnMouseMove);
    18.         */
    19.         return result;
    20.     }
    21.  
    22.     public override void ReleaseBlackboard(Blackboard toRelease)
    23.     {
    24.         toRelease.RemoveFromHierarchy();
    25.         /* COMMENTED OUT BLOCK #3
    26.         toRelease.UnregisterCallback<MouseDownEvent>(OnMouseDown);
    27.         toRelease.UnregisterCallback<MouseUpEvent>(OnMouseUp);
    28.         toRelease.UnregisterCallback<MouseMoveEvent>(OnMouseMove);
    29.         */
    30.     }
    31.  
    32.     private static void OnMouseDown(MouseDownEvent e)
    33.     {
    34.         e.StopImmediatePropagation();
    35.     }
    36.  
    37.     private static void OnMouseUp(MouseUpEvent e)
    38.     {
    39.         e.StopImmediatePropagation();
    40.     }
    41.  
    42.     private static void OnMouseMove(MouseMoveEvent e)
    43.     {
    44.         e.StopImmediatePropagation();
    45.     }
    46. }
    Code (CSharp):
    1.  
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using UnityEditor;
    5.  
    6. public class MyGraphEditor : GraphViewEditorWindow
    7. {
    8.     protected MyGraphView _view;
    9.  
    10.     public override IEnumerable<GraphView> graphViews {
    11.         get
    12.         {
    13.             yield return _view;
    14.         }
    15.     }
    16.  
    17.     private void OnEnable()
    18.     {
    19.         _view = new MyGraphView();
    20.         this.rootVisualElement.Add(_view);
    21.         _view.style.height = new StyleLength(new Length(100, LengthUnit.Percent));
    22.     }
    23.  
    24.    [MenuItem("Tools/Graph Editor Test")]
    25.    public static void ShowWindow()
    26.     {
    27.         var graphViewWindow = CreateInstance<MyGraphEditor>();
    28.         var graphView = graphViewWindow.graphViews.FirstOrDefault();
    29.         if (graphView != null)
    30.         {
    31.             var blackboard = graphView.GetBlackboard();
    32.             graphView.AddElement(blackboard);
    33.         }
    34.         graphViewWindow.Show();
    35.     }
    36. }
    Clicking the new menu item causes the window to open, just a blank editor window with a blank Blackboard. Clicking on the title bar of the Blackboard and attempting to drag it, though, causes it to immediately disappear. Checking in the UIElements Debugger shows that this is because it's somehow gotten its World Bound Y set to -79, so it's disappeared off the top of the window.

    A bit of checking in the reference source suggests that if the Blackboard was not Movable, it couldn't be dragged around. Not ideal, but a temporary workaround at least. But if you uncomment the code that sets this up, (commented out block #1,) it breaks the editor entirely: clicking on the Blackboard causes Unity to stop responding to mouse clicks. Attempting to minimize the damage with the other commented out blocks has no effect.

    Anyone have any idea what's going wrong here and how to fix it? What do I need to do to have a Blackboard that can be dragged around properly?
     
  2. MasonWheeler

    MasonWheeler

    Joined:
    Apr 2, 2016
    Posts:
    219
    ...anybody?

    I would debug this myself by building from the Reference Source, except the GraphView code is deeply integrated into UnityEditor.dll and can't be separated because it uses tons of internal APIs. So I'm kind of stuck here.
     
  3. patrickf

    patrickf

    Unity Technologies

    Joined:
    Oct 24, 2016
    Posts:
    57
    Hi,

    For some reason, the blackboard does not like to be added in ShowWindow(). Try this instead:

    Code (CSharp):
    1. using UnityEditor.Experimental.GraphView;
    2. using UnityEngine.UIElements;
    3. using System.Collections.Generic;
    4. using UnityEditor;
    5.  
    6. namespace MyNS
    7. {
    8.     public class MyGraphEditor : GraphViewEditorWindow
    9.     {
    10.         protected MyGraphView _view;
    11.  
    12.         public override IEnumerable<GraphView> graphViews {
    13.             get
    14.             {
    15.                 yield return _view;
    16.             }
    17.         }
    18.  
    19.         private void OnEnable()
    20.         {
    21.             _view = new MyGraphView();
    22.             _view.StretchToParentSize();
    23.             rootVisualElement.Add(_view);
    24.         }
    25.  
    26.         [MenuItem("Tools/Graph Editor Test")]
    27.         public static void ShowWindow()
    28.         {
    29.             var graphViewWindow = CreateInstance<MyGraphEditor>();
    30.             graphViewWindow.Show();
    31.         }
    32.     }
    33.  
    34.     public class MyGraphView : GraphView
    35.     {
    36.         public MyGraphView()
    37.         {
    38.             SetupZoom(ContentZoomer.DefaultMinScale, ContentZoomer.DefaultMaxScale);
    39.             var blackboard = new Blackboard(this);
    40.             Add(blackboard);
    41.         }
    42.     }
    43. }
    44.  
     
  4. MasonWheeler

    MasonWheeler

    Joined:
    Apr 2, 2016
    Posts:
    219
    When a Unity team member says "for some reason," should I interpret that as acknowledgment that this is a bug that will likely be investigated and fixed in a future version?

    Thanks, BTW. It's working now! :D
     
  5. patrickf

    patrickf

    Unity Technologies

    Joined:
    Oct 24, 2016
    Posts:
    57
    I said "for some reason" because I did not investigated more deeply why this happens. I cannot make any representation as whether this will be fixed or not. :) Remember that GraphView is experimental.