AudioClip custom saver

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
SealDev
Posts: 85
Joined: Thu Jun 24, 2021 5:45 am

AudioClip custom saver

Post 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;
        }
    }
User avatar
Tony Li
Posts: 22161
Joined: Thu Jul 18, 2013 1:27 pm

Re: AudioClip custom saver

Post 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();
                }
            }
        }
    }
SealDev
Posts: 85
Joined: Thu Jun 24, 2021 5:45 am

Re: AudioClip custom saver

Post 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!
User avatar
Tony Li
Posts: 22161
Joined: Thu Jul 18, 2013 1:27 pm

Re: AudioClip custom saver

Post 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.
Post Reply