Unity3D 游戏引擎之详解游戏开发音频的播放(十五)

Unity3D 游戏引擎之详解游戏开发音频的播放




雨松MOMO原创文章如转载,请注明:转载至我的独立域名博客雨松MOMO程序研究院,原文地址:http://www.xuanyusong.com/archives/550






       游戏音频的播放在任何游戏中都占据非常重要的地位,音频的播放还可以分为两种,一种为游戏音乐,另一种为游戏音效。前者适用于较长的音乐,如游戏背景音乐。第二种试用与比较短的游戏音乐,如开枪,打怪 时“砰砰”一瞬间播放的游戏音效。今天MOMO将用下面的例子带盆友们去剖析Unity3D游戏音乐与音效的播放。

 


Unity3D游戏引擎一共支持4个音乐格式的文件


.AIFF  适用于较短的音乐文件可用作游戏打斗音效

.WAV  适用于较短的音乐文件可用作游戏打斗音效

.MP3  适用于较长的音乐文件可用作游戏背景音乐

.OGG  适用于较长的音乐文件可用作游戏背景音乐

      


在场景中创建一个空的游戏对象。

GameObject -> CreateEmpty  创建成功后我命名为audio。

给audio添加一个AudioSource属性,这个属性非常的重要,Unity播放音乐主要就是要靠这个东西。


Component -> Audio - > Audio Source。









找一个音乐文件,我这里使用了一个.mp3音乐文件,我命名为0.mp3 如下图所示将它拖动到右侧Audio Clip处。







我们发现 Audio Source 有很多设置的属性,那么MOMO将一些比较重要的属性诺列出来。


AudioClip :声音片段,还可以在代码中去动态的截取音乐文件。

Mute : 是否静音

Bypass Effects: 是否打开音频特效

Play On Awake: 开机自动播放

Loop:循环播放

Volume: 声音大小,取值范围0.0 到 1.0

Pitch:播放速度,取值范围在 -3 到 3 之间 设置1 为正常播放,小于1 为减慢播放 大于1为加速播放。





创建一个脚本我命名为audio.cs 用来音乐的播放。本章将实现3个按钮 点击实现 播放音乐、 停止音乐、 暂停音乐 ,与一个横向拖动条通过手指的拖动实现动态修改音乐声音。


audio.cs 

using UnityEngine;
using System.Collections;

public class audio : MonoBehaviour {

    //音乐文件
    public AudioSource music;	
    //音量
    public float musicVolume;	
    
    void Start() {
   		 //设置默认音量
   		 musicVolume = 0.5F;	
    }
	void OnGUI() {
		
		//播放音乐按钮
		if (GUI.Button(new Rect(10, 10, 100, 50), "Play music"))  {
			
			//没有播放中
			if (!music.isPlaying){
				//播放音乐
				music.Play();
			}
			
		}
		
		//关闭音乐按钮
		if (GUI.Button(new Rect(10, 60, 100, 50), "Stop music"))  {
			
			if (music.isPlaying){
				//关闭音乐
				music.Stop();
			}
		}
		//暂停音乐
		if (GUI.Button(new Rect(10, 110, 100, 50), "Pause music"))  {
			if (music.isPlaying){
				//暂停音乐
				//这里说一下音乐暂停以后
				//点击播放音乐为继续播放
				//而停止以后在点击播放音乐
				//则为从新播放
				//这就是暂停与停止的区别
				music.Pause();
			}
		}

		//创建一个横向滑动条用于动态修改音乐音量
		//第一个参数 滑动条范围
		//第二个参数 初始滑块位置
		//第三个参数 起点
		//第四个参数 终点
		musicVolume = GUI.HorizontalSlider (new Rect(160, 10, 100, 50), musicVolume, 0.0F, 1.0F);
	
		//将音量的百分比打印出来
		GUI.Label(new Rect(160, 50, 300, 20), "Music Volueme is " + (int)(musicVolume * 100) + "%");
		
		if (music.isPlaying){
			//音乐播放中设置音乐音量 取值范围 0.0F到 1.0 
			music.volume = musicVolume;
		}
	}
}



将audio.cs 绑定在摄像头上,将audio游戏对象拖动赋值给Music 这个AudioSource这个对象。这里强调一下AudioListenr ,它音频监听器,用来监听音乐文件的播放。这是一个重要的属性,一定要勾选,只有勾选后才可以进行音乐的播放。







build and run 运行我们这个游戏Demo,一切功能完美实现,哇咔咔~





最后欢迎各位盆友可以和MOMO一起讨论Unity3D游戏开发,大家一起学习一起进步,哇咔咔~~~ 附上Unity3D工程的下载地址,Xcode项目我就不上传了,须要的自己导出。今天心里有点不高兴! 5555555555。就这样晚安~


下载地址:http://www.xuanyusong.com/archives/550




  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
DisUnity v0.2.1 提取.assets .unity3d 文本 音频 字体 纹理图片 shader脚本等 不可用于非法破解他人资源,不可用于商业目的,仅供学习研究为目的!任何责任与上传者无关! 需JVM!!! 帮助: I've been working on an asset extraction tool for Unity-based games, since I needed such a tool for two Unity games I have. Moreover, the game engine has become very popular, so the unpacker should theoretically work with thousands of different games. Right now, it supports text, audio clips, most textures (including movie textures), fonts and substances from Unity versions ranging from 2.6 to 4.2. Support for static meshes is also planned, but will be added later, due to the complexity of the mesh data. There's no GUI for the tool yet, since it's still in an early experimental stage. So if you want to use it, you should have at least basic knowledge with the command line interface. :wink: Both source code and compiled builds are available on GitHub here. It is written in Java, so you'll also need to install the Java VM. For a quick start, you can use DisUnity like this: Code: disunity "resources.assets" (also works with .unity3d files) which is equivalent to: Code: disunity -c extract "resources.assets" If that doesn't work, try this: Code: java -jar disunity.jar "resources.assets" The files should then appear in a subfolder with the same name as the file. You can also try to play with the other parameters and commands that are listed with the -h parameter, but I think most normal people won't need these right now.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值