CardboardAudioSourceEditor.cs
8.29 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright 2015 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using UnityEngine;
using UnityEditor;
using System.Collections;
/// A custom editor for properties on the CardboardAudioSource script. This appears in the Inspector
/// window of a CardboardAudioSource object.
[CustomEditor(typeof(CardboardAudioSource))]
[CanEditMultipleObjects]
public class CardboardAudioSourceEditor : Editor {
private SerializedProperty clip = null;
private SerializedProperty loop = null;
private SerializedProperty mute = null;
private SerializedProperty pitch = null;
private SerializedProperty playOnAwake = null;
private SerializedProperty volume = null;
private SerializedProperty rolloffMode = null;
private SerializedProperty maxDistance = null;
private SerializedProperty minDistance = null;
private SerializedProperty directivityAlpha = null;
private SerializedProperty directivitySharpness = null;
private Texture2D directivityTexture = null;
private SerializedProperty gainDb = null;
private SerializedProperty hrtfEnabled = null;
private SerializedProperty occlusionEnabled = null;
private GUIContent clipLabel = new GUIContent("AudioClip",
"The AudioClip asset played by the CardboardAudioSource.");
private GUIContent loopLabel = new GUIContent("Loop",
"Sets the source to loop.");
private GUIContent muteLabel = new GUIContent("Mute",
"Mutes the sound.");
private GUIContent pitchLabel = new GUIContent("Pitch",
"Sets the frequency of the sound. Use this to slow down or speed up the sound.");
private GUIContent volumeLabel = new GUIContent("Volume",
"Sets the overall volume of the sound.");
private GUIContent rolloffModeLabel = new GUIContent("Volume Rolloff",
"Which type of rolloff curve to use.");
private GUIContent maxDistanceLabel = new GUIContent("Max Distance",
"Max distance is the distance a sound stops attenuating at.");
private GUIContent minDistanceLabel = new GUIContent("Min Distance",
"Within the min distance, the volume will stay at the loudest possible. " +
"Outside this min distance it will begin to attenuate.");
private GUIContent playOnAwakeLabel = new GUIContent("Play On Awake",
"Play the sound when the scene loads.");
private GUIContent directivityLabel = new GUIContent("Directivity",
"Controls the pattern of sound emission of the source. This can change the perceived " +
"loudness of the source depending on which way it is facing relative to the listener. " +
"Patterns are aligned to the 'forward' direction of the parent object.");
private GUIContent directivityAlphaLabel = new GUIContent("Alpha",
"Controls the balance between dipole pattern and omnidirectional pattern for source " +
"emission. By varying this value, differing directivity patterns can be formed.");
private GUIContent directivitySharpnessLabel = new GUIContent("Sharpness",
"Sets the sharpness of the directivity pattern. Higher values will result in increased " +
"directivity.");
private GUIContent gainLabel = new GUIContent("Gain (dB)",
"Applies a gain to the source for adjustment of relative loudness.");
private GUIContent hrtfEnabledLabel = new GUIContent("Enable HRTF",
"Sets HRTF binaural rendering for the source. Note that this setting has no effect when " +
"global low quality mode is selected.");
private GUIContent occlusionLabel = new GUIContent("Enable Occlusion",
"Sets whether the sound of the source should be occluded when passing through objects " +
"between the source and the listener.");
void OnEnable () {
clip = serializedObject.FindProperty("sourceClip");
loop = serializedObject.FindProperty("sourceLoop");
mute = serializedObject.FindProperty("sourceMute");
pitch = serializedObject.FindProperty("sourcePitch");
playOnAwake = serializedObject.FindProperty("playOnAwake");
volume = serializedObject.FindProperty("sourceVolume");
rolloffMode = serializedObject.FindProperty("rolloffMode");
maxDistance = serializedObject.FindProperty("sourceMaxDistance");
minDistance = serializedObject.FindProperty("sourceMinDistance");
directivityAlpha = serializedObject.FindProperty("directivityAlpha");
directivitySharpness = serializedObject.FindProperty("directivitySharpness");
#if UNITY_4_5
directivityTexture = Texture2D.CreateExternalTexture(0, 0, TextureFormat.ARGB32, false, false,
System.IntPtr.Zero);
#else
directivityTexture = Texture2D.blackTexture;
#endif
gainDb = serializedObject.FindProperty("gainDb");
hrtfEnabled = serializedObject.FindProperty("hrtfEnabled");
occlusionEnabled = serializedObject.FindProperty("occlusionEnabled");
}
/// @cond
public override void OnInspectorGUI () {
serializedObject.Update();
EditorGUILayout.PropertyField(clip, clipLabel);
EditorGUILayout.Separator();
EditorGUILayout.PropertyField(mute, muteLabel);
EditorGUILayout.PropertyField(playOnAwake, playOnAwakeLabel);
EditorGUILayout.PropertyField(loop, loopLabel);
EditorGUILayout.Separator();
EditorGUILayout.PropertyField(volume, volumeLabel);
EditorGUILayout.Separator();
EditorGUILayout.PropertyField(pitch, pitchLabel);
EditorGUILayout.Separator();
EditorGUILayout.Slider(gainDb, CardboardAudio.minGainDb, CardboardAudio.maxGainDb, gainLabel);
EditorGUILayout.Separator();
EditorGUILayout.PropertyField(rolloffMode, rolloffModeLabel);
++EditorGUI.indentLevel;
EditorGUILayout.PropertyField(minDistance, minDistanceLabel);
EditorGUILayout.PropertyField(maxDistance, maxDistanceLabel);
--EditorGUI.indentLevel;
if (rolloffMode.enumValueIndex == (int)AudioRolloffMode.Custom) {
EditorGUILayout.HelpBox("Custom rolloff mode is not supported.", MessageType.Error);
}
EditorGUILayout.Separator();
EditorGUILayout.BeginHorizontal();
EditorGUILayout.BeginVertical();
GUILayout.Label(directivityLabel);
++EditorGUI.indentLevel;
EditorGUILayout.Slider(directivityAlpha, 0.0f, 1.0f, directivityAlphaLabel);
EditorGUILayout.Slider(directivitySharpness, 1.0f, 10.0f, directivitySharpnessLabel);
--EditorGUI.indentLevel;
EditorGUILayout.EndVertical();
DrawDirectivityPattern((int)(3.0f * EditorGUIUtility.singleLineHeight));
EditorGUILayout.EndHorizontal();
EditorGUILayout.PropertyField(occlusionEnabled, occlusionLabel);
EditorGUILayout.Separator();
// HRTF toggle can only be modified through the Inspector in Edit mode.
GUI.enabled = !EditorApplication.isPlaying;
EditorGUILayout.PropertyField(hrtfEnabled, hrtfEnabledLabel);
GUI.enabled = true;
serializedObject.ApplyModifiedProperties();
}
/// @endcond
private void DrawDirectivityPattern (int size) {
directivityTexture.Resize(size, size);
// Draw the axes.
Color axisColor = 0.5f * Color.black;
for (int i = 0; i < size; ++i) {
directivityTexture.SetPixel(i, size / 2, axisColor);
directivityTexture.SetPixel(size / 2, i, axisColor);
}
// Draw the 2D polar directivity pattern.
Color cardioidColor = 0.75f * Color.blue;
float offset = 0.5f * size;
float cardioidSize = 0.45f * size;
Vector2[] vertices = CardboardAudio.Generate2dPolarPattern(directivityAlpha.floatValue,
directivitySharpness.floatValue,
180);
for (int i = 0; i < vertices.Length; ++i) {
directivityTexture.SetPixel((int)(offset + cardioidSize * vertices[i].x),
(int)(offset + cardioidSize * vertices[i].y),
cardioidColor);
}
directivityTexture.Apply();
// Show the texture.
GUILayout.Box(directivityTexture);
}
}