Search Unity

Question Get AssetReference object in a custom Editor Window script using DragAndDrop.objectReferences.

Discussion in 'Addressables' started by SurajShettigar, Mar 30, 2021.

  1. SurajShettigar

    SurajShettigar

    Joined:
    Jun 21, 2018
    Posts:
    8
    Hello. I have created a custom editor window script in which I am trying to implement a Drag-and-Drop interface to drag multiple addressable objects from addessable group window and create ScriptableObject assets which has a reference to these dragged objects as AssetReference.

    [Custom Editor Window with drag and drop box]

    [Addressable assets to drag and drop]

    This is my code which I have implemented for drag-and-drop:

    Code (CSharp):
    1. Rect rect = GUILayoutUtility.GetRect(100f, 100f);
    2. GUI.Box(rect, "Drag Objects");
    3. if (rect.Contains(Event.current.mousePosition))
    4. {
    5.     if (Event.current.type == EventType.DragUpdated ||
    6.         Event.current.type == EventType.DragPerform)
    7.     {
    8.         DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
    9.         if (Event.current.type == EventType.DragPerform)
    10.         {
    11.             DragAndDrop.AcceptDrag();
    12.             this.presetCount = DragAndDrop.objectReferences.Length;
    13.             foreach(var obj in DragAndDrop.objectReferences)
    14.             {
    15.                 Debug.Log("Dragged Object: " + obj);
    16.             }
    17.         }
    18.     }
    19. }
    But the DragAndDrop.objectReferences array is empty. I understand that AssetReference object does not fall under UnityEngine.Object type. But is there any way I can access AssetReference objects in my editor script when dragging it from the addressable groups window?
     
  2. SurajShettigar

    SurajShettigar

    Joined:
    Jun 21, 2018
    Posts:
    8
    I solved the problem for now. I checked the source code for AssetReferenceDrawer.cs script located under Addressables/Editor/GUI and implemented the same technique used to drag and drop addressable files from group window to AssetReference fields in inspector.

    I imported addressables package as a local package and changed AssetEntryTreeViewItem class to public. Then I changed my custom editor drag-and-drop code as following:

    Code (CSharp):
    1.  
    2. Rect newRect = GUILayoutUtility.GetRect(100f, 100f);
    3. GUI.Box(newRect, "Drag Addressable assets");
    4. if (newRect.Contains(Event.current.mousePosition))
    5. {
    6.     if (Event.current.type == EventType.DragUpdated ||
    7.         Event.current.type == EventType.DragPerform)
    8.     {
    9.         DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
    10.         if (Event.current.type == EventType.DragPerform)
    11.         {
    12.             DragAndDrop.AcceptDrag();
    13.             List<AssetEntryTreeViewItem> draggedItems = DragAndDrop.GetGenericData("AssetEntryTreeViewItem") as List<AssetEntryTreeViewItem>;
    14.             foreach (AssetEntryTreeViewItem item in draggedItems)
    15.             {
    16.                 string path = AssetDatabase.GetAssetPath(item.entry.TargetAsset);
    17.                 AssetReference reference = new AssetReference();
    18.                 reference.SetEditorAsset(AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(path));
    19.                 /// Do something with the AssetReference object
    20.             }
    21.         }
    22.     }
    23. }
    24.  
     
    tang001 likes this.
  3. DiklatMG

    DiklatMG

    Joined:
    Oct 21, 2022
    Posts:
    1
    Here's chunk of my old code, it's still working.

    Code (CSharp):
    1. SerializedObject so;
    2. SerializedProperty charCards;
    3.  
    4. void OnEnable()
    5. {
    6.     InitProperty();
    7. }
    8.  
    9. void InitProperty()
    10. {
    11.     so = new SerializedObject(Object-ThatContainsClassName);
    12.     charCards = so.FindProperty(nameof(ClassName.charCards));
    13. }
    14.  
    15. void OnGUI()
    16. {
    17.     so.Update();
    18.     EditorGUILayout.PropertyField(charCards, new GUIContent { text = "Character Cards" });
    19.     so.ApplyModifiedProperties();
    20. }