http://blog.csdn.net/pandawuwyj/article/details/7959335

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

Unity不同平台生成中预处理的注意点的更多相关文章

  1. C# Unity游戏开发——Excel中的数据是如何到游戏中的 (四)2018.4.3更新

    本帖是延续的:C# Unity游戏开发--Excel中的数据是如何到游戏中的 (三) 最近项目不算太忙,终于有时间更新博客了.关于数据处理这个主题前面的(一)(二)(三)基本上算是一个完整的静态数据处 ...

  2. 深度学习在美团点评推荐平台排序中的应用&& wide&&deep推荐系统模型--学习笔记

    写在前面:据说下周就要xxxxxxxx, 吓得本宝宝赶紧找些广告的东西看看 gbdt+lr的模型之前是知道怎么搞的,dnn+lr的模型也是知道的,但是都没有试验过 深度学习在美团点评推荐平台排序中的运 ...

  3. C# Unity游戏开发——Excel中的数据是如何到游戏中的 (二)

    本帖是延续的:C# Unity游戏开发——Excel中的数据是如何到游戏中的 (一) 上个帖子主要是讲了如何读取Excel,本帖主要是讲述读取的Excel数据是如何序列化成二进制的,考虑到现在在手游中 ...

  4. C# Unity游戏开发——Excel中的数据是如何到游戏中的 (三)

    本帖是延续的:C# Unity游戏开发——Excel中的数据是如何到游戏中的 (二) 前几天有点事情所以没有继续更新,今天我们接着说.上个帖子中我们看到已经把Excel数据生成了.bin的文件,不过其 ...

  5. VS2008 工程中部分文件不参与编译 从生成中排除【Worldsing笔记】

    Visual Studio 2008 .VS2008.VC2008工程源文件配置.编译配置   有时编写代码时,往往存在这样的需求(或是希望有这样的功能):一个工程经过不共同的配置实现不同的版本或是功 ...

  6. Unity 查找泛型List中的相同与不同数据

    Unity查找泛型集合中的不同数据 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- ...

  7. 关于如何在 Unity 的 UI 菜单中默认创建出的控件 Raycast Target 属性默认为 false

    关于如何在 Unity 的 UI 菜单中默认创建出的控件 Raycast Target 属性默认为 false 我们在 Unity 中通过 UI 菜单创建的各种控件,比如 Text, Image 等, ...

  8. 文献综述二:UML技术在行业资源平台系统建模中的应用

    一.基本信息 标题:UML技术在行业资源平台系统建模中的应用 时间:2015 出版源:Hans汉斯 文件分类:uml技术的应用 二.研究背景 为方便行业人员高效率地搜集专业知识,实现知识的共享.采用计 ...

  9. php如何实现把多平台文件中所有的行合成一行?

    php如何实现把多平台文件中所有的行合成一行? 一.总结 1.str_replace中的数组替换:str_replace(array("/r","/n",&qu ...

随机推荐

  1. Ubuntu上安装ns2-2.34

    Ubuntu上安装ns2-2.34 步骤1 下载ns-allinone-2.34 $ tar zxf ns-allinone-2.34.tar.gz 步骤2 sudo apt-get install ...

  2. Shell 判断

    1  shell 的$! ,$?, $$,$@ $n        $1 the first parameter,$2 the second... $#        The number of co ...

  3. 如何在C#中生成与PHP一样的MD5 Hash Code

    最近在对一个现有的系统进行C#改造,该系统以前是用PHP做的,后台的管理员登陆用的是MD5加密算法.在PHP中,要对一个字符串进行MD5加密非常简单,一行代码即可: md5("Somethi ...

  4. C语言实现二叉树-01版

    故事是这样开始的,项目经理有一天终于还是拍拍我肩膀说: 无论你的链表写得多么的好,无论是多么的灵活,我也得费老半天才查找到想要的数据: 这让我的工作非常苦恼,听说有一种叫做二叉树的数据结构,你看能不能 ...

  5. paip.java swt 乱码问题解决

    paip.java swt 乱码问题解决 看累挂,Dfile.encoding是gbk的.. 作者Attilax  艾龙,  EMAIL:1466519819@qq.com  来源:attilax的专 ...

  6. php读取大文件的方法

    1.使用file 函数直接读取 $starttime = microtime_float(); ini_set('memory_limit','-1'); $file = "testfile ...

  7. Java httpclient请求,解决乱码问题

    public class HttpPostRequestUtil { public HttpPostRequestUtil() { } public static String post(String ...

  8. Passwordless SSH Login

    原文地址:http://manjeetdahiya.com/2011/03/03/passwordless-ssh-login/ Consider two machines A and B. We w ...

  9. android获取本机的IP地址和mac物理地址

    /获取本机IP地址 public String getLocalIpAddress() { WifiManager wifiManager = (WifiManager) getSystemServi ...

  10. Scala 具体的并行集合库【翻译】

    原文地址 本文内容 并行数组(Parallel Array) 并行向量(Parallel Vector) 并行范围(Parallel Range) 并行哈希表(Parallel Hash Tables ...