Unity3D开发之3D按钮的声音播放
这里我们首先就简易的制作一个非常简单的3D按钮
图中就一个cube 加个3DText,然后我们就编写代码
[RequireComponent(typeof(CompoundButton))]//特效用语
public class CompoundButtonSounds : ProfileButtonBase<ButtonSoundProfile>//泛型类需求T必须符合 where T 的要求
{
//这里说明下 定义的ButtonSoundProfile就一个声音源数组和声音值float数组 - -!这里就是为了装b用的
//CompoundButton 是摄像机检测用的组件(相当于事件源了)这个可以根据不同插件的事件源不同做处理
//Button的事件详细 我也在研究。。 - -!很蛋疼 (不同的插件可以选择不同的事件接口)
const float MinTimeBetweenSameClip = 0.1f;//常量最小按钮播放值
[SerializeField]//序列化
private AudioSource audioSource;
private static string lastClipName = string.Empty;//全局变量
private static float lastClipTime = 0f;
private Button.ButtonStateEnum lastState = Button.ButtonStateEnum.Disabled;//表示按钮交互的状态
void Start ()
{
Button button = GetComponent<Button>();
//以下是定义的按钮事件
button.OnButtonCancelled += OnButtonCancelled;
button.OnButtonHeld += OnButtonHeld;
button.OnButtonPressed += OnButtonPressed;
button.OnButtonReleased += OnButtonReleased;
button.StateChange += StateChange;
audioSource = GetComponent<AudioSource>();
}
/// <summary>
/// 状态改变
/// </summary>
/// <param name="newState"></param>
void StateChange(Button.ButtonStateEnum newState)
{
// 不能在同一个状态重复多次
if (lastState == newState)
return;
lastState = newState;
// 不活跃的按钮 不播放声音
if (!gameObject.activeSelf || !gameObject.activeInHierarchy)
return;
if (Profile == null)
{
Debug.LogError("Sound profile was null in button " + name);
return;
}
switch (newState)
{
case Button.ButtonStateEnum.Observation:
PlayClip(Profile.ButtonObservation, Profile.ButtonObservationVolume);
break;
case Button.ButtonStateEnum.ObservationTargeted:
PlayClip(Profile.ButtonObservationTargeted, Profile.ButtonObservationTargetedVolume);
break;
case Button.ButtonStateEnum.Targeted:
PlayClip(Profile.ButtonTargeted, Profile.ButtonTargetedVolume);
break;
default:
break;
}
}
//按钮取消
void OnButtonCancelled(GameObject go)
{
PlayClip(Profile.ButtonCancelled, Profile.ButtonCancelledVolume);
}
//按钮按住
void OnButtonHeld(GameObject go)
{
PlayClip(Profile.ButtonHeld, Profile.ButtonHeldVolume);
}
//按钮按下
void OnButtonPressed(GameObject go)
{
PlayClip(Profile.ButtonPressed, Profile.ButtonPressedVolume);
}
void OnButtonReleased (GameObject go)
{
PlayClip(Profile.ButtonReleased, Profile.ButtonReleasedVolume);
}
void PlayClip (AudioClip clip, float volume)
{
if (clip != null)
{
// 声音源是垃圾源,就不要做 Time.realtimeSinceStartup 静态属性表示 游戏开始实时时间
if (clip.name == lastClipName && Time.realtimeSinceStartup < MinTimeBetweenSameClip)
return;
lastClipName = clip.name;
lastClipTime = Time.realtimeSinceStartup;
if (audioSource != null)
{
audioSource.PlayOneShot(clip, volume);//这个不陌生吧(适合播放小源声音)
}
else
{
AudioSource.PlayClipAtPoint(clip, transform.position, volume);//这个是AudioSource的静态方法
//在transform.position位置创建一个空物体,并自动添加AudioSource组件,播放完成就会自动销毁
}
}
}
}
表述的也很明确了,最后就拖拽声音源到对应的属性中就行了,最后点击运行,点击按钮就ok了,
下次带来动物(老鼠)和气球的动态Mesh按钮还有文本图片等3D交互UI。最后搭建一个功能完善的UI组件
Unity3D开发之3D按钮的声音播放的更多相关文章
- iOS开发之3D Touch
1.简介 3DTouch是在6s之后苹果的一项技术,只能在6s及其以上机型真机运行,Xcode的模拟器是不支持的. Quick Actions(点击icon的快捷方式) Peek&Pop(应用 ...
- Unity3D 开发之shader教程(浅谈光照之漫反射diffuse)
在游戏开发过程中,光照应该是必不可少部分,当然,这是指大多数的稍微大型一些的3D游戏会需要,给模型或者山山水水加上光照,会看上去更加的真实,获得更好的体验.一个本身不发光物体显示什么颜色,在于本身反射 ...
- Unity3D开发之NGUI点击事件穿透响应处理
http://www.xuebuyuan.com/1936292.html 在使用NGUI 开发2D项目的时候,发现了一个问题,就是如果点出一个菜单,然后点击菜单上面的按钮的时候就会使得按钮下面的物品 ...
- 【转】Unity3D开发之Http协议网络通信
之前unity3d项目要做跟服务器通信的模块,然后服务器那边的协议是基于http的Jsonrpc通信方式一开始,用C#的本身类HttpWebRequest来提交请求,很快就在电脑上面成功了,代码也很简 ...
- Unity3D开发之Mac OS 开发环境搭建 笔记
http://www.cnblogs.com/zhaoqingqing/p/3383167.html 首先上几张图: 摸索了一上午,才搞定在模拟器中运行.至于在Iphone真机中运行,虽然有开发者证书 ...
- Unity3D开发之“获取IOS设备所在的国家代码"
原地址:http://dong2008hong.blog.163.com/blog/static/469688272014021025578/ 在前一段时间游戏开发中需要实现获取IOS设备所在的国家代 ...
- Unity3D开发之NGUI结合粒子系统的遮挡问题
原地址:http://blog.csdn.net/lihandsome/article/details/22194025 我的是NGUI3.0.3版本,在加入粒子系统的时候发现一直都是在精灵的下面,所 ...
- Unity3d 开发之 ulua 坑的总结
相同的 lua 代码在安卓上能正常运行,但在 IOS 上可能不会正常运行而导致报红,崩溃等,我在使用 lua 编程时遇到的一些坑总结如下: 1. File.ReadAllText, 诸如以下代码在 i ...
- Unity3D开发之Matrix4x4矩阵变换
在Unity开发中时常会用到Matrix4x4矩阵来变换场景中对象的位置.旋转和缩放.但是很多人都不太理解这儿Matrix4x4变换矩阵.通过DX中的变换矩阵我来讲一讲在unity中这个变换矩阵是怎么 ...
随机推荐
- 微软2014校招笔试题-String reorder
Time Limit:10000ms Case Time Limit:1000ms Memory Limit:256MB Description For this question, your pro ...
- 转载 12步轻松搞定python装饰器
作者: TypingQuietly 原文链接: https://www.jianshu.com/p/d68c6da1587a 呵呵!作为一名教python的老师,我发现学生们基本上一开始很难搞定pyt ...
- postgresql 唯一约束增强
http://blog.chinaunix.net/uid-15145533-id-2775821.html
- 爬虫--工具安装Jupyter anaconda
anaconda https://www.anaconda.com/download http://docs.anaconda.com/anaconda/user-guide/getting-star ...
- logstash grok 内置正则
参考地址:https://github.com/elastic/logstash/blob/v1.4.2/patterns/grok-patterns USERNAME [a-zA-Z0-9._-]+ ...
- Java RMI HelloWorld
Java RMI HelloWorld RMI 远程方法调用. 顾名思义就是可以像调用本地程序方法一样调用远程(其他JVM)的程序方法. 分为3个部分: Stub:中介,代理. 封装了远程对象 ...
- 等宽高的ImageButton
@SuppressLint("AppCompatCustomView") public class SquareImageButton extends ImageButton { ...
- Spring Boot + Spring Cloud 构建微服务系统(三):服务消费和负载(Feign)
Spring Cloud Feign Spring Cloud Feign是一套基于Netflix Feign实现的声明式服务调用客户端.它使得编写Web服务客户端变得更加简单.我们只需要通过创建接口 ...
- vector、deque、stack、queue、list以及set的使用
注意:以下测试案例都要加上相应的头文件,必要时要加上algorithm文件. 1.vector 连续存储结构,每个元素在内存上是连续的:支持高效的随机访问和在尾端插入/删除操作,但其他位置的插入/删除 ...
- C语言第六讲,数组
C语言第六讲,数组 一丶什么是数组 数组,就是一整块的连续内存空间. 且类型都是一样的.大小一样 比如: 1.1数组元素的访问 我们要访问数组,例如上面,我们访问元算2,元素3等等怎么访问.. 比如有 ...