您的位置:首页 > 移动开发 > Unity3D

005-unity3d 添加背景音乐、音效 以及 天空盒子

2018-01-07 12:07 323 查看

一、基础知识

1、项目中需要有AudioListener,播放器中播放的声音就是AudioListener组件坐在的位置听到的声音。默认AudioListener是放到Main Camera上。没有AudioListener的话是听不到声音的。一般默认就在摄像机上。
2、把音乐拖到Assets中,选中要播放音乐的游戏对象(如果鸡叫、坦克爆炸等声音一般放到鸡、坦克这些游戏对象上,而背景音乐等则一般放到摄像机上),点击主菜单:Component→Audio→audio source 增加Audio组件到GameObject上,然后把音乐文件拖到组件的Audio Clip属性上即可。Mute设定是否静音,Play On Awake为自动播放,Loop 循环播放,Volumn为音量。

二、示例

1、使用打箱子示例:http://www.cnblogs.com/bjlhx/p/8214277.html

2、查看主摄像机是否含有,AudioListener,如果缺失,可以再菜单中添加Component

  

public class Init : MonoBehaviour {
private GameObject goPlane;

// Use this for initialization
void Start()
{
goPlane = GameObject.Find("Plane");
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
GameObject go = GameObject.CreatePrimitive(PrimitiveType.Cube);
go.transform.position = new Vector3(i, j, -1);
if (j % 2 == 0) {
go.GetComponent<Renderer>().material.color = Color.red;
}
go.AddComponent<Rigidbody>();
go.AddComponent<AutoDestory>();
}
}

}

// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
GameObject goNew = GameObject.CreatePrimitive(PrimitiveType.Sphere);
goNew.transform.position = Camera.main.transform.position;
goNew.AddComponent<Rigidbody>();
goNew.AddComponent<AutoDestory>();
goPlane.GetComponent<AudioSource>().Play();

Vector3 targetPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 3));
goNew.GetComponent<Rigidbody>().AddForce((targetPos - Camera.main.transform.position) * 10, ForceMode.Impulse);
}
}
}


View Code
注意代码中的goPlane。

三、天空盒子

  天空盒子一共有六个面,可以单独设定贴图,可以导入一些内置的免费SkyBox。

  5.0版本以前

    在Project上点击右键,选择:Import package →Characters→SkyBoxs.然后在主菜单选择:Edit→Render Settings,然后在再Inspector面板中,修改SkyBox meterial,选择导入的素材即可。

  5.0版本以后

    默认已添加

    如果修改,主菜单→windows→lighting
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: