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();
    }

}

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

 

你可能感兴趣的:(C#,计算机相关基础,Define,Symbols,Unity宏定义)