I’m writing a custom inspector script for my Vendor Items, all is going well but I can’t figure out how to hide certain fields if not needed. If I select “Unlimited” I would like to hide the “Quantity” field same for “Requirements” (Class & Level) it there are none. Thanks in advance.
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(Vendor))]
public class Vendor_Inspector : Editor
{
private SerializedProperty vendorItems;
private void OnEnable()
{
vendorItems = serializedObject.FindProperty("items");
}
public override void OnInspectorGUI()
{
Vendor vendor = (Vendor)target;
GUIStyle labelStyle = new(GUI.skin.label)
{
fontStyle = FontStyle.BoldAndItalic,
richText = true,
alignment = TextAnchor.MiddleCenter,
};
serializedObject.Update();
vendorItems.isExpanded = EditorGUILayout.Foldout(vendorItems.isExpanded, vendorItems.name);
if (vendorItems.isExpanded)
{
EditorGUI.indentLevel++;
// item count label
if (vendorItems.arraySize > 1)
{
EditorGUILayout.LabelField("<color=#baddff>Vendor has </color>" + vendorItems.arraySize + "<color=#baddff> Items For Sale</color>", labelStyle);
}
else
{
EditorGUILayout.LabelField("<color=#baddff>Vendor has </color>" + vendorItems.arraySize + "<color=#baddff> Item For Sale</color>", labelStyle);
}
// draw item fields
for (var i = 0; i < vendorItems.arraySize; i++)
{
var item = vendorItems.GetArrayElementAtIndex(i);
string name = vendor.MyItems[i].ItemForSale.ToString();
// ----->
EditorGUILayout.BeginHorizontal();
EditorGUILayout.PropertyField(item, new GUIContent($"Item: {name}"));
if (vendor.MyItems[i].Unlimited == false)
{
// hide Quantity field
}
if (vendor.MyItems[i].Requirements == true)
{
// hide ItemClass field
// hide ItemLevel field
}
Color originalColor = GUI.backgroundColor;
GUI.backgroundColor = Color.red;
if (GUILayout.Button("Remove", GUILayout.Width(60)))
{
vendorItems.DeleteArrayElementAtIndex(i);
}
GUI.backgroundColor = originalColor;
EditorGUILayout.EndHorizontal();
// <-----
}
GUILayout.Space(20);
// button for adding more items
if (GUILayout.Button("Add New Vendor Item"))
{
vendorItems.InsertArrayElementAtIndex(vendorItems.arraySize);
}
EditorGUI.indentLevel--;
// apply changes to the serializedProperty
if (GUI.changed)
{
EditorUtility.SetDirty(target);
}
}
serializedObject.ApplyModifiedProperties();
}
}