I’ll try to make this concise as possible. I’m making a custom editor to manage my Levels for my game. I have 3 arrays I’m using: levels, subLevels, and units. Levels contain multiple subLevels and subLevels contain multiple units. Units being the possible enemy types that can spawn in that subLevel.
I’ve created buttons that I can use to easily add or subtract a level, subLevel, or Unit. When I add a new subLevel, it copies the values and inserts the new subLevel. After I added the subLevel, if I attempt to change a unit to a different type in one subLevel, it changes the respective element in ALL subLevels in that particular level. This is the main problem I’m trying to fix. IF I remove/add a unit of a subLevel, this particular subLevel won’t be affected by the aforementioned problem.
This problem seems to start on line 82. The for loop is going through each subLevel from the for loop on line 36. I don’t know how to setup my code to ONLY affect the desired index on line 108 and 109. I’ve tried to break/continue out of the for loop if GUI.changed, but to no luck.
If you need more information, don’t hesitate to ask. I’ve been stuck on the problem for a week now
[CustomEditor(typeof(LevelManager))]
public class EditorLevelManger : Editor
{
public override void OnInspectorGUI()
{
//base.OnInspectorGUI();
Undo.RecordObject(this, name);
LevelManager script = (LevelManager)target;
EditorGUILayout.LabelField("Total Levels: " + script.level.Length);
EditorGUILayout.Space();
//Loop Through Level Array
for (int i = 0; i < script.level.Length; i++)
{
EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField("Level " + i, EditorStyles.boldLabel, GUILayout.MaxWidth(60));
//Remove index from array
if (GUILayout.Button("-", GUILayout.MaxWidth(25), GUILayout.MaxHeight(14)) && script.level.Length > 1)
{
ArrayUtility.RemoveAt(ref script.level, i);
break;
}
//If last index in array, Add element instead of Insert
if (GUILayout.Button("+", GUILayout.MaxWidth(25), GUILayout.MaxHeight(14)))
{
if (i == script.level[i].subLevel.Length - 1)
ArrayUtility.Add(ref script.level, script.level[i]);
else
ArrayUtility.Insert(ref script.level, i, script.level[i]);
}
EditorGUILayout.EndHorizontal();
//Loop Through subLevel Array
for (int u = 0; u < script.level[i].subLevel.Length; u++)
{
EditorGUI.indentLevel = 1;
EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField("Sub " + u, EditorStyles.miniBoldLabel, GUILayout.MaxWidth(60));
// Setting Up Distance per level using sub-levels
int[] test = new int[script.level.Length];
for (int x = 0; x < script.level.Length; x++)
{
for (int y = 0; y < script.level[x].subLevel.Length; y++)
{
if (x == i && y < u)
test[i] += script.level[x].subLevel[y].distance;
}
}
EditorGUILayout.LabelField(test[i].ToString() + "m", GUILayout.MaxWidth(60));
//Remove index from array
if (GUILayout.Button("-", GUILayout.MaxWidth(25), GUILayout.MaxHeight(14)) && script.level[i].subLevel.Length > 1)
{
ArrayUtility.RemoveAt(ref script.level[i].subLevel, u);
break;
}
//If last index in array, Add element instead of Insert
if (GUILayout.Button("+", GUILayout.MaxWidth(25), GUILayout.MaxHeight(14)))
{
if (i == script.level[i].subLevel.Length - 1)
ArrayUtility.Add(ref script.level[i].subLevel, script.level[i].subLevel[u]);
else
ArrayUtility.Insert(ref script.level[i].subLevel, u, script.level[i].subLevel[u]);
}
EditorGUILayout.EndHorizontal();
EditorGUI.indentLevel = 1;
EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField("Distance", GUILayout.MaxWidth(90));
script.level[i].subLevel[u].distance = EditorGUILayout.IntField(script.level[i].subLevel[u].distance, GUILayout.MaxWidth(80));
EditorGUILayout.EndHorizontal();
//Loop Through Units Array
for (int j = 0; j < script.level[i].subLevel[u].units.Length; j++)
{
EditorGUILayout.BeginHorizontal();
GUI.enabled = false;
EditorGUI.indentLevel = 0;
EditorGUILayout.LabelField(j.ToString("F0"), GUILayout.MaxWidth(15));
GUI.enabled = true;
//Remove index from array
if (GUILayout.Button("-", GUILayout.MaxWidth(25), GUILayout.MaxHeight(14)) && script.level[i].subLevel[u].units.Length > 1)
{
ArrayUtility.RemoveAt(ref script.level[i].subLevel[u].units, j);
break;
}
//If last index in array, Add element instead of Insert
if (GUILayout.Button("+", GUILayout.MaxWidth(25), GUILayout.MaxHeight(14)))
{
if (j == script.level[i].subLevel[u].units.Length - 1)
ArrayUtility.Add(ref script.level[i].subLevel[u].units, script.level[i].subLevel[u].units[j]);
else
{
ArrayUtility.Insert(ref script.level[i].subLevel[u].units, j++, script.level[i].subLevel[u].units[j]);
script.level[i].subLevel[u].units[j].unitType = Unit.Type.__;
}
}
script.level[i].subLevel[u].units[j].unitType = (Unit.Type)EditorGUILayout.EnumPopup(script.level[i].subLevel[u].units[j].unitType, GUILayout.MaxWidth(100));
script.level[i].subLevel[u].units[j].spawnPercent = EditorGUILayout.FloatField(script.level[i].subLevel[u].units[j].spawnPercent, GUILayout.MaxWidth(30));
EditorGUILayout.LabelField("%", GUILayout.MaxWidth(50));
EditorGUILayout.EndHorizontal();
}
}
EditorGUILayout.Space();
EditorGUILayout.Space();
EditorGUI.indentLevel = 0;
}
if (GUI.changed)
{
serializedObject.ApplyModifiedProperties();
EditorUtility.SetDirty(script);
EditorSceneManager.MarkSceneDirty(SceneManager.GetActiveScene());
}
}
}