Question by
Moohead · Jan 07, 2017 at 05:38 PM ·
custom-inspector
Custom inspector popup for creating a item list
I'm trying to make a custom inspector to add items to my item list, and want to have popup menu's where I can just select attributes for the item. However when I select it on one it changes it on all of them.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using UnityEditor;
[CustomEditor(typeof(CardManager))]
internal class CardEditorInspector : Editor
{
bool showingDwarven = false;
bool showingSet1 = false;
public static string Common;
public static string Uncommon;
public static string Rare;
public string[] currentRarity = new string[] { "Common", "Uncommon", "Rare" };
public int RarityTypes = 0;
public override void OnInspectorGUI()
{
CardManager cm = (CardManager)target;
List<Dwarven> dwarven = new List<Dwarven>();
for (int i = 0; i < cm.cardList.Count; i++)
{
if (cm.cardList[i].GetType() == typeof(Dwarven))
{
dwarven.Add((Dwarven)cm.cardList[i]);
}
showingDwarven = EditorGUILayout.Foldout(showingDwarven, "Dwarven");
if (showingDwarven == true)
{
EditorGUI.indentLevel = 1;
showingSet1 = EditorGUILayout.Foldout(showingSet1, "Set 1");
if (showingSet1 == true)
{
EditorGUI.indentLevel = 1;
for (int i = 0; i < dwarven.Count; i++)
{
EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField(dwarven[i].name);
if (GUILayout.Button("-"))
cm.cardList.Remove(dwarven[i]);
EditorGUILayout.EndHorizontal();
EditorGUI.indentLevel += 1;
dwarven[i].name = EditorGUILayout.TextField("Name: ", dwarven[i].name);
RarityTypes = EditorGUILayout.Popup(RarityTypes, currentRarity);
switch (RarityTypes)
{
case 0:
dwarven[i].Rarity = Common;
break;
case 1:
dwarven[i].Rarity = Uncommon;
break;
case 2:
dwarven[i].Rarity = Rare;
break;
}
dwarven[i].ID = int.Parse(EditorGUILayout.TextField("ID: ", dwarven[i].ID.ToString()));
dwarven[i].PlayRestriction = EditorGUILayout.TextField("Play Restriction: ", dwarven[i].PlayRestriction);
dwarven[i].SubTypes = EditorGUILayout.TextField("Sub-Types: ", dwarven[i].SubTypes);
dwarven[i].Cost = int.Parse(EditorGUILayout.TextField("Cost: ", dwarven[i].Cost.ToString()));
EditorGUI.indentLevel -= 1;
EditorGUILayout.Space();
}
if (GUILayout.Button("Add New Card"))
{
Dwarven newDwarven = CreateInstance<Dwarven>();
newDwarven.name = "";
newDwarven.Rarity = "";
newDwarven.ID = 0;
newDwarven.PlayRestriction = "";
newDwarven.SubTypes = "";
newDwarven.Cost = 0;
cm.cardList.Add(newDwarven);
}
EditorGUI.indentLevel = 0;
}
}
}
}
Comment
Can you please edit your post to use the multi-line code input (the little 101010 button). It's really not possible to make sense of the example when it's just been pasted all on one line inside of back-tics.