1. 程式人生 > >Unity 程式碼改巨集定義

Unity 程式碼改巨集定義

 

兩個函式

PlayerSettings.GetScriptingDefineSymbolsForGroup(targetGroup); //所有巨集定義 ; 分割
PlayerSettings.SetScriptingDefineSymbolsForGroup(targetGroup, sz); //寫入全部巨集,相當於改配置

使用筆記

需要考慮一組巨集定義的互斥問題,要保持不改動的巨集不變,示例程式碼如下
public static void SetDebugerLevle(string logType)
{
    BuildTargetGroup targetGroup = BuildTargetGroup.Android;
    string ori = PlayerSettings.GetScriptingDefineSymbolsForGroup(targetGroup);
    string debugType = "Debuger_" + logType;

    List<string> defineSymbols = new List<string>(ori.Split(';'));
    for (int i = 0; i < defineSymbols.Count; ++i)
    {
        if (defineSymbols[i] == debugType)
        {
            Debug.LogFormat("========== debuglog {0}", logType);
            return;
        }

        if (defineSymbols[i].StartsWith("Debuger_"))
        {
            defineSymbols[i] = debugType;
            debugType = null;
            break;
        }
    }

    if (debugType != null)
    {
        defineSymbols.Add(debugType);
    }

    PlayerSettings.SetScriptingDefineSymbolsForGroup(targetGroup, string.Join(";", defineSymbols.ToArray()));
    Debug.LogFormat("========== debuglog {0}", logType);
}