UGUI Button组件音效扩展

AudioSource在场景上,clip在asset文件中 

 对Button组件进行封装

using UnityEditor;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class GButton: Button
{
    [SerializeField]
    private bool sound = true;
    [SerializeField]
    private AudioClip clickEff;

    [SerializeField]
    private AudioSource clickAudio;

    protected override void Awake()
    {
        base.Awake();
        this.clickEff = AssetDatabase.LoadAssetAtPath<AudioClip>("Assets/.../xx.mp3");
        
    }

    public override void OnPointerClick(PointerEventData eventData)
    {
        base.OnPointerClick(eventData);
        this.clickAudio = GameObject.Find("/.../SFX").GetComponent<AudioSource>();
        if (sound && clickEff != null)
        {
            this.clickAudio.PlayOneShot(clickEff);
        }
    }
}

自定义编辑器类

using UnityEditor;

[CustomEditor(typeof(GButton))]
public class GButtonEditor : UnityEditor.UI.ButtonEditor
{
    GButton gButton;

    protected override void OnEnable()
    {
        base.OnEnable();
        this.gButton = (GButton)target;
    }

    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
        EditorGUILayout.Space();
        serializedObject.Update();

        var prop_Sound = this.serializedObject.FindProperty("sound");
        EditorGUILayout.PropertyField(prop_Sound);
        if (prop_Sound.boolValue)
        {
            EditorGUILayout.PropertyField(this.serializedObject.FindProperty("clickEff"));
            EditorGUILayout.PropertyField(this.serializedObject.FindProperty("clickAudio"));
        }

        this.serializedObject.ApplyModifiedProperties();
    }
}