Esempio n. 1
0
    public static void CreateWindow()
    {
        AudioManagerEditor window = EditorWindow.GetWindow <AudioManagerEditor>();

        window.titleContent.text = "AudioManager";
        window.Show();
    }
Esempio n. 2
0
        void ShowWaves()
        {
            AudioClip clip = clipProperty.GetValue <AudioClip>();
            string    playRangeStartSeconds = clip == null ? "" : "(" + (sourceSettings.PlayRangeStart * clip.length).Round(0.01f) + "s)";
            string    playRangeEndSeconds   = clip == null ? "" : "(" + (sourceSettings.PlayRangeEnd * clip.length).Round(0.01f) + "s)";
            string    curvesLabel           = string.Format("Start: {0} {2} | End: {1} {3}", sourceSettings.PlayRangeStart.Round(0.01f), sourceSettings.PlayRangeEnd.Round(0.01f), playRangeStartSeconds, playRangeEndSeconds);

            EditorGUILayout.LabelField(curvesLabel, GUILayout.Height(22f));
            AudioManagerEditor.ShowPreviewButton(EditorGUI.IndentedRect(GUILayoutUtility.GetLastRect()), sourceSettings);

            EditorGUI.BeginChangeCheck();

            Rect rect = EditorGUI.IndentedRect(GUILayoutUtility.GetLastRect());

            EditorGUI.MinMaxSlider(new Rect(rect.x - 9f, rect.y + 12f, rect.width + 10f, rect.height), ref sourceSettings.PlayRangeStart, ref sourceSettings.PlayRangeEnd, 0f, 1f);

            if (EditorGUI.EndChangeCheck())
            {
                for (int i = 0; i < targets.Length; i++)
                {
                    AudioSourceSettings settings = (AudioSourceSettings)targets[i];
                    settings.PlayRangeStart = float.IsNaN(sourceSettings.PlayRangeStart) ? 0f : Mathf.Clamp(sourceSettings.PlayRangeStart, 0f, settings.PlayRangeEnd);
                    settings.PlayRangeEnd   = float.IsNaN(sourceSettings.PlayRangeEnd) ? 1f : Mathf.Clamp(sourceSettings.PlayRangeEnd, settings.PlayRangeStart, 1f);
                }

                serializedObject.Update();
                ClampFades();
            }


            if (clip == null || clip.channels == 1)
            {
                ShowWave(textureLeft, 40f);
            }
            else
            {
                ShowWave(textureLeft, 20f);
                ShowWave(textureRight, 20f);
            }
        }
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        if (_manager == null)
        {
            _manager = Object.FindObjectOfType <AudioManager>();
        }
        if (_manager == null)
        {
            EditorGUI.LabelField(position, label.text + ": AudioManager required on scene to draw gui", EditorStyles.boldLabel);
            EditorGUI.PropertyField(position, property, label, true);
            return;
        }

        SerializedProperty type  = property.FindPropertyRelative("Type");
        SerializedProperty id    = property.FindPropertyRelative("Id");
        SerializedProperty clips = property.FindPropertyRelative("Clips");

        var labelWidth = EditorGUIUtility.labelWidth;
        var width      = position.width;

        bool custom = type.stringValue == "Custom";

        if (id.stringValue == null)
        {
            id.stringValue = string.Empty;
        }

        EditorGUI.BeginProperty(position, label, property);

        bool customValid = custom && clips.arraySize > 0;

        if (customValid)
        {
            if (clips.arraySize == 0)
            {
                customValid = false;
            }
            else
            {
                var firstClipProp = clips.GetArrayElementAtIndex(0);
                var firstClip     = firstClipProp.objectReferenceValue as AudioClip;
                customValid = firstClip != null;
            }
        }
        bool soundValid = false;

        if (!custom)
        {
            if (clips.arraySize > 0)
            {
                clips.arraySize = 0;
                clips.serializedObject.ApplyModifiedProperties();
            }

            var soundInBase = !string.IsNullOrEmpty(id.stringValue) ? _manager.Base.Sounds.SingleOrDefault(s => s.Id == id.stringValue) : null;
            if (soundInBase == null)
            {
                soundInBase = _manager.Base.Sounds.SingleOrDefault(s => s.Type == type.stringValue);
            }

            if (soundInBase != null)
            {
                if (soundInBase.Id != id.stringValue)
                {
                    if (!string.IsNullOrEmpty(id.stringValue))
                    {
                        Debug.LogWarning("Sound Id changed from " + id.stringValue + " to " + soundInBase.Id, property.serializedObject.targetObject);
                    }
                    id.stringValue = soundInBase.Id;
                    id.serializedObject.ApplyModifiedProperties();
                }
                if (soundInBase.Type != type.stringValue)
                {
                    Debug.LogWarning("Sound Type changed from " + type.stringValue + " to " + soundInBase.Type, property.serializedObject.targetObject);
                    type.stringValue = soundInBase.Type;
                    type.serializedObject.ApplyModifiedProperties();
                }

                soundValid = true;
            }
        }
        bool valid = (custom && customValid) || soundValid;

        var colored = position;

        colored.y      += 5;
        colored.height -= 8;
        AudioManagerUtils.DrawColouredRect(colored, valid ? AudioManagerUtils.Blue : AudioManagerUtils.Red);


        position.y     += 10;
        position.height = 20;
        position.width  = labelWidth - 5;
        EditorGUI.LabelField(position, label);
        position.x += labelWidth;


        if (custom)
        {
            position.width = 20;
        }
        else
        {
            position.width = width - labelWidth - 5;
        }

        if (GUI.Button(position, custom ? "~" : type.stringValue, EditorStyles.toolbarButton))
        {
            AudioManagerEditor.DrawMenu(data =>
            {
                type.stringValue = (string)data;
                if (type.stringValue != "Custom")
                {
                    id.stringValue = _manager.Base.Sounds.Single(s => s.Type == type.stringValue).Id;
                }

                property.serializedObject.ApplyModifiedProperties();
            });
        }

        if (custom)
        {
            position.width  = width - (labelWidth + 25);
            position.x      = labelWidth + 40;
            position.height = 16;
            position.y     += 1;

            Object clip = null;
            if (clips.arraySize > 0)
            {
                clip = clips.GetArrayElementAtIndex(0).objectReferenceValue;
                if (clip == null)
                {
                    clips.arraySize = 0;

                    clips.serializedObject.ApplyModifiedProperties();
                }
            }

            var newClip = EditorGUI.ObjectField(position, clip, typeof(AudioClip), false);
            if (clip != newClip)
            {
                if (clips.arraySize < 1)
                {
                    clips.arraySize = 1;

                    clips.serializedObject.ApplyModifiedProperties();
                }
                clips.GetArrayElementAtIndex(0).objectReferenceValue = newClip;
            }
        }

        EditorGUI.EndProperty();

        if (property.depth == 0)
        {
            Sound sound = fieldInfo.GetValue(property.serializedObject.targetObject) as Sound;
            if (sound != null && clips.arraySize == 0 && sound.Clips != null)
            {
                sound.Clips = null;
                fieldInfo.SetValue(property.serializedObject.targetObject, sound);
                EditorUtility.SetDirty(property.serializedObject.targetObject);
            }
        }

        if (GUI.changed)
        {
            EditorUtility.SetDirty(property.serializedObject.targetObject);
            property.serializedObject.ApplyModifiedProperties();
        }
    }