Hi there,
I’m making a custom editor for my class and I’m struggling with how to use Serialized Properties correctly with System.Serializable classes? Basically I can’t assign the object reference to the classes I create in my UpdatePrizesForLevel function without declaring a class that derived from Object. Is there a way around this? Thanks
[System.Serializable]
public class PrizeEntriesForLevel
{
public int levelNumber;
[SerializeField]
PrizeEntry[] prizeEntries;
public PrizeEntriesForLevel(int levelNumber, PrizeEntry[] newPrizeEntries)
{
this.levelNumber = levelNumber;
prizeEntries = newPrizeEntries;
}
}
[System.Serializable]
public class PrizeEntry
{
public int starsRequired = 0;
public string prizeName;
public PrizeEntry(int newStars, string prize)
{
starsRequired = newStars;
prizeName = prize;
}
}
using UnityEngine;
using System.Collections;
using UnityEditor;
using Google.GData.Spreadsheets;
using System.Collections.Generic;
[CustomEditor(typeof(PrizeManager), true)]
public class PrizeManagerEditor : Editor {
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
if (GUILayout.Button("Update"))
{
UpdatePrizesForLevel(serializedObject.FindProperty("spreadsheetID").stringValue);
}
}
void UpdatePrizesForLevel(string spreadsheetID)
{
ListFeed test = LoadGoogleSpreadsheet.GetSpreadsheet(spreadsheetID);
List<PrizeEntriesForLevel> prizeEntriesForAllLevels = new List<PrizeEntriesForLevel>();
List<PrizeEntry> prizeEntriesForIndividualLevel = new List<PrizeEntry>();
foreach (ListEntry row in test.Entries)
{
if (!string.IsNullOrEmpty( row.Title.Text))
{
int levelNumber = int.Parse(row.Title.Text);
for (int j = 1; j < row.Elements.Count; j++)
{
if(!string.IsNullOrEmpty(row.Elements[j].Value))
{
PrizeEntry newPrizeEntry = new PrizeEntry(j, row.Elements[j].Value);
prizeEntriesForIndividualLevel.Add(newPrizeEntry);
}
}
PrizeEntriesForLevel newEntryForLevel = new PrizeEntriesForLevel(levelNumber, prizeEntriesForIndividualLevel.ToArray());
prizeEntriesForAllLevels.Add(newEntryForLevel);
}
}
SerializedProperty property = serializedObject.FindProperty("prizeEntries");
property.ClearArray();
property.arraySize = prizeEntriesForAllLevels.Count;
for (int i = 0; i < prizeEntriesForAllLevels.Count; i++)
{
property.InsertArrayElementAtIndex(i);
SerializedProperty prop = property.GetArrayElementAtIndex(i);
// I'd like to assign the prop value here but can't!
}
serializedObject.ApplyModifiedProperties();
}
}