这里我们首先就简易的制作一个非常简单的3D按钮![这里写图片描述](https://img-blog.csdn.net/20170915120955448?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfNDAyNTI1Mzk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
图中就一个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组件