Unity3D的项目,这周吃亏在宏上了。大背景是项目需要在Unity中用Hudson自动生成不同平台的版本。
程序设计语言的预处理的概念:在编译之前进行的处理。
#if UNITY_WEBPLAYER
BuildTarget target = BuildTarget.WebPlayer;
#elif UNITY_STANDALONE_WIN && UNITY_EDITOR
BuildTarget target = BuildTarget.StandaloneWindows;
#elif UNITY_ANDROID
BuildTarget target = BuildTarget.Android;
#else
BuildTarget target = BuildTarget.iPhone;
#endif
#if UNITY_WEBPLAYER
public const string AssetRootPath = AutomaticBuild.WebPlatFormDataPath + "/";
#elif UNITY_STANDALONE_WIN && UNITY_EDITOR
public const string AssetRootPath = AutomaticBuild.WinPlatFormDataPath + "/";
#elif UNITY_ANDROID
public const string AssetRootPath = AutomaticBuild.AndRoidPlatFormDataPath + "/";
#else
public const string AssetRootPath = AutomaticBuild.IOSPlatFormDataPath + "/";
#endif
如上面两段代码,打开Unity项目(例如PC & Mac Standalone保存的)之后,再打开项目的Script(这里用VS2008+VA),会发现上述加粗行高亮。即Target和AssetRootPath在编译前已然确定,且之后不能对其做出变更。
当采用Unity支持的命令编译时C:\program files\Unity\Editor>Unity.exe -quit -batchmode -executeMethod MyEditorScript.MyMethod
此时MyMethod可能用了如下代码,
BuildPipeline.BuildPlayer( levels, "WebPlayerBuild", BuildTarget.WebPlayer, BuildOptions.None);
但Target和AssetRootPath并没有赋予应有的Web相应值,会造成生成的Unity3D文件能生成但不对,执行BuildPlayer时会报Runtime Error错。
不禁让我想起Effective C++里的第2个条款:尽量以const, enum, inline替换 #define。果然金科玉律……
我的解决方式如下:
1.在MyMethod中先调用
private static string AssetRootPath = null;
public static string GetAssetRootPath()
{
if (AssetRootPath != null)
return AssetRootPath;
if (EditorUserBuildSettings.activeBuildTarget==BuildTarget.WebPlayer)
{
AssetRootPath = "WebData/LatestData/";
}
else if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.Android)
{
AssetRootPath = "AndroidData/LatestData/";
}
else if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.StandaloneWindows)
{
AssetRootPath = "WinData/LatestData/";
}
else
{
AssetRootPath = "IOSData/LatestData/";
}
return AssetRootPath;
}
public static BuildTarget GetBuildTarget()
{
return EditorUserBuildSettings.activeBuildTarget;
}
由于需求的小变更,小小地重构了上次的代码:
ClearISingleFileSeries();}
转载自:http://blog.csdn.net/pandawuwyj/article/details/7959335
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。