美文网首页
Unity中,在工程内所有预制体中,找到带Btn组件的节点,为其

Unity中,在工程内所有预制体中,找到带Btn组件的节点,为其

作者: 全新的饭 | 来源:发表于2024-06-02 15:18 被阅读0次

    说明

    放在Editor目录下。

    播放音效相关

    播放音效的组件:UIClickBtnAudioCtr.cs
    在该文档中可查看。
    Unity中,按钮按下抬起的缩放效果

    音乐音效系统
    Unity中,基本的音乐音效控制系统(改良)

    代码

    AddBtnSoundCtr.cs

    using System.Collections.Generic;
    using UnityEngine;
    using UnityEditor;
    using System;
    using UnityEngine.UI;
    
    public static class AddBtnSoundCtr
    {
        [MenuItem("Tools/向Btn预制体添加音效控制组件")]
        private static void Create()
        {
            var btnPrefabs = GetPrefabs(go => go.GetComponentInChildren<Button>(true) != null);
            foreach (var prefab in btnPrefabs)
            {
                ModifyPrefab(prefab, go =>
                {
                    var hasModified = false;
                    var btns = go.GetComponentsInChildren<Button>(true);
                    foreach (var btn in btns)
                    {
                        if (btn.GetComponent<UIClickBtnAudioCtr>() == null)
                        {
                            btn.GetOrAddComponent<UIClickBtnAudioCtr>();
                            if (!hasModified)
                            {
                                hasModified = true;
                            }
                        }
                    }
                    if (hasModified)
                    {
                        Debug.Log("修改了预制体:" + go.name);
                    }
                });
            }
    
            List<GameObject> GetPrefabs(Func<GameObject, bool> filter)
            {
                List<GameObject> prefabs = new List<GameObject>();
                var resourcesPath = Application.dataPath;
                var absolutePaths = System.IO.Directory.GetFiles(resourcesPath, "*.prefab", System.IO.SearchOption.AllDirectories);
                for (int i = 0; i < absolutePaths.Length; i++)
                {
                    EditorUtility.DisplayProgressBar("获取预制体……", "获取预制体中……", (float)i / absolutePaths.Length);
    
                    string path = "Assets" + absolutePaths[i].Remove(0, resourcesPath.Length);
                    path = path.Replace("\\", "/");
                    GameObject prefab = AssetDatabase.LoadAssetAtPath(path, typeof(GameObject)) as GameObject;
                    if (prefab != null && filter(prefab))
                    {
                        prefabs.Add(prefab);
                    }
                }
                EditorUtility.ClearProgressBar();
                return prefabs;
            }
            void ModifyPrefab(GameObject prefab, Action<GameObject> modify)
            {
                var prefabPath = PrefabToPath(prefab);
                var instance = PrefabUtility.LoadPrefabContents(prefabPath);
                modify(instance);
                PrefabUtility.SaveAsPrefabAsset(instance, prefabPath);
                PrefabUtility.UnloadPrefabContents(instance);
            }
            string PrefabToPath(GameObject prefab)
            {
                string assetPath = AssetDatabase.GetAssetPath(prefab);
                return assetPath;
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:Unity中,在工程内所有预制体中,找到带Btn组件的节点,为其

          本文链接:https://www.haomeiwen.com/subject/whrsqjtx.html