Page 1 of 1

AudioClip custom saver

Posted: Thu Jul 29, 2021 2:50 pm
by SealDev
I use autosaving.
1. How could I custom save/load an AudioSource.clip? (remember currently playing music)
2. Should this script be outside the Plugins folder because it references a component?
3. Is it okay to use "new Awake" instead of override? (this script will be attached to the object containing the AudioSource)

Code: Select all

using UnityEngine;

namespace PixelCrushers
{

    public class SaverTemplate : Saver
    {
        private AudioClip song;

        private new void Awake()
        {
            song = GetComponent<AudioSource>().clip;
        }

        public override string RecordData()
        {
            SaveSystem.Serialize(song); //save obj to string
            return string.Empty;
        }
    }

Re: AudioClip custom saver

Posted: Thu Jul 29, 2021 3:37 pm
by Tony Li
Hi,

> 1. How could I custom save/load an AudioSource.clip? (remember currently playing music)

Like the example script below. However, this assumes that the audio clip asset is in a Resources folder. This way the saver can find the audio clip again when restoring saved data.


> 2. Should this script be outside the Plugins folder because it references a component?

It's up to you. It can be in Plugins because it only references class in Plugins and Unity engine classes. But you may want to keep it outside of Plugins to keep it with your other scripts.


> 3. Is it okay to use "new Awake" instead of override? (this script will be attached to the object containing the AudioSource)

No. You should almost never use "new" to cancel a parent class's method. Instead, override it so the parent's code can also run. See this version:

Code: Select all

using UnityEngine;

namespace PixelCrushers
{
    [RequireComponent(typeof(AudioSource))]
    public class AudioClipSaver : Saver
    {
        private AudioSource source;
        
        protected override void Awake()
        {
            base.Awake();
            source = GetComponent<AudioSource>();
        }
        
        public override string RecordData()
        {
            if (source.clip == null) 
            {
                return string.Empty;
            }
            else 
            {
                return source.clip.name;
            }
        }
        
        public override void ApplyData(string s)
        {
            if (s != "")
            {
                source.clip = Resources.Load<AudioClip>(s);
                if (source.clip != null)
                {
                    source.Play();
                }
            }
        }
    }

Re: AudioClip custom saver

Posted: Fri Jul 30, 2021 2:32 am
by SealDev
It works! Surprisingly this block of code is required even if the AudioSource was set to play on awake. Interesting.

Code: Select all

                if (source.clip != null)
                {
                    source.Play();
                }
Thank you so much!

Re: AudioClip custom saver

Posted: Fri Jul 30, 2021 7:55 am
by Tony Li
Glad to help!

That code is needed because you may be setting the clip after the audio source's Awake() method has run.