Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

EditorWindow saving list for while unity is closed

Discussion in 'Scripting' started by BeautifulFish, Oct 19, 2014.

  1. BeautifulFish

    BeautifulFish

    Joined:
    Sep 25, 2014
    Posts:
    55
    Hi,

    I've been working on a little 'audio mixer' editor window. (So I can control multiple audio sources in one place. I've recently managed to figure out the serialisation, so the settings can be adjusted in play mode)

    The problem is that when I close down unity and open it again, the list elements are all gone. The screenshots shot a bit better what I mean. (The list is a list of 'Track' elements, a custom class)

    When I re-open unity, the game objects are all there in the hierarchy, with the settings left as they were.

    When I get the new errors, I have to use a line like this to reset it..
    Code (csharp):
    1.  
    2. void OnFocus(){
    3. audiosources.Clear ();
    4. }
    5.  
    It seems like the elements are there, but all the information for each object is gone from my 'track' objects, inside my list of track elements - 'audiosources' . Really not sure where to be looking to solve this: is it a serialisation issue? or a completely different issue I should look into

    Thanks for reading, I know it's quite lengthy

    Here's my scripts anyway: (I've removed some gui layout lines for ease of read )
    Track.cs:
    Code (csharp):
    1.  
    2. usingUnityEngine;
    3. usingSystem.Collections;
    4. usingUnityEditor;
    5.  
    6. [System.Serializable]
    7. public class Track {
    8.  
    9.  [SerializeField]
    10. publicstring_name;
    11.  
    12.  [SerializeField]
    13. publicfloat_volume;
    14.  
    15.  [SerializeField]
    16. publicfloat_pan;
    17.  
    18.  [SerializeField]
    19. publicGameObject_gameobject;
    20.  
    21.  [SerializeField]
    22. publicAudioClip_audioclip;
    23.  
    24. public Track()
    25.  {
    26.  
    27. _name = "Default Name";
    28. _volume = 1;
    29. _pan = 0;
    30.  
    31. _gameobject = GameObject.Instantiate (Resources.Load ("Track"), Vector3.zero, Quaternion.identity)asGameObject;
    32. _gameobject.transform.parent = GameObject.FindGameObjectWithTag ("AudioManager").transform;
    33.  
    34.  }
    35.  
    36. public void OnGUI()
    37.  {
    38.  
    39. //Volume
    40. GUILayout.Label ("Volume:", EditorStyles.boldLabel);
    41. _volume = GUILayout.HorizontalSlider(_volume, 0, 1);
    42. _gameobject.audio.volume = _volume;
    43.  
    44. //Pan
    45. GUILayout.Label ("Pan:", EditorStyles.boldLabel);
    46. _pan = GUILayout.HorizontalSlider(_pan, -1, 1);
    47. _gameobject.audio.pan = _pan;
    48.  
    49. //AudioClip
    50. GUILayout.Label ("AudioClip:", EditorStyles.boldLabel);
    51. _audioclip = EditorGUILayout.ObjectField(_audioclip, typeof(AudioClip), true) asAudioClip;
    52. _gameobject.audio.clip = _audioclip;
    53.  
    54. //Name
    55. GUILayout.Label ("Name:", EditorStyles.boldLabel);
    56. if(_name != null){
    57. _name = GUILayout.TextArea(_name);
    58.  }
    59. _gameobject.name = _name;
    60.  
    61.  }
    62.  
    63. }
    64.  

    Mixer.cs:
    Code (csharp):
    1.  
    2. usingUnityEngine;
    3. usingSystem.Collections;
    4. usingUnityEditor;
    5. usingSystem.Collections.Generic;
    6.  
    7. [System.Serializable]
    8. public class Mixer : EditorWindow {
    9.  
    10.  [SerializeField]
    11. public List<Track> audiosources;
    12.  
    13. AudioSource[] inscene;
    14.  
    15.  [MenuItem("Audio/Mixer")]
    16. static void Init()
    17.  {
    18.  
    19. Mixerwindow = (Mixer)Mixer.GetWindow <Mixer> ();
    20. window.title = "Mixer";
    21.  
    22.  }
    23.  
    24. voidOnEnable ()
    25.  {
    26.  
    27. hideFlags = HideFlags.HideAndDontSave;
    28.  
    29.  }
    30.  
    31. public void OnGUI()
    32.  {
    33.  
    34. if (audiosources != null ) {
    35. GUILayout.Label ("AS:" + audiosources.Count);
    36.  }
    37.  
    38. if(GUILayout.Button ("New"))
    39.  {
    40.  
    41. audiosources.Add(newTrack());
    42.  
    43.  }
    44.  
    45. for (inti = 0; i < audiosources.Count; i++) {
    46.  
    47. audiosources [i].OnGUI ();
    48.  
    49. if(GUILayout.Button ("Delete"))
    50.  {
    51.  
    52. stringtemp = audiosources[i]._name;
    53.  
    54. audiosources.Remove(audiosources[i]);
    55.  
    56. DestroyImmediate(GameObject.Find(temp));
    57.  
    58.  }
    59.  
    60.  }
    61.  
    62.  }
    63.  
    64. voidUpdate()
    65.  {
    66.  
    67. Repaint ();
    68.  
    69.  }
    70.  
    71. }
    72.  
     

    Attached Files:

  2. PolymorphiK Games

    PolymorphiK Games

    Joined:
    Oct 5, 2014
    Posts:
    51
    You want the data to be saved. You could either save the data to an XML, or extend to a .asset file where it can save the data internally then you just load it when you need it. This approach might best suit you as you might want to access the data at run-time in your game.
     
    ocimum likes this.
  3. BeautifulFish

    BeautifulFish

    Joined:
    Sep 25, 2014
    Posts:
    55
    Hey,
    Cheers for the quick reply!
    Saving sounds ok to me, I'm not too familiar with xml, so I think i'll give the .asset approach a look.
    Thanks again
     
    ocimum likes this.
  4. PolymorphiK Games

    PolymorphiK Games

    Joined:
    Oct 5, 2014
    Posts:
    51
    I made my own AudioManager its in the asset Store called "Audio Manager" I use the .asset approach works really well.