/// <summary> /// 实例化获取一个 AudioSources组件对象 /// </summary> /// <param name="name"></param> /// <returns></returns> private AudioSource InstantiateAudioSourceInstance(string name) { GameObject goInstance = ResourcesManagerUtility.Instantiate(name); goInstance.transform.SetParent(transform); return(goInstance.GetAddComponentEx <AudioSource>()); }
void Update() { if (Input.GetKeyDown(KeyCode.A)) { ResourcesManagerUtility.InstantiateAssetSync(prefabPath, parent, false); } }
/// <summary>/// 停止播放一个音效/// </summary> /// <param name="isReleaseClip">是否在停止的时候释放引用的音效资源</param> public void StopNormalAudio(string audioPath, bool isReleaseClip = false) { if (string.IsNullOrEmpty(audioPath)) { return; } string audioName = IOUtility.GetFileNameWithoutExtensionEx(audioPath); if (string.IsNullOrEmpty(audioName)) { return; } var isPlaying = IsPlayingNormalAudio(audioName, out var curAudioSources); if (isPlaying == false) { return; } curAudioSources.Stop(); if (isReleaseClip) { AudioClip clip = curAudioSources.clip; curAudioSources.clip = null; ResourcesManagerUtility.ReleaseComponentReferenceAsset(curAudioSources, clip); } RecycleNormalAudioSource(curAudioSources); }
/// <summary>/// 创建页面实例/// </summary> private static void CreateUIPageInstance(string pageName, string pagePath, Transform parent, Action <GameObject> afterInitialedInstanceAction, LoadAssetChannelUsage loadAssetChannel) { GameObject go = ResourcesManagerUtility.InstantiateAssetSync(pagePath, parent, false, loadAssetChannel); if (go != null) { go.name = pageName; } afterInitialedInstanceAction?.Invoke(go); }
/// <summary>/// 普通的音效播放/// </summary> /// <param name="isforceReplay">如果正在播放是否强制重播</param> public bool PlayNormalAudioSync(string audioPath, float volume, bool isforceReplay = false) { if (string.IsNullOrEmpty(audioPath)) { return(false); } string audioName = IOUtility.GetFileNameWithoutExtensionEx(audioPath); if (string.IsNullOrEmpty(audioName)) { return(false); } if (IsPlayingNormalAudio(audioName, out var curAudioSource)) { if (isforceReplay) { curAudioSource.Stop(); curAudioSource.Play(); } return(true); } curAudioSource = GetNextAvailableAudioSource(); LoadAssetResult <AudioClip> assetInfor = ResourcesManagerUtility.LoadAssetSync <AudioClip>(audioPath, null, null); if (assetInfor == null) { mAllAvailableAudioSources.Push(curAudioSource); return(false); } else { assetInfor.ReferenceWithComponent(curAudioSource, (audioClip) => { if (curAudioSource.clip == audioClip) { return; } ResourcesManagerUtility.ReleaseComponentReferenceAsset(curAudioSource, curAudioSource.clip); curAudioSource.clip = audioClip; }); curAudioSource.volume = volume; mAllPlayingAudioSources.AddLast(curAudioSource); return(true); } }
private bool PlayBackgroundAudioSync(LoadAssetResult <AudioClip> clipAsset, float volume, bool isLoop = true, bool isforceReplay = false) { if (clipAsset == null) { return(false); } if (mCurBackgroundAudioSource.clip != null) { if (mCurBackgroundAudioSource.clip.name == clipAsset.mLoadAssetInstanceName) { mCurBackgroundAudioSource.volume = volume; mCurBackgroundAudioSource.loop = isLoop; if (isforceReplay) { mCurBackgroundAudioSource.Stop(); mCurBackgroundAudioSource.Play(); } return(true); } //当前正在播放这个背景音 else { mCurBackgroundAudioSource.Stop(); //结束当前的背景音 } } clipAsset.ReferenceWithComponent(mCurBackgroundAudioSource, (audioClip) => { if (mCurBackgroundAudioSource.clip == audioClip) { return; } ResourcesManagerUtility.ReleaseComponentReferenceAsset(mCurBackgroundAudioSource, mCurBackgroundAudioSource.clip); mCurBackgroundAudioSource.clip = audioClip; return; }); mCurBackgroundAudioSource.volume = volume; mCurBackgroundAudioSource.loop = isLoop; mCurBackgroundAudioSource.Play(); return(true); }
/// <summary>/// 停止当前背景/// </summary> /// <param name="isReleaseClip">是否在停止的时候释放引用的音效资源</param> public void StopBackgroundAudio(bool isReleaseClip = false) { if (mCurBackgroundAudioSource == null) { return; } if (null == mCurBackgroundAudioSource.clip) { return; } mCurBackgroundAudioSource.Stop(); if (isReleaseClip) { AudioClip clip = mCurBackgroundAudioSource.clip; mCurBackgroundAudioSource.clip = null; ResourcesManagerUtility.ReleaseComponentReferenceAsset(mCurBackgroundAudioSource, clip); } }
/// <summary>/// 停止播放背景/// </summary> /// <param name="isReleaseClip">是否在停止的时候释放引用的音效资源</param> private void StopBackgroundAudio(string audioName, bool isReleaseClip = false) { if (mCurBackgroundAudioSource == null) { return; } if (mCurBackgroundAudioSource.clip == null) { return; } if (mCurBackgroundAudioSource.clip.name != audioName) { return; } mCurBackgroundAudioSource.Stop(); if (isReleaseClip) { AudioClip clip = mCurBackgroundAudioSource.clip; mCurBackgroundAudioSource.clip = null; ResourcesManagerUtility.ReleaseComponentReferenceAsset(mCurBackgroundAudioSource, clip); } }
/// <summary> /// 播放背景音 /// </summary> /// <param name="audioPath"></param> /// <param name="volume"></param> /// <param name="isLoop"></param> /// <param name="isforceReplay"></param> /// <returns></returns> public bool PlayBackgroundAudioSync(string audioPath, float volume, bool isLoop = true, bool isforceReplay = false) { if (string.IsNullOrEmpty(audioPath)) { return(false); } string audioName = IOUtility.GetFileNameWithoutExtensionEx(audioPath); if (string.IsNullOrEmpty(audioName)) { return(false); } if (mCurBackgroundAudioSource.clip != null) { if (mCurBackgroundAudioSource.clip.name == audioName) { mCurBackgroundAudioSource.volume = volume; mCurBackgroundAudioSource.loop = isLoop; if (isforceReplay) { mCurBackgroundAudioSource.Stop(); mCurBackgroundAudioSource.Play(); } return(true); } //当前正在播放这个背景音 } LoadAssetResult <AudioClip> clipAsset = ResourcesManagerUtility.LoadAssetSync <AudioClip>(audioPath, null, null); if (clipAsset == null) { return(false); } return(PlayBackgroundAudioSync(clipAsset, volume, isLoop, isforceReplay)); }