Unity方便修改产品名和包名的小工具

csharp 复制代码
using UnityEditor;
using UnityEngine;
using System.Collections.Generic;

public class BundleIdentifierEditor : EditorWindow
{
    // 存储选项数据
    private class Option
    {
        public string label;
        public string productName;
        public string bundleIdentifierSuffix; // 仅存储后缀部分
    }

    private const string DefaultPrefix = "com.HappyMaster."; // 固定前缀部分

    // 存储所有选项
    private List<Option> options = new List<Option>();

    // 当前选中的选项
    private Option selectedOption;

    // 初始化窗口
    [MenuItem("Tools/Bundle Identifier Editor")]
    public static void ShowWindow()
    {
        GetWindow<BundleIdentifierEditor>("Bundle Identifier Editor");
    }

    private void OnEnable()
    {
        // 默认选项(可以修改或删除)
        options.Add(new Option { label = "Option 1", productName = "影片播控", bundleIdentifierSuffix = "VideoControl" });
        options.Add(new Option { label = "Option 2", productName = "影片服务端", bundleIdentifierSuffix = "VideoServer" });

        // 默认选中第一个选项
        selectedOption = options[0];
    }

    private void OnGUI()
    {
        GUILayout.Label("选择并修改产品名称和捆绑标识符", EditorStyles.boldLabel);

        // 创建可选的选项
        for (int i = 0; i < options.Count; i++)
        {
            Option option = options[i];
            bool isSelected = selectedOption == option;
            if (GUILayout.Toggle(isSelected, option.label))
            {
                selectedOption = option;
            }
        }

        // 显示当前选中的选项的产品名和捆绑标识符
        if (selectedOption != null)
        {
            GUILayout.Space(10);
            GUILayout.Label("当前选项: " + selectedOption.label, EditorStyles.boldLabel);
            selectedOption.productName = EditorGUILayout.TextField("Product Name", selectedOption.productName);
            selectedOption.bundleIdentifierSuffix = EditorGUILayout.TextField("Bundle Identifier Suffix", selectedOption.bundleIdentifierSuffix);

            // 应用按钮
            if (GUILayout.Button("Apply Changes"))
            {
                ApplyChanges();
            }
        }

        // 添加新选项按钮
        GUILayout.Space(10);
        if (GUILayout.Button("Add New Option"))
        {
            AddNewOption();
        }
    }

    private void ApplyChanges()
    {
        if (selectedOption != null)
        {
            // 拼接默认前缀和用户提供的后缀
            string bundleIdentifier = DefaultPrefix + selectedOption.bundleIdentifierSuffix;

            // 应用到 PlayerSettings
            PlayerSettings.productName = selectedOption.productName;
            PlayerSettings.applicationIdentifier = bundleIdentifier;

            Debug.Log($"Product Name set to: {selectedOption.productName}");
            Debug.Log($"Bundle Identifier set to: {bundleIdentifier}");
        }
    }

    private void AddNewOption()
    {
        // 新建一个选项,使用默认名称和标识符
        Option newOption = new Option
        {
            label = "Option " + (options.Count + 1),
            productName = "NewProduct",
            bundleIdentifierSuffix = "NewProduct"
        };

        // 添加到选项列表
        options.Add(newOption);
    }
}
相关推荐
孟无岐3 小时前
【Laya】HttpRequest 网络请求
网络·typescript·游戏引擎·游戏程序·laya
JIes__14 小时前
Unity(二)——MonoBehavior中的重要内容
unity·游戏引擎
孟无岐20 小时前
【Laya】LocalStorage 本地存储
typescript·游戏引擎·游戏程序·laya
妙为1 天前
unreal engine5角色把敌人 “挤飞”
游戏引擎·虚幻·ue·unrealengine5
4Forsee1 天前
【增强现实】快速上手 Vuforia Unity Android AR 应用开发
android·unity·ar
两水先木示1 天前
【Unity】对指定物体进行描边——模板测试法
unity·游戏引擎·shader·外描边
Miss_SQ1 天前
实现Unity录音、百度云语音转文字
unity·语音识别
CreasyChan1 天前
unity 对象池实测可用
unity·c#
weixin_424294671 天前
Unity项目的Artifacts文件夹过大怎么解决?
unity·游戏引擎