- Home /
Filling list with editor scripts is empty on Play
Hi everybody. Apologies for yet another question about Editor Scripts on this page.
I'm filling a List through an editor script, but it resets on play mode. I read about SerializeField and I guess that I missed something there.
Here are the relevant Scripts:
AssetAudioPair:
using UnityEngine;
using System.Collections;
[System.Serializable]
public class AssetAudioPair {
public Sprite sprite;
public AudioClip audioFeedback;
public string textContent;
public AssetAudioPair(Sprite s, AudioClip a){
this.sprite = s;
this.audioFeedback = a;
}
public AssetAudioPair(){
}
}
GameManager:
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using System.Linq;
public class GameManager : MonoBehaviour
{
[SerializeField]
public List<AssetAudioPair> imagePairs;
[SerializeField]
public List<AssetAudioPair> textPairs;
// Lots of other stuff, but these are the relevant fields
}
GameManagerEditor:
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine.UI;
[CustomEditor(typeof(GameManager))]
public class GameManagerEditor : Editor {
List<AudioClip> feedback;
public override void OnInspectorGUI () {
//Called whenever the inspector is drawn for this object.
DrawDefaultInspector();
if (GUILayout.Button ("Fill Images")) {
feedback = new List<AudioClip>();
var audioPaths = from a in AssetDatabase.GetAllAssetPaths()
where a.Contains("/Audio/new/")
where a.Contains("03_WRONG")
select a;
foreach(string path in audioPaths){
feedback.Add(AssetDatabase.LoadAssetAtPath<AudioClip>(path));
}
var imagePaths = from s in AssetDatabase.GetAllAssetPaths ()
where s.Contains ("/Textures/GameUI/Images/")
select s;
foreach (string path in imagePaths) {
AssetAudioPair x = new AssetAudioPair ((AssetDatabase.LoadAssetAtPath<Sprite> (path)), null);
if (!manager.imagePairs.Any(p => p.sprite == x.sprite)) {
manager.imagePairs.Add (x);
}
}
foreach(AssetAudioPair aP in manager.imagePairs){
aP.audioFeedback = feedback[Random.Range(0,3)];
}
Debug.Log (manager.imagePairs.Count);
}
}
}
Am I serializing the wrong fields? Am I just doing it wrong? What am I missing?
Thanks!
Answer by YinXiaozhou · Sep 25, 2016 at 02:29 AM
The posted code does not show how you get the manager variable in your OnInspectorGUI. Anyway, reason should be you didn't mark the dirty state of your target object. Put code below into your if(button) block,
EditorUtility.SetDirty(target)
Right, but keep in $$anonymous$$d that SetDirty doesn't work properly on scene objects. It doesn't mark the scene as dirty. In most cases it's easier to use Undo.RecordObject. RecordObject has to be called before you do any changes to your object. Unity automatically checks all recorded objects at the end of the OnInspectorGUI to see if the object has changed and if it did it will create an undo step for it and save the changes.
Oh yes. Undo.RecordObject is definitely better. Forgive me as a cave programmer co$$anonymous$$g from the age that don't have the method in Unity:) Plz, write a new answer, so they can mark it as the answer.
manager is just a reference to target casted to my Game$$anonymous$$anager class.
Surprisingly though, SetDirty works, as it keeps my objects alive with the correct contents on play.
Undo.RecordObject keeps the objects alive, but the contents (Sprite and AudioClip) get reset.
First I had some errors because some of the objects were already serialised in the prefab, but i solved that issue.
Any idea why EditorUtility.SetDirty(manager);
works and Undo.RecordObject(target "added items");
doesnt keep the contents alive?
I suppose I'm using it wrong, can you point me in the right direction?
Your answer
Follow this Question
Related Questions
Editor script variables resetting after restarting Unity 2 Answers
Custom Editor window loses values on play. 1 Answer
SerializedProperty and PropertyField NullReference 1 Answer
Variable data removed on play. 1 Answer
Can you force editor scripts to serialize their data,How to serialize fields in an editor script 0 Answers