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.

Question TreeView in custom editor drag and drop to Inspector

Discussion in 'Immediate Mode GUI (IMGUI)' started by Monetai, Jun 2, 2020.

  1. Monetai

    Monetai

    Joined:
    Sep 18, 2015
    Posts:
    4
    Hello!

    I'm working on an editor that list all assets from a certain type in my project.
    It look like that:

    Screenshot_1.png

    I would like to be able to drag the asset in the left tree view to a corresponding variable exposed in the inspector, but I can't find a way to do that.. The thing is I don't really know if I'm using the tree view correctly.. when dragging in the tree view, should I be dragging rows? dragging a reference to the asset itself? What should I put in the DragAndDrop.SetGenericData?
    It make a perfect transition to a second question, do you know a good ressource on tree view usage and best practice? I couldn't find any ='(


    Thank you for your help!!
     
  2. DeepShade

    DeepShade

    Joined:
    Apr 10, 2018
    Posts:
    8
    Hi,
    I hope it is not to late, but have you seen the TreeView Example Project? For your specific case you can look for the TreeViewWithTreeModel class:
    Code (CSharp):
    1.         protected override void SetupDragAndDrop(SetupDragAndDropArgs args)
    2.         {
    3.             if (hasSearch)
    4.                 return;
    5.  
    6.             DragAndDrop.PrepareStartDrag();
    7.             var draggedRows = GetRows().Where(item => args.draggedItemIDs.Contains(item.id)).ToList();
    8.             DragAndDrop.SetGenericData(k_GenericDragID, draggedRows);
    9.             DragAndDrop.objectReferences = new UnityEngine.Object[] { }; // this IS required for dragging to work
    10.             string title = draggedRows.Count == 1 ? draggedRows[0].displayName : "< Multiple >";
    11.             DragAndDrop.StartDrag (title);
    12.         }
     
  3. avi8111

    avi8111

    Joined:
    Sep 2, 2019
    Posts:
    2
    geate. the answer above is what I need. well done
     
  4. Nexer8

    Nexer8

    Joined:
    Dec 10, 2017
    Posts:
    271
    What did you use for k_GenericDragID? Anything special that needs to go there? Asking because the link no longer works.
     
  5. masterton

    masterton

    Joined:
    Dec 11, 2012
    Posts:
    37
    k_GenericDragId is a string, and you can name it anything. For example, the tree control in the Timeline package has it set as:

    Code (CSharp):
    1. const string k_GenericDragId = "TimelineDragging";