Unity通过代码修改Scripting Define Symbols(宏定义)

目前 我们项目使用XLua进行ios代码的热更,在PC上测试Lua代码是否正确,是通过修改宏定义来修改C#代码的执行,从而读取Lua热更代码。每次添加删除宏定义都要手动: File ->PlayerSettings去修改,又记不住定义的字符串。本人比较懒,觉得每次去输入很麻烦,所以想通过代码修改该宏定义。

然后就有了如下代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System;

public class XLuaExample : EditorWindow
{
    private static string def = "HOTFIX_ENABLE";
    [MenuItem("XLua/添加宏定义")]
    static void AddXlua()
    {
        string xlua = PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone);
        if (xlua.Equals(string.Empty))
        {
            PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone, def);
        }
        else if (!xlua.Contains(def))
        {
            PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone, xlua + ";" + def);
        }
        AssetDatabase.Refresh();
    }

    [MenuItem("XLua/删除宏定义")]
    static void RemoveXlua()
    {
        string xlua = PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone);
        int _index = xlua.IndexOf(def);
        if (_index<0)
        {
            return;
        }
        if (_index > 0)//如果不在第一个  把前边的分号删掉
        {
            _index -= 1;
        }
        int _length = def.Length;
        if (xlua.Length > _length)//如果长度大于当前长度,才有分号
        {
            _length += 1;
        }
        xlua = xlua.Remove(_index, _length);
        PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone, xlua);
        AssetDatabase.Refresh();
    }

}

测试了几下,通过两个菜单按钮,可以修改该宏定义,只是现在宏定义是固定字符串,也可以修改为窗口,传宏定义的参数。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值