位置: 编程技术 - 正文
推荐整理分享Unity3d开发之 多渠道批量打包(四)(unity多开同一个项目的方法),希望有所帮助,仅作参考,欢迎阅读内容。
文章相关热门搜索词:unity多pass,unity多选,unity多选,unity多开同一个项目的方法,unity3d dots,unity多场景ui,unity怎么多开,unity多平台,内容如对您有帮助,希望把文章链接给更多的朋友!
原文地址: 。
Unity默认提供了一些预定义标签如:
UNITY_EDITOR : 编辑器模式下。
UNITY_STANDALONE:PC Mac Linux模型下。
UNITY_IPHONE:IOS模式下。
UNITY_ANDROID:ANDROID模式下。
还有很多预定义标签、大家可以在这里看到:
官方提供的标签属于比较大的范围标签,比如我希望在UNITY_ANDROID下面在写一些自定义的标签、类 QQ UC CMCC这样的渠道标签该如何呢?如下图所示,ProjectSetting打包界面每个平台都会有Scripting Define Symbols这个选项,可以在这里添加每个平台下对应的自定义标签(多个标签用“;”号隔开),这里我设置的是Android平台,如果IOS也需要打这样的渠道标签那么也要在IOS页面Scripting Define Symbols选项处添加对应的标签。
标签做出来了就好办了,然后在代码中我们可以这样来写。如果在Scripting Define Symbols中没有出现的标签默认是不启用的,就像 #define Test 一样,会自动被注释掉。
然后我们看看如何实现脚本批量打包。
usingUnityEngine;usingUnityEditor;usingSystem.Collections;usingSystem.Collections.Generic;usingSystem;usingSystem.IO; publicclassMyEditorScript { //得到工程中所有场景名称 staticstring[]SCENES=FindEnabledEditorScenes(); //一系列批量build的操作 [MenuItem("Custom/Build Android QQ")] staticvoidPerformAndroidQQBuild() { BulidTarget("QQ","Android"); } [MenuItem("Custom/Build Android UC")] staticvoidPerformAndroidUCBuild() { BulidTarget("UC","Android"); } [MenuItem("Custom/Build Android CMCC")] staticvoidPerformAndroidCMCCBuild() { BulidTarget("CMCC","Android"); } [MenuItem("Custom/Build Android ALL")] staticvoidPerformAndroidALLBuild() { BulidTarget("QQ","Android"); BulidTarget("UC","Android"); BulidTarget("CMCC","Android"); } [MenuItem("Custom/Build iPhone QQ")] staticvoidPerformiPhoneQQBuild() { BulidTarget("QQ","IOS"); } [MenuItem("Custom/Build iPhone QQ")] staticvoidPerformiPhoneUCBuild() { BulidTarget("UC","IOS"); } [MenuItem("Custom/Build iPhone CMCC")] staticvoidPerformiPhoneCMCCBuild() { BulidTarget("CMCC","IOS"); } [MenuItem("Custom/Build iPhone ALL")] staticvoidPerformiPhoneALLBuild() { BulidTarget("QQ","IOS"); BulidTarget("UC","IOS"); BulidTarget("CMCC","IOS"); } //这里封装了一个简单的通用方法。staticvoidBulidTarget(stringname,stringtarget){ stringapp_name=name; stringtarget_dir=Application.dataPath"/TargetAndroid"; stringtarget_name=app_name".apk"; BuildTargetGrouptargetGroup=BuildTargetGroup.Android; BuildTargetbuildTarget=BuildTarget.Android; stringapplicationPath=Application.dataPath.Replace("/Assets",""); if(target=="Android"){target_dir=applicationPath"/TargetAndroid";target_name=app_name".apk"; targetGroup=BuildTargetGroup.Android;}if(target=="IOS"){target_dir=applicationPath"/TargetIOS";target_name=app_name;targetGroup=BuildTargetGroup.iPhone;buildTarget=BuildTarget.iPhone;} //每次build删除之前的残留if(Directory.Exists(target_dir)) { if(File.Exists(target_name)) { File.Delete(target_name); } }else{Directory.CreateDirectory(target_dir);} //==================这里是比较重要的东西=======================switch(name){case"QQ": PlayerSettings.bundleIdentifier="com.game.qq";PlayerSettings.bundleVersion="v0.0.1"; PlayerSettings.SetScriptingDefineSymbolsForGroup(targetGroup,"QQ"); break;case"UC": PlayerSettings.bundleIdentifier="com.game.uc";PlayerSettings.bundleVersion="v0.0.1"; PlayerSettings.SetScriptingDefineSymbolsForGroup(targetGroup,"UC"); break;case"CMCC": PlayerSettings.bundleIdentifier="com.game.cmcc";PlayerSettings.bundleVersion="v0.0.1"; PlayerSettings.SetScriptingDefineSymbolsForGroup(targetGroup,"CMCC"); break;} //==================这里是比较重要的东西======================= //开始Build场景,等待吧~ GenericBuild(SCENES,target_dir"/"target_name,buildTarget,BuildOptions.None); } privatestaticstring[]FindEnabledEditorScenes(){List<string>EditorScenes=newList<string>();foreach(EditorBuildSettingsScenesceneinEditorBuildSettings.scenes){if(!scene.enabled)continue;EditorScenes.Add(scene.path);}returnEditorScenes.ToArray();} staticvoidGenericBuild(string[]scenes,stringtarget_dir,BuildTargetbuild_target,BuildOptionsbuild_options) { EditorUserBuildSettings.SwitchActiveBuildTarget(build_target); stringres=BuildPipeline.BuildPlayer(scenes,target_dir,build_target,build_options); if(res.Length>0){ thrownewException("BuildPlayer failure: "res); } } }
这里面忘说了一点,如果我们希望在性能高的手机上用一套好的资源,在性能低的手机上用一套差一点的资源该怎么办?那么首先我们先搞清楚Unity会把什么资源打包,什么资源不打包?
1.Resources文件夹
Resources文件夹是一个只读的文件夹,通过Resources.Load()来读取对象。因为这个文件夹下的所有资源都可以运行时来加载,所以Resources文件夹下的所有东西都会被无条件的打到发布包中。建议这个文件夹下只放Prefab或者一些Object对象,因为Prefab会自动过滤掉对象上不需要的资源。举个例子我把模型文件还有贴图文件都放在了Resources文件夹下,但是我有两张贴图是没有在模型上用的,那么此时这两张没用的贴图也会被打包到发布包中。假如这里我用Prefab,那么Prefab会自动过滤到这两张不被用的贴图,这样发布包就会小一些了。
2.StreamingAssets
StreamingAssets文件夹也是一个只读的文件夹,但是它和Resources有点区别,Resources文件夹下的资源会进行一次压缩,而且也会加密,不使用点特殊办法是拿不到原始资源的。但是StreamingAssets文件夹就不一样了,它下面的所有资源不会被加密,然后是原封不动的打包到发布包中,这样很容易就拿到里面的文件。所以StreamingAssets适合放一些二进制文件,而Resources更适合放一些GameObject和Object文件。StreamingAssets 只能用过www类来读取!!
3. 最后凡是在Hierarchy视图对象引用过的资源文件也会被无条件打包到发布包中。如果有一部分文件可能没有在Resources文件夹下也没有在StreamingAssets文件夹下,也没有被Hierarchy视图游戏对象引用,那么这类资源是不会被打包到发布包中的。
OK!搞清楚这一点就好办了!在处理不同包对应不同资源包的时候,尽量让可配置的资源放在Resources 或StreamingAssets文件夹下,运行的时候程序动态的来读取它们,最后显示在游戏中就可以了。在批量打包前,在Project视图下创建不同包的资源文件夹, 然后脚本 AssetDatabase 动态的将资源拷贝至Resources或StreamingAssets 文件夹中,
[MenuItem("Custom/Build Android QQ")] staticvoidPerformAndroidQQBuild() { //先把资源拷贝到Resources或者StreamingAssetsAssetDatabase.CopyAsset("path","newPath"); //然后开始编译版本 BulidTarget("QQ","Android"); }
代码中说到还有一个重要的东西就是PlayerSettings类。因为在Build的时候不同平台下可能会有一些PlatformSettings是不一样的,所以需要在脚本里面动态的设置它,强大的参数列表在这里:
另外,由于IOS工程比较特殊,使用这样的方法我们只能生成出来多个IOS的工程文件,但是这并不是最终发布的版本。如果想一键生成.ipa文件的话。
1.通过命令行来Build IOS 1生成的xcode工程,最终生成渠道包。
亲测可用!!!!超级方便
【猫猫的Unity Shader之旅】之Unity Shader概述及学习环境搭建 一、什么是ShaderShader君,中文名叫做着色器,本质上它是一段程序,专门用来处理3D图形的渲染过程。这么说大家一定还是不太懂,还是让我们有请Shader
static function FindObjectsOfType (type : Type) : Object[] 转载自:staticfunctionFindObjectsOfType(type:Type):Object[]Description描述ReturnsalistofallactiveloadedobjectsofTypetype.返回Type类型的所有激活的加载的物体列表Itwillreturnnoasse
Unity Vuforia 导出IOS 模拟器从未运行成功。3.0.9插件导出IOS各种报错,不知所云。更新至4.0.妥妥的解决了,真机测试通过。
标签: unity多开同一个项目的方法
本文链接地址:https://www.jiuchutong.com/biancheng/384576.html 转载请保留说明!上一篇:Unity3d 双摇杆 easyTouch(unity2d摇杆)
下一篇:【猫猫的Unity Shader之旅】之Unity Shader概述及学习环境搭建(猫的游戏视频)
友情链接: 武汉网站建设