Created
May 8, 2020 03:35
-
-
Save nosix/c097599a80c991bb018753f53bcb18ec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEditor; | |
using UnityEngine; | |
using UnityEngine.AddressableAssets; | |
namespace AssetLoader | |
{ | |
[RequireComponent(typeof(AudioSource))] | |
public class AudioSourceLoader : MonoBehaviour | |
{ | |
[SerializeField] private AssetReferenceAudioClip audioClip; | |
#if UNITY_EDITOR | |
private void OnValidate() | |
{ | |
if (audioClip == null) | |
{ | |
var clip = GetComponent<AudioSource>().clip; | |
if (clip == null) return; | |
var guid = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(clip)); | |
audioClip = new AssetReferenceAudioClip(guid); | |
} | |
else | |
{ | |
GetComponent<AudioSource>().clip = null; | |
} | |
} | |
#endif | |
private void Start() | |
{ | |
audioClip.LoadAssetAsync<AudioClip>().Completed += handle => | |
{ | |
GetComponent<AudioSource>().clip = handle.Result; | |
}; | |
} | |
} | |
// Generics は Inspector に表示されないため定義している | |
[System.Serializable] | |
public class AssetReferenceAudioClip : AssetReferenceT<AudioClip> | |
{ | |
public AssetReferenceAudioClip(string guid) : base(guid) | |
{ | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment