Unity 一键给所有按钮添加按钮音效

UGUI使用,NGUI需要稍微修改一点

1.按钮音效脚本

//=====================================================
// - FileName:      SoundButtonTool.cs
// - Author:       Autumn
// - CreateTime:    2019/05/28 10:51:42
// - Email:         [email protected]
// - Description:   
// -  (C) Copyright 2019, webeye,Inc.
// -  All Rights Reserved.
//======================================================
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Qarth;

namespace GameWish.Game
{
    public enum ButtonClickSound
    {
        Normal,
        Close,
    }

    [RequireComponent(typeof(Button))]
    public class SoundButtonTool : MonoBehaviour
    {
        public ButtonClickSound clickSound = ButtonClickSound.Normal;
        void ClickSound()
        {
            switch (clickSound)
            {
                case ButtonClickSound.Normal:
                    AudioMgr.S.PlaySound(GameSound.EFFECT_CLICK_BTN);
                    break;
                case ButtonClickSound.Close:
                    AudioMgr.S.PlaySound(GameSound.EFFECT_CLOSE_BTN);
                    break;
            }
        }
        private void Awake()
        {
            Button btn = GetComponent

2.Editor工具类

//=====================================================
// - FileName:      AddButtonClickSound.cs
// - Author:       Autumn
// - CreateTime:    2019/05/28 10:59:23
// - Email:         [email protected]
// - Description:   
// -  (C) Copyright 2019, webeye,Inc.
// -  All Rights Reserved.
//======================================================
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Qarth;
using UnityEditor;
using UnityEngine.EventSystems;

namespace GameWish.Game 
{
    public class AddButtonClickSound : ScriptableObject
    {
        [MenuItem("Tools/ButtonAudio/AddButtonSoundInScene")]
        static void AddSoundForButton() 
        {
            GetAllSelectGo((child)=>
            {
                if (child.GetComponent

里面用到一些框架的功能,其实可以用你们自己代替 比如AddMissingComponent,Log.i其实就是Debug.Log,AudioMgr这些用自己的方法替代就好了。多余的引用自行删除即可。

你可能感兴趣的:(Unity)