-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUIButtonSound.cs
More file actions
executable file
·74 lines (64 loc) · 1.72 KB
/
Copy pathUIButtonSound.cs
File metadata and controls
executable file
·74 lines (64 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using UnityEngine;
using System.Collections;
public class UIButtonSound : MonoBehaviour {
AudioListener mListener;
public enum Trigger
{
OnClick,
OnMouseOver,
OnMouseOut,
OnPress,
OnRelease,
}
public AudioClip audioClip;
public Trigger trigger = Trigger.OnClick;
public float volume = 1f;
public float pitch = 1f;
void OnHover (bool isOver)
{
if (enabled && ((isOver && trigger == Trigger.OnMouseOver) || (!isOver && trigger == Trigger.OnMouseOut)))
{
PlaySound(audioClip, volume, pitch);
}
}
void OnPress (bool isPressed)
{
if (enabled && ((isPressed && trigger == Trigger.OnPress) || (!isPressed && trigger == Trigger.OnRelease)))
{
PlaySound(audioClip, volume, pitch);
}
}
public void OnClick ()
{
int v = PlayerPrefs.GetInt ("Sound", 1);
if (enabled && trigger == Trigger.OnClick && v != 0)
{
PlaySound(audioClip, volume, pitch);
}
}
AudioSource PlaySound (AudioClip clip, float volume, float pitch)
{
if (clip != null && volume > 0.01f)
{
if (mListener == null)
{
mListener = GameObject.FindObjectOfType(typeof(AudioListener)) as AudioListener;
if (mListener == null)
{
Camera cam = Camera.main;
if (cam == null) cam = GameObject.FindObjectOfType(typeof(Camera)) as Camera;
if (cam != null) mListener = cam.gameObject.AddComponent<AudioListener>();
}
}
if (mListener != null && mListener.enabled && mListener.gameObject && mListener.gameObject.activeInHierarchy)
{
AudioSource source = mListener.GetComponent<AudioSource>();
if (source == null) source = mListener.gameObject.AddComponent<AudioSource>();
source.pitch = pitch;
source.PlayOneShot(clip, volume);
return source;
}
}
return null;
}
}