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

简介:

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





















本文转蓬莱仙羽 51CTO博客,原文链接:http://blog.51cto.com/dingxiaowei/1366108,如需转载请自行联系原作者

相关文章
|
8月前
|
数据采集 开发工具 图形学
Windows平台实现Unity下窗体|摄像头|屏幕采集推送
随着Unity3D的应用范围越来越广,越来越多的行业开始基于Unity3D开发产品,如传统行业中虚拟仿真教育、航空工业、室内设计、城市规划、工业仿真等领域。
|
1天前
|
数据采集 编解码 图形学
Android平台Unity下如何通过WebCamTexture采集摄像头数据并推送至RTMP服务器或轻量级RTSP服务
Android平台Unity下如何通过WebCamTexture采集摄像头数据并推送至RTMP服务器或轻量级RTSP服务
118 0
|
8月前
|
编解码 监控 图形学
Windows平台Unity下播放RTSP或RTMP如何开启硬解码?
我们在做Windows平台Unity播放RTMP或RTSP的时候,遇到这样的问题,比如展会、安防监控等场景下,需要同时播放多路RTMP或RTSP流,这样对设备性能,提出来更高的要求。
|
8月前
|
图形学 开发者 Windows
Unity平台如何实现RTSP转RTMP推送?
Unity平台下,RTSP、RTMP播放和RTMP推送,甚至包括轻量级RTSP服务这块都不再赘述,今天探讨的一位开发者提到的问题,如果在Unity下,实现RTSP播放的同时,随时转RTMP推送出去?
|
8月前
|
数据采集 vr&ar 图形学
Windows平台Unity Camera场景实现轻量级RTSP服务和RTMP推送
随着VR技术在医疗、军事、农业、学校、景区、消防、公共安全、研学机构、展厅展馆,商场等场所普及,开发者对Unity平台下的直播体验提出了更高的要求。
|
8月前
|
数据采集 编解码 vr&ar
Android平台实现VR头显Unity下音视频数据RTMP推送
随着技术发展的日新月异,虚拟现实产业已经从过去的探索期,自2020年起,慢慢过渡到高速发展期,随着5G时代的到来,大带宽高可靠低延迟网络环境,为虚拟现实产业提供了很好的网络保障,虚拟现实在越来越多的场景下有了应用价值,典型场景如工业互联网、虚拟仿真、文旅文博、智慧交通、智慧能源、智慧医疗、智慧校园、智慧农业等。同事,行业也对清晰度、流畅性和交互感也提出了更高的要求。本文从Android平台的采集推送为例,介绍下基于头显或类似终端的低延迟解决方案。
|
8月前
|
数据采集 物联网 Linux
Unity3D下实现Linux平台RTMP推流(以采集Unity窗体和声音为例)
随着物联网等行业的崛起,越来越多的传统行业如虚拟仿真、航天工业、工业仿真、城市规划等,对Linux下的生态构建,有了更大的期望,Linux平台下,可选的直播推拉流解决方案相对Windows和移动端,非常少,基于Unity的Linux推送方案,更是几无参考。本文以Unity3d环境下Linux平台推送Unity窗体和Unity采集的音频,然后编码推送到RTMP服务器为例,大概说下实现过程。
142 0
|
8月前
|
编解码 图形学 Android开发
Unity3D平台实现全景实时RTMP|RTSP流渲染
好多开发者的使用场景,需要在Windows特别是Android平台实现Unity3D的全景实时视频渲染,本文以Windows平台为例,简单介绍下具体实现: 如果是RTSP或RTMP流数据,实际上难点,主要在于拉取RTSP或RTMP流,解析解码,然后把解码后的YUV数据,回调到Unity层,Unity创建个Sphere,创建个材质球(Material),并把材质球挂在到Sphere即可。
138 0
|
8月前
|
Linux C# 图形学
Unity3D下Linux平台播放RTSP或RTMP流
尽管Windows平台有诸多优势,Linux平台的发展还是势不可挡,特别实在传统行业,然而Linux生态构建,总是差点意思,特别是有些常用的组件,本文基于已有的Linux平台RTSP、RTMP播放模块,构建Unity下的RTSP和RTMP直播播放。
|
8月前
|
开发工具 图形学 Android开发
如何在Unity3d平台下低延迟播放RTMP或RTSP流
随着VR类、工业仿真、智慧城市等场景的快速发展,开发者对Unity3d低延迟的直播需求量越来越大,前两年,大牛直播SDK发布了Windows平台、Android平台和iOS平台的Unity3d RTMP和RTSP的播放,好多公司用起来体验都非常好,以下介绍大概实现流程。
115 0