可以在player setting里面定义宏
也可以通过代码定义
PlayerSettings. SetScriptingDefineSymbolsForGroup ( BuildTargetGroup. Standalone, item. Symbol) ;
官方解释
我以前一直以为定义一个符号之后,需要进行判断是否存在这个符号什么的。但是没有必要。 C#指令详细解释
使用来多渠道打包sdk
这个很好用的一点是可以来多渠道打包sdk 给不同的渠道定义不同的symbol 把和这些sdk相关的接入性质代码全部带上预编译指令 在自动化打包的时候,定义这些symbol,输出不同的包出来 但是我还有个问题,就是说,怎么把不同的文件区分出来。 就是说oppo需要依赖oppo.dll,华为需要依赖huawei.dll 怎么在打不同种类包的时候把这些文件给剔除掉。
[ CreateAssetMenu ( order = 12 , menuName = "创建渠道设定" ) ]
public class ChannelSetting : ScriptableObject
{
public string Channel;
public string Symbol;
}
[ MenuItem ( "Build/BuildAll" ) ]
static void BuildAllPlatform ( )
{
var settings = Resources. LoadAll < ChannelSetting> ( "ChannelSettings" ) ;
foreach ( var item in settings)
{
PlayerSettings. SetScriptingDefineSymbolsForGroup ( BuildTargetGroup. Standalone, item. Symbol) ;
var path = Path. Combine ( System. Environment. CurrentDirectory, item. Channel, $" { item. Channel } .exe" ) ;
BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions ( ) ;
buildPlayerOptions. locationPathName = path;
buildPlayerOptions. target = BuildTarget. StandaloneWindows;
buildPlayerOptions. targetGroup = BuildTargetGroup. Standalone;
var res = BuildPipeline. BuildPlayer ( buildPlayerOptions) ;
}
}
比较坑的一点是,打包时候需要指定输出文件夹,我一直以为是要指定文件夹,结果一直 报错UnityException: Build path contains project built with "Create Visual Studio Solution" option, which is incompatible with current build settings. Consider building your project into an empty directory.
其实是还需要加上文件的名字