Unity不同平台生成中预处理的注意点

简介: Unity3D的项目,这周吃亏在宏上了。大背景是项目需要在Unity中用Hudson自动生成不同平台的版本。 程序设计语言的预处理的概念:在编译之前进行的处理。

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中先调用

SwitchActiveBuildTarget (target : BuildTarget)函数。

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


    /// <summary>
    /// 删除单个文件的数组
    /// </summary>
    static void ClearISingleFileSeries()
    {
        string[] SingleFileSeries = { Application.dataPath + "/Plugins/I18N.dll", Application.dataPath + "/Plugins/I18N.CJK.dll", Application.dataPath + "/Plugins/I18N.West.dll" };
        ClearFiles(SingleFileSeries);
    }


    /// <summary>
    /// 删除filesPath数组内指向的文件
    /// </summary>
    ///  <param name="filesPath"></param>
    static void ClearFiles(string[] filesPath)
    {
        foreach (string singleFilePath in filesPath)
        {
            if (File.Exists(singleFilePath))
            {
                try
                {
                    File.Delete(singleFilePath);
                }
                catch (System.Exception ex)
                {
                    //catch ex
                }
            }
        }
        
    }


    /// <summary>
    /// 删除目前做Web版本会出现内存问题的Audio资源
    /// </summary>
    static void ClearDirectorySeries()
    {
        string[] audioPath = { Application.dataPath + "/Game/Audio/Resources", Application.dataPath + "/Game/Audio/SFX", Application.dataPath + "/Game/MyGUI" };
        foreach (string audioDirectory in audioPath)
        {
            if (Directory.Exists(audioDirectory))  //保护,避免文件目录不存在跳异常
                ClearFilesAndDirectory(audioDirectory);
        }
    }

        
    /// <summary>
    /// 删除dataPath文件目录下的所有子文件及子文件夹
    /// </summary>
    ///  <param name="DirectoryPath"></param>
    static void ClearFilesAndDirectory(string DirectoryPath)
    {
        DirectoryInfo dir = new DirectoryInfo(DirectoryPath);


        //文件
        foreach (FileInfo fChild in dir.GetFiles("*"))
        {
            if (fChild.Attributes != FileAttributes.Normal)
                fChild.Attributes = FileAttributes.Normal;
            fChild.Delete();
        }


        //文件夹
        foreach (DirectoryInfo dChild in dir.GetDirectories("*"))
        {
            if (dChild.Attributes != FileAttributes.Normal)
                dChild.Attributes = FileAttributes.Normal;
            ClearFilesAndDirectory(dChild.FullName);
            dChild.Delete();
        }

    }


转载自:http://blog.csdn.net/pandawuwyj/article/details/7959335

相关文章
|
1月前
|
存储 缓存 图形学
Unity3D学习笔记11——后处理
Unity3D学习笔记11——后处理
24 1
|
3月前
|
图形学
初识Unity——基本模型、场景操作、世界坐标系和局部坐标系
初识Unity——基本模型、场景操作、世界坐标系和局部坐标系
79 1
|
3月前
|
图形学
【unity小技巧】unity通过代码进行更改后处理效果
【unity小技巧】unity通过代码进行更改后处理效果
32 0
|
人工智能 API 图形学
unity基础脚本代码总结
unity基础脚本代码总结
168 0
|
数据可视化 vr&ar 图形学
Unity可视化编程XDreamer插件导入
前言 XDreamer是一款基于Unity平台开发的,可在Unity(包括编辑器与运行时)中使用的可扩展的中文交互编辑软件,可进行2D、3D、VR、AR、MR开发。 本期博客为XDreamer的官方讲解的学习记录。可以理解为UE4中的蓝图效果。是从事美术人员的福音,美术人员也可不用编写程序进行游戏的制作。 一、下载XDreamer官方插件包 XDreamer中文交互编辑器http://www.xdreamer.com.cn/请在官网进行下载,得到如下的文件。 二、插件加载 目前我导入到URP
576 0
Unity可视化编程XDreamer插件导入
|
C语言 计算机视觉 C++
ffmpeg 纯静态编译,以及添加自定义库流程摘要
需求:    1. 纯静态编译ffmpeg ,即ldd ./ffmpeg 的结果是:not a dynamic executable    2.  修改ffmpeg 项目,添加自定义功能库    3. 自定义库由c++实现,要求能被纯c的ffmpeg项目调用    4. 自定义库必须使用g++ 的一些高级特性编译,要求g++支持c++11    5. 自定义库使用了pthread库 和openmp 库    6. 自定义库使用了opencv 3.0.0库,    7. 禁用所有的图形显示库x11,xcb,声音设备avdevice等等,静态链接这些库,会很痛苦。
4888 0
|
搜索推荐 Linux C#
Unity基础
Unity是什么,Unity是一个游戏开发引擎,他功能强大,学习简单,炉石传说,王者荣耀等游戏就是利用Unity引擎开发出来的
294 0
Unity基础
|
图形学 异构计算
Unity全面优化
Unity的项目优化已经是老生常谈,很多人在项目完成之后,即便创意新颖,也会觉得差强人意,原因就在于没有做详细的项目优化。众所周知,Unity是一个综合性的3D开发引擎,其中包含图像渲染,逻辑处理,数据存储,发布测试等等各方面的内容。
972 0
|
图形学 Android开发
基于Unity3d 引擎的Android游戏优化(续)
VSync Count 垂直同步 中新建一个场景空的时候,帧速率(FPS总是很低),大概在60~70之间。一直不太明白是怎么回事,现在基本上明白了。我在这里解释一下原因,如有错误,欢迎指正。在Unity3D中当运行场景打开Profiler的时候,我们会看到VSync 这一项占了很大的比重。
1464 0
|
Java C# Android开发
基于Unity3d 引擎的Android游戏优化
更新不透明贴图的压缩格式为ETC 4bit,因为android市场的手机中的GPU有多种,每家的GPU支持不同的压缩格式,但他们都兼容ETC格式, 对于透明贴图,我们只能选择RGBA 16bit 或者RGBA 32bit。
1931 0