赞
踩
之前用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_NEWER
- PlayerSettings.WebGL.template = "PROJECT:WXTemplate2020";
- #else
- PlayerSettings.WebGL.template = "PROJECT:WXTemplate";
- #endif
-
-
- PlayerSettings.WebGL.linkerTarget = WebGLLinkerTarget.Wasm;
-
- PlayerSettings.WebGL.dataCaching = false;
-
-
- #if UNITY_2021_2_OR_NEWER
- PlayerSettings.WebGL.debugSymbolMode = WebGLDebugSymbolMode.Embedded;
- #else
- PlayerSettings.WebGL.debugSymbols = true;
- #endif
-
-
- EditorSettings.spritePackerMode = SpritePackerMode.AlwaysOnAtlas;
-
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
对应的,我们要修改成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();
- }
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
完整的编辑器代码如下:
- public class PlatformSetting : EditorWindow
- {
- private Vector2 mScrollPosition = Vector2.zero;
- private static string wxPackageName = "WX-WASM-SDK";
-
-
-
- #region 设置模板相关
- private string mMoveDirPath = "";
- #endregion
-
-
- private 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 mac
- string macPath = path.Replace("\\", "/"); // mac finder doesn't like backward slashes
-
- if (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 this
- e.HelpLink = ""; // do anything with this variable to silence warning about not using it
- }
- }
-
- private static void OpenInWin(string path)
- {
- bool openInsidesOfFolder = false;
-
- // try windows
- string winPath = path.Replace("/", "\\"); // windows explorer doesn't like forward slashes
-
- if (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 this
- e.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();
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。