原创内容,转载请注明原文网址:http://homeqin.cn/a/wenzhangboke/jishutiandi/youxikaifa/2018/1229/285.html
常州手机游戏开发-Unity3D热更新-LuaFramework-声音管理器
LuaFramework内置的管理器包括GameManager(处理热更新)、luaManager(lua脚本管理器)、PanelManager(界面管理器)、NetworkManager(网络管理器)、ResourceManager(资源管理器)、TimerManager(时间管理器)、线程管理器(ThreadManager)和SoundManager(声音管理器)。这篇就给大家介绍下与播放声音相关的SoundManager(声音管理器)。
1、常州平台运营使用方法
SoundManager估计是从其他地方拷过来的,并不能很好的与框架结合,这里我们先看看原来的SoundManager的使用方法,再介绍它的不足之处及改进方法。虽然SoundManager定义了好几个方法,但能直接在lua中使用的只有用于播放背景音乐的PlayBacksound。
把声音文件放到Resource目录下,由于SoundManager使用Resources.Load加载声音文件,声音文件必须放到这个目录下。
然后编写lua代码,调用soundMgr:PlayBacksound即可,它的第一个参数指明Resource目录下的文件名,第二个参数为true表示开始播放,false表示停止播放。
--主入口函数。从这里开始lua逻辑
function Main()
LuaHelper = LuaFramework.LuaHelper;
soundMgr = LuaHelper.GetSoundManager();
soundMgr:PlayBacksound("testsound", true)
end
运行常州微信公众平台游戏,即可听到音效。
2、改进的声音管理器
这一部分我们需要改进声音管理器,实现这么几个功能:
• 从本地“数据目录”读取打包后的声音文件,使它支持热更新;
• 添加播放/停止背景音乐的PlayBackSound/StopBackSound和播放音效的PlaySound方法;
• 使用缓存存储加载后的声音文件,以提高运行效率。
为了支持加载AudioClip的加载,在ResourceManager中添加LoadAudioClip方法,该方法将会加载资源包abName的资源assetName,加载完AudioClip资源后调用回调函数func,常州微信小程序开发代码如下。
//载入音效资源
public void LoadAudioClip(string abName,
string assetName, Action<UObject[]> func)
{
LoadAsset<AudioClip>(abName, new string[] { assetName }, func);
}
修改SoundManager,使用Hashtable类型的sounds存储加载后的声音,包含PlayBackSound、StopBackSound和PlaySound三个API。代码如下所示。
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
namespace LuaFramework {
public class SoundManager : Manager {
private AudioSource audio;
private Hashtable sounds = new Hashtable();
string backSoundKey = "";
void Start() {
audio = GetComponent<AudioSource>();
if (audio == null)
{
gameObject.AddComponent<AudioSource>();
}
}
//常州网站开发建设回调函数原型
private delegate void GetBack(AudioClip clip, string key);
//获取声音资源
private void Get(string abName,string assetName,GetBack cb)
{
string key = abName + "." + assetName;
if (sounds[key] == null)
{
ResManager.LoadAudioClip(abName, assetName, (objs) =>
{
if (objs == null || objs[0] == null)
{
Debug.Log("PlayBackSound fail");
cb(null, key);
return;
}
else
{
sounds.Add(key, objs[0]);
cb(objs[0] as AudioClip, key);
return;
}
});
}
else
{
cb(sounds[key] as AudioClip, key);
return;
}
}
//游戏开发运营播放背景音乐
public void PlayBackSound(string abName, string assetName)
{
backSoundKey = abName + "." + assetName;
Get(abName, assetName, (clip, key) =>
{
if (clip == null)
return;
if (key != backSoundKey)
return;
audio.loop = true;
audio.clip = clip;
audio.Play();
});
}
//停止播放音乐
public void StopBackSound()
{
backSoundKey = "";
audio.Stop();
}
//播放音效
public void PlaySound(string abName, string assetName)
{
Get(abName, assetName, (clip, key) =>
{
if (clip == null)
return;
if (Camera.main == null)
return;
AudioSource.PlayClipAtPoint(clip,
Camera.main.transform.position);
});
}
}
}
修改代码后,需要重新生成wrap文件(点击菜单栏的Lua→Clear wrap files和Lua→Generate All)。然后编写lua代码调试它吧!这里演示的是先播放背景音乐,3秒后停止播放,每隔0.3秒播放一次音效的功能。
--常州手游开发主入口函数。从这里开始lua逻辑
function Main()
LuaHelper = LuaFramework.LuaHelper;
soundMgr = LuaHelper.GetSoundManager();
soundMgr:PlayBackSound("sound", "testsoundbg")
UpdateBeat:Add(Update, self)
end
local lastTime = 0
function Update()
if Time.time > 3 then
soundMgr:StopBackSound();
end
if Time.time - lastTime > 0.3 then
soundMgr:PlaySound("sound", "testsoundshoot")
lastTime = Time.time
end
end
上篇:上一篇:常州手游开发-Unity3D编写Lua逻辑
下篇:下一篇:常州微信小程序游戏-U3D陀螺仪检查手机方向