zoukankan      html  css  js  c++  java
  • 005-unity3d 添加背景音乐、音效 以及 天空盒子

    一、基础知识

    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

      

    3、添加音乐素材,直接从本地拖拽至Assets目录中。

    4、添加背景音乐,一般默认添加在主摄像机上,Component→Audio→Audio source

      

    5、添加音效,添加到具体事物上。

      这里添加到地面上,每次球体发射时,触发声音。使用脚本控制

    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

  • 相关阅读:
    krdoc 工程
    在ubuntu16.04 上安装php5.6.30
    https自签名 证书签发
    websocket 实现 前端vue-socket.io 服务端 koa2(socket.io)
    工作杂项 0528未 整理
    python 创建django项目;
    关于conda,pip使用杂项:
    笔记 日常
    ubuntu 16.04下 django 1.11.1项目启动问题(kr_doc)
    react-native如何看待 this.setState()
  • 原文地址:https://www.cnblogs.com/bjlhx/p/8227671.html
Copyright © 2011-2022 走看看