chenxin
2020-12-05 fa584507591723218b0a6f99ab1d446490e9efa0
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
using UnityEditor;
using UnityEngine;
 
namespace ActionGameFramework.Audio.Editor
{
    /// <summary>
    /// Custom editor for <see cref="HealthChangeAudioSource"/> that sorts sounds on the fly
    /// </summary>
    [CustomEditor(typeof(HealthChangeAudioSource))]
    public class HealthChangeAudioSourceEditor : UnityEditor.Editor
    {
        protected const string k_HelpMessage =
            "This list needs to be sorted in order " +
            "for sounds to be played correctly" +
            "\nList will sort automatically when this component is deselected." +
            "\nYou can also press the \'Sort\' button once you are done editing the sound list.";
 
        /// <summary>
        /// The <see cref="HealthChangeAudioSource"/> that is selected
        /// </summary>
        protected HealthChangeAudioSource m_Source;
 
        /// <summary>
        /// Sort the sounds when the <see cref="HealthChangeAudioSource"/> is selected
        /// </summary>
        protected void OnEnable()
        {
            m_Source = target as HealthChangeAudioSource;
        }
 
        /// <summary>
        /// Sort the sounds when <see cref="HealthChangeAudioSource"/> is deselected
        /// </summary>
        protected void OnDisable()
        {
            Sort();
        }
 
        /// <summary>
        /// Sort the <see cref="HealthChangeAudioSource"/>'s sound list
        /// </summary>
        protected void Sort()
        {
            if (m_Source != null)
            {
                m_Source.Sort();
                EditorUtility.SetDirty(m_Source);
            }
        }
 
        /// <summary>
        /// Provide a button to manually sort sounds that were edited
        /// </summary>
        public override void OnInspectorGUI()
        {
            EditorGUILayout.HelpBox(k_HelpMessage, MessageType.Info);
            base.OnInspectorGUI();
            if (GUILayout.Button("Sort"))
            {
                Sort();
            }
        }
    }
}