mycj_demo/mycj/Assets/Game/Scripts/FB-Framework/FB-Sound/FBSound.cs
2024-12-02 09:37:47 +08:00

102 lines
2.6 KiB
C#
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Felix-BangFBSound
//   へ     /|
//  /7    ∠_/
//  / │    
//  Z _,    /`ヽ
// │     ヽ   /  〉
//  Y     `  /  /
// イ● 、 ●  ⊂⊃〈  /
// ()  へ    | \〈
//  >ー 、_  ィ  │
//  / へ   / ノ<|
//  ヽ_ノ  (_  │//
//  7       |
//  ―r ̄ ̄`ー―_
// Describe
// Createtime
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace FBFramework
{
public class FBSound : FBSingleton<FBSound>
{
public string ResourceDir = "";
AudioSource f_bgm;
AudioSource f_effectSound;
public float BgmVolum
{
get { return f_bgm.volume; }
set { f_bgm.volume = value; }
}
public float EffectVolum
{
get { return f_effectSound.volume; }
set { f_effectSound.volume = value; }
}
protected override void Awake()
{
base.Awake();
f_bgm = this.gameObject.AddComponent<AudioSource>();
f_bgm.playOnAwake = true;
f_bgm.loop = true;
f_effectSound = this.gameObject.AddComponent<AudioSource>();
}
/// <summary> 播放背景音乐 </summary>
public void PlayBgm(string audioName)
{
string oldName = "";
if (f_bgm.clip == null)
oldName = "";
else
oldName = f_bgm.clip.name;
if (string.IsNullOrEmpty(oldName))
{
AudioClip clip = GetClip(audioName);
if (clip != null)
{
f_bgm.clip = clip;
f_bgm.Play();
}
}
}
/// <summary> 关闭背景音乐 </summary>
public void StopBgm()
{
f_bgm.Stop();
f_bgm.clip = null;
}
/// <summary> 播放音效 </summary>
public void PlayEffect(string audioName)
{
AudioClip clip = GetClip(audioName);
f_effectSound.PlayOneShot(clip);
}
private AudioClip GetClip(string audioName)
{
string path = string.Empty;
if (string.IsNullOrEmpty(ResourceDir))
path = string.Empty;
else
path = ResourceDir + "/" + audioName;
return Resources.Load<AudioClip>(path);
}
}
}