Question by
Reizo · Oct 13, 2017 at 09:17 PM ·
editor-scriptingassetsscriptableobjectcustom editor
ScriptableObjects inside another ScriptableObject not saving changes in Custom Editor Window
Up until, 5.5, this was working fine, but now (2017.1) the changes I make to the cards (which are ScriptableObjects stored in a list inside a Deck, another ScriptableObject) are lost on play/restart. What am I missing?
Basically, I load the Deck asset, from which I get a reference to the list of Cards, which I modify and then use EditorUtility.SetDirty to save the same deck asset. The problem is that all modifications to the Cards contained in the list inside the Deck asset are lost on play/restart.
public class CardManager : EditorWindow
{
#region Variables/References
public ManagerDeck deck;
Vector2 scrollPos;
List<Card> activeList = new List<Card>();
public List<Card> popupList = new List<Card>();
#endregion
[MenuItem("Window/Card Manager %#i")]
static void Init()
{
GetWindow(typeof(CardManager));
}
void OnEnable()
{
if (EditorPrefs.HasKey("ObjectPath"))
{
string objectPath = EditorPrefs.GetString("ObjectPath");
deck = AssetDatabase.LoadAssetAtPath(objectPath, typeof(ManagerDeck)) as ManagerDeck;
}
activeList = new List<Card>(deck.CardList);
}
Rect buttonRect;
void OnGUI()
{
GUILayout.BeginHorizontal();
GUILayout.Label("Card Manager", EditorStyles.boldLabel);
if (deck != null)
{
if (GUILayout.Button("Show Deck"))
{
EditorUtility.FocusProjectWindow();
Selection.activeObject = deck;
}
}
if (GUILayout.Button("Open Deck")) { OpenDeck(); }
if (GUILayout.Button("Reset Deck")) { ResetDeck(); }
GUILayout.EndHorizontal();
if (deck == null)
{
GUILayout.BeginHorizontal();
GUILayout.Space(10);
if (GUILayout.Button("Open Existing Deck", GUILayout.ExpandWidth(false)))
{
OpenDeck();
}
GUILayout.EndHorizontal();
}
GUILayout.Space(20);
if (deck != null)
{
scrollPos = EditorGUILayout.BeginScrollView(scrollPos, false, false);
if (deck.CardList == null)
Debug.Log("wtf");
if (deck.CardList.Count > 0)
{
#region CardDisplay
foreach (Card card in activeList)
{
GUILayout.BeginHorizontal();
card.Card_Name = EditorGUILayout.TextField(card.Card_Name as string, GUILayout.Width(Screen.width / 12f));
card.Class = (CardClass)EditorGUILayout.EnumPopup(card.Class, GUILayout.Width(Screen.width / 12f));
card.Rarity = (CardRarity)EditorGUILayout.EnumPopup(card.Rarity, GUILayout.Width(Screen.width / 12f));
GUILayout.EndHorizontal();
GUILayout.Space(5);
}
#endregion
if (GUILayout.Button("Add Card"))
{
AddCard();
}
}
else
{
GUILayout.Label("This Deck is Empty.");
if (GUILayout.Button("Add Card"))
{
AddCard();
}
}
EditorGUILayout.EndScrollView();
}
if (GUI.changed)
{
EditorUtility.SetDirty(deck);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
}
void OpenDeck()
{
string absPath = EditorUtility.OpenFilePanel("Select Deck", "", "");
if (absPath.StartsWith(Application.dataPath))
{
string relPath = absPath.Substring(Application.dataPath.Length - "Assets".Length);
deck = AssetDatabase.LoadAssetAtPath(relPath, typeof(ManagerDeck)) as ManagerDeck;
if (deck.CardList == null)
deck.CardList = new List<Card>();
if (deck)
{
EditorPrefs.SetString("ObjectPath", relPath);
}
}
}
void ResetDeck()
{
activeList.Clear();
activeList = new List<Card>(deck.CardList);
if (deck.CardList.Count > 0)
{
SortRelative(CardProperty.ID, activeList);
buttonProp = CardProperty.ID;
}
}
void AddCard()
{
Card newCard = CreateCard.Create();
newCard.Card_Name = "New Card";
deck.CardList.Add(newCard);
ResetDeck();
}
void DeleteCard(int index)
{
deck.CardList.RemoveAt(index);
}
}
Comment