Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

ObjectField in a ListView is not working.

Discussion in 'UI Toolkit' started by watsonsong, Sep 29, 2021.

  1. watsonsong

    watsonsong

    Joined:
    May 13, 2015
    Posts:
    555
    I test the following code:
    Code (CSharp):
    1.  
    2. using UnityEditor;
    3. using UnityEditor.UIElements;
    4. using UnityEngine;
    5. using UnityEngine.UIElements;
    6.  
    7. public class ObjectFieldTest : EditorWindow
    8. {
    9.     [MenuItem("Test/ObjectFieldTest", false, 10000)]
    10.     private static void OpenWindow()
    11.     {
    12.         var window = GetWindowWithRect<ObjectFieldTest>(
    13.             new Rect(0, 0, 500, 400));
    14.         window.titleContent = new GUIContent("ObjectFieldTest");
    15.         window.Show();
    16.     }
    17.  
    18.     private void OnEnable()
    19.     {
    20.         var root = this.rootVisualElement;
    21.  
    22.         var listView = new ListView(new int[1])
    23.         {
    24.             reorderMode = ListViewReorderMode.Animated,
    25.             makeItem = () =>
    26.             {
    27.                 return new ObjectField("Source")
    28.                 {
    29.                     allowSceneObjects = false,
    30.                     objectType = typeof(Object),
    31.                 };
    32.             },
    33.             bindItem = (root, index) => { },
    34.             fixedItemHeight = EditorGUIUtility.singleLineHeight,
    35.         };
    36.  
    37.         root.Add(listView);
    38.     }
    39. }
    40.  
    And when I open the window and drop any object to the ObjectField, the unity report error:
    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. UnityEngine.UIElements.ListViewDragger.MakeDragAndDropArgs (UnityEngine.UIElements.ListViewDragger+DragPosition dragPosition) (at <846d2b3d2b8c4a779aa00bc573b6e54d>:0)
    3. UnityEngine.UIElements.ListViewDragger.OnDrop (UnityEngine.Vector3 pointerPosition) (at <846d2b3d2b8c4a779aa00bc573b6e54d>:0)
    4. UnityEngine.UIElements.ListViewDraggerAnimated.OnDrop (UnityEngine.Vector3 pointerPosition) (at <846d2b3d2b8c4a779aa00bc573b6e54d>:0)
    5. UnityEngine.UIElements.DragEventsProcessor.OnDragPerformEvent (UnityEngine.UIElements.DragPerformEvent evt) (at <846d2b3d2b8c4a779aa00bc573b6e54d>:0)
    6. UnityEngine.UIElements.EventCallbackFunctor`1[TEventType].Invoke (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.PropagationPhase propagationPhase) (at <846d2b3d2b8c4a779aa00bc573b6e54d>:0)
    7. UnityEngine.UIElements.EventCallbackRegistry.InvokeCallbacks (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.PropagationPhase propagationPhase) (at <846d2b3d2b8c4a779aa00bc573b6e54d>:0)
    8. UnityEngine.UIElements.CallbackEventHandler.HandleEvent (UnityEngine.UIElements.EventBase evt) (at <846d2b3d2b8c4a779aa00bc573b6e54d>:0)
    9. UnityEngine.UIElements.EventDispatchUtilities.PropagateEvent (UnityEngine.UIElements.EventBase evt) (at <846d2b3d2b8c4a779aa00bc573b6e54d>:0)
    10. UnityEngine.UIElements.MouseEventDispatchingStrategy.SendEventToRegularTarget (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.BaseVisualElementPanel panel) (at <846d2b3d2b8c4a779aa00bc573b6e54d>:0)
    11. UnityEngine.UIElements.MouseEventDispatchingStrategy.SendEventToTarget (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.BaseVisualElementPanel panel) (at <846d2b3d2b8c4a779aa00bc573b6e54d>:0)
    12. UnityEngine.UIElements.MouseEventDispatchingStrategy.DispatchEvent (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel iPanel) (at <846d2b3d2b8c4a779aa00bc573b6e54d>:0)
    13. UnityEngine.UIElements.EventDispatcher.ApplyDispatchingStrategies (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel, System.Boolean imguiEventIsInitiallyUsed) (at <846d2b3d2b8c4a779aa00bc573b6e54d>:0)
    14. UnityEngine.UIElements.EventDispatcher.ProcessEvent (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel) (at <846d2b3d2b8c4a779aa00bc573b6e54d>:0)
    15. UnityEngine.UIElements.EventDispatcher.Dispatch (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel, UnityEngine.UIElements.DispatchMode dispatchMode) (at <846d2b3d2b8c4a779aa00bc573b6e54d>:0)
    16. UnityEngine.UIElements.BaseVisualElementPanel.SendEvent (UnityEngine.UIElements.EventBase e, UnityEngine.UIElements.DispatchMode dispatchMode) (at <846d2b3d2b8c4a779aa00bc573b6e54d>:0)
    17. UnityEngine.UIElements.UIElementsUtility.DoDispatch (UnityEngine.UIElements.BaseVisualElementPanel panel) (at <846d2b3d2b8c4a779aa00bc573b6e54d>:0)
    18. UnityEngine.UIElements.UIElementsUtility.UnityEngine.UIElements.IUIElementsUtility.ProcessEvent (System.Int32 instanceID, System.IntPtr nativeEventPtr, System.Boolean& eventHandled) (at <846d2b3d2b8c4a779aa00bc573b6e54d>:0)
    19. UnityEngine.UIElements.UIEventRegistration.ProcessEvent (System.Int32 instanceID, System.IntPtr nativeEventPtr) (at <846d2b3d2b8c4a779aa00bc573b6e54d>:0)
    20. UnityEngine.UIElements.UIEventRegistration+<>c.<.cctor>b__1_2 (System.Int32 i, System.IntPtr ptr) (at <846d2b3d2b8c4a779aa00bc573b6e54d>:0)
    21. UnityEngine.GUIUtility.ProcessEvent (System.Int32 instanceID, System.IntPtr nativeEventPtr, System.Boolean& result) (at <ae7f5af45fd74d6f8ef88ea586136af6>:0)
    I submit a bug(Case 1369139), and attach the test project.
     

    Attached Files:

  2. MoonbladeStudios

    MoonbladeStudios

    Joined:
    Oct 14, 2013
    Posts:
    185
    I have the same issue.
    In the UI Toolkit Event Debugger it shows that "DragPerformEvent" has the Target : "<null>" that looks weird, but I don't know if it has something to do with the problem.

    Anyway it has something to do with list reorder mode : "Animated". If you switch to "Simple" reorder mode, the problem disappears. So looks like a Unity bug.

    Anyway, if you find anything else please share :)
     
    bai_ming and Nexer8 like this.
  3. jonathanma_unity

    jonathanma_unity

    Unity Technologies

    Joined:
    Jan 7, 2019
    Posts:
    229
    Thanks for the bug report we will look at it.
     
    bai_ming and MoonbladeStudios like this.
  4. watsonsong

    watsonsong

    Joined:
    May 13, 2015
    Posts:
    555
    Is there any progress about this bug? It still happens in 2021.2.12f1.
     
    Nexer8 likes this.
  5. jonathanma_unity

    jonathanma_unity

    Unity Technologies

    Joined:
    Jan 7, 2019
    Posts:
    229
    A fix was merged into 2021.2 a week ago.
     
    Aldeminor and Nexer8 like this.