网站开发软件要求,wordpress 大前端2.0,网站左侧浮动代码,wordpress wp();unity3d入门教程九 20.2播放音频20.3在代码中播放21.1延时调用21.2invoke API21.3消息调用22.1交互界面22.2添加canvas22.3canavas的位置22.4添加text 这里给一个资源网站#xff0c;可以部分免费下载#xff0c;音乐和音效超多#xff0c;支持检索 爱给网 https://www.aige… unity3d入门教程九 20.2播放音频20.3在代码中播放21.1延时调用21.2invoke API21.3消息调用22.1交互界面22.2添加canvas22.3canavas的位置22.4添加text 这里给一个资源网站可以部分免费下载音乐和音效超多支持检索 爱给网 https://www.aigei.com/music/
在unity显示为波形图可以直接将音频拖入其中
以后会有专门的人员提供音乐等素材
我们一开始都是使用别人开发好的资源使用的
20.2播放音频 1 检查有没有设置音频资源
2 Play On Awake 是否勾选
3 Game窗口上的 Mute Audio 不要禁止
不要选中Mute Audio 。如果按下去则为 Mute 禁音。可以自己试一下。
20.3在代码中播放 using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class MyPiano : MonoBehaviour
{// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){if (Input.GetMouseButtonDown( 0 )){// 判断鼠标是否点中当前物体Vector3 mousePos Camera.main.ScreenToWorldPoint(Input.mousePosition);mousePos.z 0;float distance (mousePos - transform.position).magnitude;if(distance 2 ){// 取得 AudioSource 组件AudioSource audio GetComponentAudioSource();audio.Play();//if(! audio.isPlaying)//{// audio.Play();//}//如果正在播放则停止否则启动//if(audio.isPlaying)//{// audio.Stop();//}//else//{// audio.Play();//}}}}
}另外一个api一次性播放新开一个播放如射击游戏时的播放
多次点击多次声音都会播出产生叠加的效果
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class MyGun : MonoBehaviour
{// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){if (Input.GetMouseButtonDown(0)){Vector3 mousePos Camera.main.ScreenToWorldPoint(Input.mousePosition);mousePos.z 0;float distance (mousePos - transform.position).magnitude;if (distance 2){ AudioSource audio GetComponentAudioSource();audio.PlayOneShot(audio.clip);// audio.Play();}}}
}21.1延时调用 using System.Collections;
using System.Collections.Generic;
using UnityEngine;/** Unity游戏开发入门教程* * 作者 邵发* * 官网 http://afanihao.cn/game/index.html* */public class MyGirl : MonoBehaviour
{// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){if(Input.GetMouseButtonDown(0)){Debug.Log(22 该去上班了 , time Time.time);// Response();Invoke(Response, 3);}}// 应答void Response(){Debug.Log(知道啦! time Time.time);}
}虽然自己可以计时延迟时间再调用函数但直接使用已有的延时函数就比较方便了
21.2invoke API 21.3消息调用 MyGame using System.Collections;
using System.Collections.Generic;
using UnityEngine;/** Unity游戏开发入门教程* * 作者 邵发* */public class MyGame : MonoBehaviour
{// public int score 0;// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){}public void AddScore(int value){this.score value;Debug.Log(得分1 ,当前总分 score);// ... 更新UI显示 ...}
}MyPet using System.Collections;
using System.Collections.Generic;
using UnityEngine;/** Unity游戏开发入门教程* * 作者 邵发* * 官网 http://afanihao.cn/game/index.html* */public class MyPet : MonoBehaviour
{// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){// 判断鼠标是否点中当前物体if(Input.GetMouseButtonDown(0)){Vector3 mousePos Camera.main.ScreenToWorldPoint(Input.mousePosition);mousePos.z 0;float distance (mousePos - transform.position).magnitude;if (distance 2){// 打中了// GameObject main GameObject.Find(游戏主控);//MyGame myGame main.GetComponentMyGame();//myGame.AddScore(1);GameObject main GameObject.Find(游戏主控);// 在游戏主控节点上遍历所有的脚本组件看哪个脚本有AddScore方法并执行这个方法main.SendMessage(AddScore, 1);}}}
}22.1交互界面 如上按钮控件等不属于后面的游戏空间无论其如何变化按钮等操作都不会变化
22.2添加canvas 1 添加 Canvas 在 Hierarchy窗口右键 GameObject | UI | Canvas
同时添加一个 Canvas 和一个 EventSystem 事件系统管理器
2 指定 Canvas 的显式方式 选中 Canvas 节点
Render Mode : 设为 Screen Space – Camera Render Camera 指向 Main Camera Plane Distance 显示平面与摄像机的距离 设为 5 此时Canvas 覆盖整个屏幕空间
3 添加 Text 文件控件 注意所有 UGUI元素都应该放在 Canvas 节点下面
右键选中 Canvas 添加一个 Text 子节点
设置一下 Text 的属性
Best Fit 自动调整字体大小适应 Rect矩形框 Color : 文本的颜色
颜色选择时先外圈选择大致范围再内圈进行精细调整
22.3canavas的位置 可以在3D的效果下观察游戏对象和canavas是不在同一平面的
22.4添加text 可新建目录font在此加入字体资源将字体资源拖入可以charactor可以改变字体的形式