0
\$\begingroup\$

I need to

  1. drag and drop GameObject to ObjectField.

  2. drag and drop a group of GameObjects to list name.

The main problem is either do 1 or 2 work. I need the function same as unity List Inspector for future devolvement. Code as follow.

static SerializedProperty ThisList;
public override void OnInspectorGUI()
{
    ThisList = serializedObject.FindProperty("G");//List name call G
    serializedObject.Update();
    Show(ThisList);
    serializedObject.ApplyModifiedProperties();
}

public static void Show(SerializedProperty list)
{
    EditorGUILayout.PropertyField(list);
    EditorGUI.indentLevel += 1;
    if (list.isExpanded)
    {         EditorGUILayout.PropertyField(list.FindPropertyRelative("Array.size"));
        ShowElements(list);
    }
    EditorGUI.indentLevel -= 1;     
}


private static GameObject[] a =new GameObject[50000];
private static void ShowElements(SerializedProperty list)
{
    //This code can drag and drop GameObject to ObjectField.
    for (int i = 0; i < list.arraySize; i++)
    {
        list.GetArrayElementAtIndex(i).objectReferenceValue = EditorGUILayout.ObjectField("OBj", list.GetArrayElementAtIndex(i).objectReferenceValue, typeof(GameObject));
    }


    //This code can drag and drop a group of GameObjects to list name.
    if (Event.current.type == EventType.DragUpdated)
    {
        DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
        Event.current.Use();
    }
    else if (Event.current.type == EventType.DragPerform)
    {          
        for (int j = 0; j < DragAndDrop.objectReferences.Length; j++)
        {
            a[j] = (DragAndDrop.objectReferences[j] as GameObject);
        }
        Event.current.Use();
    }
}

However, it not work when both codes together.

If -> EditorGUILayout.PropertyField(list, true); I can get the same result as the unity function. but I cant Custom the List. (such as add name, change position, add button...)

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

One solution is to use a object field and cast to a gameobject to drag and drop

GameObject G;//gameobject

G = (GameObject)EditorGUILayout.ObjectField("Drag GameObject here :", G, typeof(GameObject), true);

for the list you can create a normal list

editableElements = new List<GameObject>(); SerializedObject classOBJ; SerializedProperty groupofProps;

then add somewhere along the way

editableElements.Add(go);

you can edit the list however you normally do

then re-draw to window

 classOBJ = new SerializedObject(this);
 groupofProps = classOBJ.FindProperty("editableElements");
 var size = groupofProps.arraySize;
 for (int index = 0; index < size; index++)
 { 
      var property = groupofProps.GetArrayElementAtIndex(index);           
      EditorGUILayout.PropertyField(property);
 }
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .