Unity 微信小游戏转回WEBGL发布设置
之前用unity发布过webgl版,后来导入tx开源的小游戏工具(minigame.202211231905.unitypackage)测试一段时间后,再发布webgl版,发现有些发布设置已经被微信小游戏的工具修改过了;(在WXEditorWindow.cs里,有兴趣的童靴可以自己看下)
public static void Init(){PlayerSettings.WebGL.threadsSupport = false;PlayerSettings.runInBackground = false;PlayerSettings.WebGL.compressionFormat = WebGLCompressionFormat.Disabled;
#if UNITY_2020_1_OR_NEWERPlayerSettings.WebGL.template = "PROJECT:WXTemplate2020";
#elsePlayerSettings.WebGL.template = "PROJECT:WXTemplate";
#endifPlayerSettings.WebGL.linkerTarget = WebGLLinkerTarget.Wasm;PlayerSettings.WebGL.dataCaching = false;#if UNITY_2021_2_OR_NEWERPlayerSettings.WebGL.debugSymbolMode = WebGLDebugSymbolMode.Embedded;
#elsePlayerSettings.WebGL.debugSymbols = true;
#endifEditorSettings.spritePackerMode = SpritePackerMode.AlwaysOnAtlas;}
对应的,我们要修改成webgl版的(我的unity版本是2021.3.11):
PlayerSettings.WebGL.compressionFormat = WebGLCompressionFormat.Gzip;PlayerSettings.WebGL.template = "PROJECT:MyCustomTmp";PlayerSettings.WebGL.dataCaching = true;PlayerSettings.WebGL.debugSymbolMode = WebGLDebugSymbolMode.Off;PlayerSettings.WebGL.decompressionFallback = true;
同时,微信小游戏的工具里的.jslib文件和对应的引用文件也会影响webgl版的正常打包,所以写个编辑器批处理把微信小游戏工具的整个文件夹转移到备份文件夹中。要打微信小游戏版再转回来。
if (!string.IsNullOrEmpty(mMoveDirPath))//有填写要移动到的路径{CreateDir(mMoveDirPath);//复制最上层的.meta文件FileInfo flinfo = new FileInfo(Path.Combine(Application.dataPath, wxPackageName + ".meta"));flinfo.CopyTo(Path.Combine(mMoveDirPath, flinfo.Name), true);string orginDir = Path.Combine(Application.dataPath, wxPackageName);//复制整个文件夹if (CopyDirectory(orginDir, Path.Combine(mMoveDirPath, wxPackageName), true)){//复制完成后删除文件夹 和 文件夹的.meta文件DelectDir(orginDir);Directory.Delete(orginDir, true);var filepath = orginDir + ".meta";if (File.Exists(filepath)){File.Delete(filepath);AssetDatabase.Refresh();}}}
完整的编辑器代码如下:
public class PlatformSetting : EditorWindow{private Vector2 mScrollPosition = Vector2.zero;private static string wxPackageName = "WX-WASM-SDK";#region 设置模板相关private string mMoveDirPath = "";#endregionprivate void OnGUI(){mScrollPosition = GUILayout.BeginScrollView(mScrollPosition);EditorGUILayout.LabelField("", EditorStyles.boldLabel);//模板设置EditorGUILayout.LabelField("Tmp Settings", EditorStyles.boldLabel);//GUILayout.Label("导出路径", labelStyle);var choosePathButtonClicked = false;var openTargetButtonClicked = false;var resetButtonClicked = false;if (mMoveDirPath == ""){GUIStyle pathButtonStyle = new GUIStyle(GUI.skin.button);pathButtonStyle.fontSize = 12;pathButtonStyle.margin.left = 20;choosePathButtonClicked = GUILayout.Button("选择移动到路径", pathButtonStyle, GUILayout.Height(30), GUILayout.Width(200));}else{int pathButtonHeight = 28;GUIStyle pathLabelStyle = new GUIStyle(GUI.skin.textField);pathLabelStyle.fontSize = 12;pathLabelStyle.alignment = TextAnchor.MiddleLeft;pathLabelStyle.margin.top = 6;pathLabelStyle.margin.bottom = 6;pathLabelStyle.margin.left = 20;GUILayout.BeginHorizontal();// 路径框GUILayout.Label(mMoveDirPath, pathLabelStyle, GUILayout.Height(pathButtonHeight - 6), GUILayout.ExpandWidth(true), GUILayout.MaxWidth(EditorGUIUtility.currentViewWidth - 126));openTargetButtonClicked = GUILayout.Button("打开", GUILayout.Height(pathButtonHeight), GUILayout.Width(40));resetButtonClicked = GUILayout.Button("重选", GUILayout.Height(pathButtonHeight), GUILayout.Width(40));GUILayout.EndHorizontal();}EditorGUILayout.Space();if (choosePathButtonClicked){// 弹出选目录窗口var dstPath = EditorUtility.SaveFolderPanel("选择你要暂时移动至的目录", "", "");if (dstPath != ""){mMoveDirPath = dstPath;}}if (openTargetButtonClicked){把微信的通用函数挪来用了ShowInExplorer(mMoveDirPath);}if (resetButtonClicked){mMoveDirPath = "";}EditorGUILayout.LabelField("", EditorStyles.boldLabel);if (GUILayout.Button("设置回自定义的webgl模板")){SetCustomWebGLTmp();}if (GUILayout.Button("还原微信回原本文件夹")){ReturnToWXDir();}GUILayout.EndScrollView();}/// <summary>/// 设置回自己自定义的模板/// </summary>void SetCustomWebGLTmp(){PlayerSettings.WebGL.compressionFormat = WebGLCompressionFormat.Gzip;PlayerSettings.WebGL.template = "PROJECT:MyCustomTmp";PlayerSettings.WebGL.dataCaching = true;PlayerSettings.WebGL.debugSymbolMode = WebGLDebugSymbolMode.Off;PlayerSettings.WebGL.decompressionFallback = true;//Debug.Log(Application.dataPath);if (!string.IsNullOrEmpty(mMoveDirPath))//有填写{CreateDir(mMoveDirPath);//复制最上层的.meta文件FileInfo flinfo = new FileInfo(Path.Combine(Application.dataPath, wxPackageName + ".meta"));flinfo.CopyTo(Path.Combine(mMoveDirPath, flinfo.Name), true);string orginDir = Path.Combine(Application.dataPath, wxPackageName);//复制整个文件夹if (CopyDirectory(orginDir, Path.Combine(mMoveDirPath, wxPackageName), true)){//复制完成后删除文件夹 和 文件夹的.meta文件DelectDir(orginDir);Directory.Delete(orginDir, true);var filepath = orginDir + ".meta";if (File.Exists(filepath)){File.Delete(filepath);AssetDatabase.Refresh();}}}}/// <summary>/// 还原文件到微信目录/// </summary>void ReturnToWXDir(){string orginDir = Path.Combine(Application.dataPath, wxPackageName);var filepath = orginDir + ".meta";CreateDir(orginDir);CopyDirectory(Path.Combine(mMoveDirPath, wxPackageName), orginDir, true);FileInfo flinfo = new FileInfo(Path.Combine(mMoveDirPath, wxPackageName + ".meta"));flinfo.CopyTo(Path.Combine(Application.dataPath, flinfo.Name), true);AssetDatabase.Refresh();}/// <summary>/// 复制文件夹/// </summary>/// <param name="SourcePath"></param>/// <param name="DestinationPath"></param>/// <param name="overwriteexisting"></param>/// <returns></returns>private bool CopyDirectory(string SourcePath, string DestinationPath, bool overwriteexisting){bool ret = false;var separator = Path.DirectorySeparatorChar;var ignoreFiles = new List<string>() { };// { "unityNamespace.js" };// eventEmitter - 改名为event-emitter// loading和libs 是可交互视频用到的文件,先下掉可交互方案var ignoreDirs = new List<string>() { };//{ "eventEmitter", "loading", "libs" };try{if (Directory.Exists(SourcePath)){if (Directory.Exists(DestinationPath) == false){Directory.CreateDirectory(DestinationPath);}else{// 已经存在,删掉目录下无用的文件foreach (string filename in ignoreFiles){var filepath = Path.Combine(DestinationPath, filename);if (File.Exists(filepath)){File.Delete(filepath);}}foreach (string dir in ignoreDirs){var dirpath = Path.Combine(DestinationPath, dir);if (Directory.Exists(dirpath)){Directory.Delete(dirpath);}}}foreach (string fls in Directory.GetFiles(SourcePath)){FileInfo flinfo = new FileInfo(fls);//if (flinfo.Extension == ".meta" || ignoreFiles.Contains(flinfo.Name))//{// continue;//}flinfo.CopyTo(Path.Combine(DestinationPath, flinfo.Name), overwriteexisting);}foreach (string drs in Directory.GetDirectories(SourcePath)){DirectoryInfo drinfo = new DirectoryInfo(drs);if (ignoreDirs.Contains(drinfo.Name)){continue;}if (CopyDirectory(drs, Path.Combine(DestinationPath, drinfo.Name), overwriteexisting) == false)ret = false;}}ret = true;}catch (Exception ex){ret = false;UnityEngine.Debug.LogError(ex);}return ret;}# region 一些IO操作public static void CreateDir(string srcPath){if (!Directory.Exists(srcPath)){DirectoryInfo dir = new DirectoryInfo(srcPath);CreateDir(dir.Parent.ToString());Directory.CreateDirectory(srcPath);}return;}public static void DelectDir(string srcPath){if (!Directory.Exists(srcPath)){return;}try{DirectoryInfo dir = new DirectoryInfo(srcPath);FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); //返回目录中所有文件和子目录foreach (FileSystemInfo i in fileinfo){if (i is DirectoryInfo) //判断是否文件夹{DirectoryInfo subdir = new DirectoryInfo(i.FullName);subdir.Delete(true); //删除子目录和文件}else{ //如果 使用了 streamreader 在删除前 必须先关闭流 ,否则无法删除 sr.close();File.Delete(i.FullName); //删除指定文件}}}catch (Exception e){throw e;}}private static bool IsInMacOS{get{return UnityEngine.SystemInfo.operatingSystem.IndexOf("Mac OS") != -1;}}private static bool IsInWinOS{get{return UnityEngine.SystemInfo.operatingSystem.IndexOf("Windows") != -1;}}public static void ShowInExplorer(string path){if (IsInWinOS){OpenInWin(path);}else if (IsInMacOS){OpenInMac(path);}else // couldn't determine OS{OpenInWin(path);OpenInMac(path);}}private static void OpenInMac(string path){bool openInsidesOfFolder = false;// try macstring macPath = path.Replace("\\", "/"); // mac finder doesn't like backward slashesif (Directory.Exists(macPath)) // if path requested is a folder, automatically open insides of that folder{openInsidesOfFolder = true;}if (!macPath.StartsWith("\"")){macPath = "\"" + macPath;}if (!macPath.EndsWith("\"")){macPath = macPath + "\"";}string arguments = (openInsidesOfFolder ? "" : "-R ") + macPath;try{System.Diagnostics.Process.Start("open", arguments);}catch (System.ComponentModel.Win32Exception e){// tried to open mac finder in windows// just silently skip error// we currently have no platform define for the current OS we are in, so we resort to thise.HelpLink = ""; // do anything with this variable to silence warning about not using it}}private static void OpenInWin(string path){bool openInsidesOfFolder = false;// try windowsstring winPath = path.Replace("/", "\\"); // windows explorer doesn't like forward slashesif (Directory.Exists(winPath)) // if path requested is a folder, automatically open insides of that folder{openInsidesOfFolder = true;}try{System.Diagnostics.Process.Start("explorer.exe", (openInsidesOfFolder ? "/root," : "/select,") + winPath);}catch (System.ComponentModel.Win32Exception e){// tried to open win explorer in mac// just silently skip error// we currently have no platform define for the current OS we are in, so we resort to thise.HelpLink = ""; // do anything with this variable to silence warning about not using it}}#endregion}public class PackageEnter : EditorWindow{[MenuItem("Tools/自动设置工具")]static void ShowAutoPackWindow(){PlatformSetting window = EditorWindow.GetWindow<PlatformSetting>();window.Show();}}
Unity 微信小游戏转回WEBGL发布设置相关推荐
- Unity - 微信小游戏
总参考:Unity WebGL 微信小游戏适配方案(公测) 安装与使用 下载 Unity插件,并导入至游戏项目中,版本更新请查看更新日志 请查阅推荐引擎版本,安装时选择WebGL组件 最终选择Unit ...
- Unity微信小游戏使用微信云开发记录
最近项目上架微信小游戏,首先使用了微信官方sdk转成微信小游戏,官方地址如下: https://github.com/wechat-miniprogram/minigame-unity-webgl-t ...
- unity微信小游戏开发【含源码】
经过 经过断断续续一个多月的开发, 10次左右大大小小的修改, <熊猫奇遇记>开发完成. 遗憾 遗憾的是小游戏并没有通过微信的审核,4次全败, 每次都是会修改很多东西, 再经过朋友们的试玩 ...
- Unity 开发微信小游戏初探
前言 最近因项目需要开始研究Unity开发微信小游戏相关的知识.期间遇到各种坑,网上查阅的资料基本类似,无法解决自己遇到的问题.特用本文记录下过程,方便其他人遇到同样的问题时能够参考. 开发环境 Un ...
- 【论坛精华】个人微信小游戏发布流程
最近微信小游戏太火了.微信有庞大的流量基础,又有社交分享功能,第三方开发者通过小游戏的社交能力实现爆发,不难看出,当前是上线微信小游戏不错的时机,各位开发者势必不能错过这波红利.那么个人要如何发布微信 ...
- 【微信小游戏实战】零基础制作《欢乐停车场》二、关卡设计
1.游戏立项 微信小游戏中有一款<欢乐停车场Plus>的小游戏,大家可以搜索玩下.这是一款益智类的小游戏,游戏中有红.黄.绿.蓝.紫5辆豪车6个停车位,玩家通过可行走路线移动小车,最终让各 ...
- 新手入门:如何用Laya开发微信小游戏?
1.环境准备 1.1 LayaAirIDE 1.7.14版本才开始集成微信小游戏开发. 1.2 微信小游戏开发工具 微信小游戏开发工具是小游戏开发与测试的环境,由于LayaAir引擎的开发者完全可以使 ...
- Laya之微信小游戏入门
1.环境准备 1.1 LayaAirIDE 1.7.14版本才开始集成微信小游戏开发 1.2 微信小游戏开发工具 微信小游戏开发工具是小游戏开发与测试的环境,由于LayaAir引擎的开发者完全可以使用 ...
- 微信小游戏个人开发者如何盈利
微信小游戏,抖音小游戏,等H5小游戏非常的火,也处于流量的红利期,那么对于我们个人开发者而言我们能能否抓住微信小游戏的红利期,来实现人生的第一桶金,来实现睡后有收入呢? 今天小编带你来看下,个人开发者 ...
最新文章
- 1.5s~0.02s,期间我们可以做些什么?
- 20145201 《信息安全系统设计基础》课程总结
- Delphi XE7 Update1修正列表
- 网络逻辑结构设计的内容不包括( )。【最全!最详细解析!】
- Uncaught TypeError: object is not a function
- [AGC016B]Colorful Hats
- java 字符串文字筛选_重新开始Java的原始字符串文字讨论
- C++ 异常变量的生命周期
- 《C++ Primer 5th》笔记(10 / 19):泛型算法
- 工作的习惯,看到好收藏下
- 运用Unity实现AOP拦截器[结合异常记录实例]
- 单调队列:temperature
- Thread 编程:简明(1) - 协作式取消 VS 线程终止
- 转载:编译原理三大圣书——龙书、虎书、鲸书
- 电容式麦克风和动圈式麦克风的工作原理
- ALFA缺陷检测软件外观检测自学习人工智能软件
- 初探ViewBinding
- wps本地模板文件夹的位置
- 自定义 Oh My Zsh 主题 cchi.zsh-theme
- H5兼容问题及解决方法