Hello dear friends,
i really need your help on this matter please. I am not able to solve this on my own.
I have got a reorderable list which works great. This List is a List of item. Now on every item itself i want to draw another reorderable list. It has to be a List of properties. I have already a field on the item which is a List of properties. I tried a few things but i am drawing the list not individually for each item but it is being drawn for all items, which is not what i want. I have attached a gif too, if you take a look you´ll know what i mean. Thanks.
Apr 24 12:31 PM - Codeshare or code below:
public class ItemHolder : MonoBehaviour
{
public List<Item> items = new List<Item>();
public List<Property> properties = new List<Property>(); // Here is the list but i think it does not belong here
}
---------------------------------------------------------------------------------
[CustomEditor(typeof(ItemHolder))]
public class ItemHolderCustomEditor : Editor
{
ItemHolder t;
ReorderableList reorderableList;
ReorderableList reorderableList2;
int currentTab;
public void OnEnable()
{
t = (ItemHolder)target;
//List1
reorderableList = new ReorderableList(serializedObject, serializedObject.FindProperty("items"), true, true, true, true);
reorderableList.drawHeaderCallback += DrawHeader;
reorderableList.drawElementCallback += DrawElement;
reorderableList.elementHeightCallback += ElementHeightCallback;
reorderableList.onAddCallback += AddItem;
reorderableList.onRemoveCallback += RemoveItem;
//List2
reorderableList2 = new ReorderableList(serializedObject, serializedObject.FindProperty("properties"), true, true, true, true);
reorderableList2.drawHeaderCallback += DrawHeader2;
reorderableList2.drawElementCallback += DrawElement2;
reorderableList2.elementHeightCallback += ElementHeightCallback2;
reorderableList2.onAddCallback += AddItem2;
reorderableList2.onRemoveCallback += RemoveItem2;
}
public override void OnInspectorGUI()
{
//base.OnInspectorGUI();
reorderableList.DoLayoutList();
serializedObject.ApplyModifiedProperties();
}
private void DrawHeader(Rect rect)
{
GUI.Label(rect, "Items");
}
private void DrawElement(Rect rect, int index, bool active, bool focused)
{
Item item = t.items[index];
SerializedProperty itemProperty = serializedObject.FindProperty("items").GetArrayElementAtIndex(index);
currentTab = GUI.Toolbar(new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight), currentTab, new string[] { "Main", "Interactable", "Essentials", "Properties", "Audio" });
GUILayout.Space(20);
switch (currentTab)
{
case 0:
item.id = EditorGUI.IntField(new Rect(rect.x, rect.y + 40, rect.width, EditorGUIUtility.singleLineHeight), "ID", item.id);
item.name = EditorGUI.TextField(new Rect(rect.x, rect.y + 60, rect.width, EditorGUIUtility.singleLineHeight), "Name", item.name);
item.sprite = (Sprite)EditorGUI.ObjectField(new Rect(rect.x, rect.y + 80, rect.width, EditorGUIUtility.singleLineHeight), "Sprite", item.sprite, typeof(Sprite), false);
item.categoryIndex = EditorGUI.Popup(new Rect(rect.x, rect.y + 100, rect.width, EditorGUIUtility.singleLineHeight), "Category", item.categoryIndex, t.settings.categoryNames);
EditorGUI.LabelField(new Rect(rect.x, rect.y + 120, rect.width, EditorGUIUtility.singleLineHeight), "Description");
item.description = EditorGUI.TextArea(new Rect(rect.x, rect.y + 140, rect.width, 40), item.description);
break;
case 1:
GUI.enabled = item.interactable = EditorGUI.Toggle(new Rect(rect.x, rect.y + 40, rect.width, EditorGUIUtility.singleLineHeight), new GUIContent("Interactable", "Item can be clicked and executed"), item.interactable);
SerializedProperty unityEventProperty = itemProperty.FindPropertyRelative("OnInteract");
EditorGUI.PropertyField(new Rect(rect.x, rect.y + 60, rect.width, EditorGUIUtility.singleLineHeight), unityEventProperty);
item.eventDelay = EditorGUI.FloatField(new Rect(rect.x, rect.y + 148, rect.width, EditorGUIUtility.singleLineHeight), "EventDelay", item.eventDelay);
item.hotbar = EditorGUI.Toggle(new Rect(rect.x, rect.y + 160, rect.width, EditorGUIUtility.singleLineHeight), new GUIContent("Hotbar", "Item can be added to the hotbar"), item.hotbar);
item.cooldown = EditorGUI.FloatField(new Rect(rect.x, rect.y + 180, rect.width, EditorGUIUtility.singleLineHeight), new GUIContent("Cooldown", "Cooldown time in seconds"), item.cooldown);
GUI.enabled = true;
break;
case 2:
GUI.enabled = item.stackable = EditorGUI.Toggle(new Rect(rect.x, rect.y + 40, rect.width, EditorGUIUtility.singleLineHeight), new GUIContent("Stackable", "Item will be stacked on one Slot"), item.stackable);
item.maxStackQuantity = EditorGUI.IntField(new Rect(rect.x, rect.y + 60, rect.width, EditorGUIUtility.singleLineHeight), new GUIContent("Maximum Stack", "The maximum amount that can be stacked. 0 for no Limitation"), item.maxStackQuantity);
GUI.enabled = true;
item.destructable = EditorGUI.Toggle(new Rect(rect.x, rect.y + 80, rect.width, EditorGUIUtility.singleLineHeight), new GUIContent("Destructable", "Item can be destroyed"), item.destructable);
item.durability = EditorGUI.FloatField(new Rect(rect.x, rect.y + 100, rect.width, EditorGUIUtility.singleLineHeight), "Durabillity", item.durability);
item.weight = EditorGUI.FloatField(new Rect(rect.x, rect.y + 120, rect.width, EditorGUIUtility.singleLineHeight), "Weight", item.weight);
break;
case 3:
reorderableList2.DoList(new Rect(rect.x, rect.y + 40, rect.width, 20)); // Draw List inside the tab 3
break;
case 4:
item.addItemAudioClip = (AudioClip)EditorGUI.ObjectField(new Rect(rect.x, rect.y + 40, rect.width, EditorGUIUtility.singleLineHeight), "Add", item.addItemAudioClip, typeof(AudioClip), true);
item.removeItemAudioClip = (AudioClip)EditorGUI.ObjectField(new Rect(rect.x, rect.y + 60, rect.width, EditorGUIUtility.singleLineHeight), "Remove", item.removeItemAudioClip, typeof(AudioClip), true);
item.destroyItemAudioClip = (AudioClip)EditorGUI.ObjectField(new Rect(rect.x, rect.y + 80, rect.width, EditorGUIUtility.singleLineHeight), "Destroy", item.destroyItemAudioClip, typeof(AudioClip), true);
item.interactAudioClip = (AudioClip)EditorGUI.ObjectField(new Rect(rect.x, rect.y + 100, rect.width, EditorGUIUtility.singleLineHeight), "Interact", item.interactAudioClip, typeof(AudioClip), true);
break;
}
}
private float ElementHeightCallback(int index)
{
float propertyHeight = EditorGUI.GetPropertyHeight(reorderableList.serializedProperty.GetArrayElementAtIndex(index), true);
return propertyHeight + 240;
}
private void AddItem(ReorderableList list)
{
t.items.Add(new Item());
EditorUtility.SetDirty(target);
}
private void RemoveItem(ReorderableList list)
{
t.items.RemoveAt(list.index);
EditorUtility.SetDirty(target);
}
private void OnDisable()
{
reorderableList.drawHeaderCallback -= DrawHeader;
reorderableList.drawElementCallback -= DrawElement;
reorderableList.onAddCallback -= AddItem;
reorderableList.onRemoveCallback -= RemoveItem;
reorderableList2.drawHeaderCallback -= DrawHeader2;
reorderableList2.drawElementCallback -= DrawElement2;
reorderableList2.onAddCallback -= AddItem2;
reorderableList2.onRemoveCallback -= RemoveItem2;
}
//LIST2
private void DrawHeader2(Rect rect)
{
GUI.Label(rect, "Properties");
}
private void DrawElement2(Rect rect, int index, bool isActive, bool isFocused)
{
Property item = t.properties[index];
SerializedProperty itemProperty = serializedObject.FindProperty("properties").GetArrayElementAtIndex(index);
GUI.Label(rect, "TEST");
}
private float ElementHeightCallback2(int index)
{
float propertyHeight = EditorGUI.GetPropertyHeight(reorderableList.serializedProperty.GetArrayElementAtIndex(index), true);
return propertyHeight + 50;
}
private void AddItem2(ReorderableList list)
{
t.properties.Add(new Property());
EditorUtility.SetDirty(target);
}
private void RemoveItem2(ReorderableList list)
{
t.properties.RemoveAt(list.index);
EditorUtility.SetDirty(target);
}
}
-----------------------------------------------------------------
[Serializable]
public class Item
{
public int quantity;
public int id;
public string name;
public string description;
public Sprite sprite;
public bool interactable;
public UnityEvent OnInteract;
public float eventDelay;
public float cooldown;
public float cooldownEnd;
public bool stackable;
public bool maxStack;
public int maxStackQuantity;
public bool destructable;
public bool hotbar;
public int categoryIndex;
public float durability;
public float weight;
public List<Property> propList = new List<Property>(); //Here is the List
public AudioClip addItemAudioClip;
public AudioClip removeItemAudioClip;
public AudioClip destroyItemAudioClip;
public AudioClip interactAudioClip;
}