当前位置:   article > 正文

Unity 微信小游戏转回WEBGL发布设置_select a template in player settings.

select a template in player settings.

之前用unity发布过webgl版,后来导入tx开源的小游戏工具(minigame.202211231905.unitypackage)测试一段时间后,再发布webgl版,发现有些发布设置已经被微信小游戏的工具修改过了;(在WXEditorWindow.cs里,有兴趣的童靴可以自己看下)

  1. public static void Init()
  2. {
  3. PlayerSettings.WebGL.threadsSupport = false;
  4. PlayerSettings.runInBackground = false;
  5. PlayerSettings.WebGL.compressionFormat = WebGLCompressionFormat.Disabled;
  6. #if UNITY_2020_1_OR_NEWER
  7. PlayerSettings.WebGL.template = "PROJECT:WXTemplate2020";
  8. #else
  9. PlayerSettings.WebGL.template = "PROJECT:WXTemplate";
  10. #endif
  11. PlayerSettings.WebGL.linkerTarget = WebGLLinkerTarget.Wasm;
  12. PlayerSettings.WebGL.dataCaching = false;
  13. #if UNITY_2021_2_OR_NEWER
  14. PlayerSettings.WebGL.debugSymbolMode = WebGLDebugSymbolMode.Embedded;
  15. #else
  16. PlayerSettings.WebGL.debugSymbols = true;
  17. #endif
  18. EditorSettings.spritePackerMode = SpritePackerMode.AlwaysOnAtlas;
  19. }


对应的,我们要修改成webgl版的(我的unity版本是2021.3.11):

  1. PlayerSettings.WebGL.compressionFormat = WebGLCompressionFormat.Gzip;
  2. PlayerSettings.WebGL.template = "PROJECT:MyCustomTmp";
  3. PlayerSettings.WebGL.dataCaching = true;
  4. PlayerSettings.WebGL.debugSymbolMode = WebGLDebugSymbolMode.Off;
  5. PlayerSettings.WebGL.decompressionFallback = true;


同时,微信小游戏的工具里的.jslib文件和对应的引用文件也会影响webgl版的正常打包,所以写个编辑器批处理把微信小游戏工具的整个文件夹转移到备份文件夹中。要打微信小游戏版再转回来。
 

  1. if (!string.IsNullOrEmpty(mMoveDirPath))//有填写要移动到的路径
  2. {
  3. CreateDir(mMoveDirPath);
  4. //复制最上层的.meta文件
  5. FileInfo flinfo = new FileInfo(Path.Combine(Application.dataPath, wxPackageName + ".meta"));
  6. flinfo.CopyTo(Path.Combine(mMoveDirPath, flinfo.Name), true);
  7. string orginDir = Path.Combine(Application.dataPath, wxPackageName);
  8. //复制整个文件夹
  9. if (CopyDirectory(orginDir, Path.Combine(mMoveDirPath, wxPackageName), true))
  10. {//复制完成后删除文件夹 和 文件夹的.meta文件
  11. DelectDir(orginDir);
  12. Directory.Delete(orginDir, true);
  13. var filepath = orginDir + ".meta";
  14. if (File.Exists(filepath))
  15. {
  16. File.Delete(filepath);
  17. AssetDatabase.Refresh();
  18. }
  19. }
  20. }

完整的编辑器代码如下:
 

  1. public class PlatformSetting : EditorWindow
  2. {
  3. private Vector2 mScrollPosition = Vector2.zero;
  4. private static string wxPackageName = "WX-WASM-SDK";
  5. #region 设置模板相关
  6. private string mMoveDirPath = "";
  7. #endregion
  8. private void OnGUI()
  9. {
  10. mScrollPosition = GUILayout.BeginScrollView(mScrollPosition);
  11. EditorGUILayout.LabelField("", EditorStyles.boldLabel);
  12. //模板设置
  13. EditorGUILayout.LabelField("Tmp Settings", EditorStyles.boldLabel);
  14. //GUILayout.Label("导出路径", labelStyle);
  15. var choosePathButtonClicked = false;
  16. var openTargetButtonClicked = false;
  17. var resetButtonClicked = false;
  18. if (mMoveDirPath == "")
  19. {
  20. GUIStyle pathButtonStyle = new GUIStyle(GUI.skin.button);
  21. pathButtonStyle.fontSize = 12;
  22. pathButtonStyle.margin.left = 20;
  23. choosePathButtonClicked = GUILayout.Button("选择移动到路径", pathButtonStyle, GUILayout.Height(30), GUILayout.Width(200));
  24. }
  25. else
  26. {
  27. int pathButtonHeight = 28;
  28. GUIStyle pathLabelStyle = new GUIStyle(GUI.skin.textField);
  29. pathLabelStyle.fontSize = 12;
  30. pathLabelStyle.alignment = TextAnchor.MiddleLeft;
  31. pathLabelStyle.margin.top = 6;
  32. pathLabelStyle.margin.bottom = 6;
  33. pathLabelStyle.margin.left = 20;
  34. GUILayout.BeginHorizontal();
  35. // 路径框
  36. GUILayout.Label(mMoveDirPath, pathLabelStyle, GUILayout.Height(pathButtonHeight - 6), GUILayout.ExpandWidth(true), GUILayout.MaxWidth(EditorGUIUtility.currentViewWidth - 126));
  37. openTargetButtonClicked = GUILayout.Button("打开", GUILayout.Height(pathButtonHeight), GUILayout.Width(40));
  38. resetButtonClicked = GUILayout.Button("重选", GUILayout.Height(pathButtonHeight), GUILayout.Width(40));
  39. GUILayout.EndHorizontal();
  40. }
  41. EditorGUILayout.Space();
  42. if (choosePathButtonClicked)
  43. {
  44. // 弹出选目录窗口
  45. var dstPath = EditorUtility.SaveFolderPanel("选择你要暂时移动至的目录", "", "");
  46. if (dstPath != "")
  47. {
  48. mMoveDirPath = dstPath;
  49. }
  50. }
  51. if (openTargetButtonClicked)
  52. {把微信的通用函数挪来用了
  53. ShowInExplorer(mMoveDirPath);
  54. }
  55. if (resetButtonClicked)
  56. {
  57. mMoveDirPath = "";
  58. }
  59. EditorGUILayout.LabelField("", EditorStyles.boldLabel);
  60. if (GUILayout.Button("设置回自定义的webgl模板"))
  61. {
  62. SetCustomWebGLTmp();
  63. }
  64. if (GUILayout.Button("还原微信回原本文件夹"))
  65. {
  66. ReturnToWXDir();
  67. }
  68. GUILayout.EndScrollView();
  69. }
  70. /// <summary>
  71. /// 设置回自己自定义的模板
  72. /// </summary>
  73. void SetCustomWebGLTmp()
  74. {
  75. PlayerSettings.WebGL.compressionFormat = WebGLCompressionFormat.Gzip;
  76. PlayerSettings.WebGL.template = "PROJECT:MyCustomTmp";
  77. PlayerSettings.WebGL.dataCaching = true;
  78. PlayerSettings.WebGL.debugSymbolMode = WebGLDebugSymbolMode.Off;
  79. PlayerSettings.WebGL.decompressionFallback = true;
  80. //Debug.Log(Application.dataPath);
  81. if (!string.IsNullOrEmpty(mMoveDirPath))//有填写
  82. {
  83. CreateDir(mMoveDirPath);
  84. //复制最上层的.meta文件
  85. FileInfo flinfo = new FileInfo(Path.Combine(Application.dataPath, wxPackageName + ".meta"));
  86. flinfo.CopyTo(Path.Combine(mMoveDirPath, flinfo.Name), true);
  87. string orginDir = Path.Combine(Application.dataPath, wxPackageName);
  88. //复制整个文件夹
  89. if (CopyDirectory(orginDir, Path.Combine(mMoveDirPath, wxPackageName), true))
  90. {//复制完成后删除文件夹 和 文件夹的.meta文件
  91. DelectDir(orginDir);
  92. Directory.Delete(orginDir, true);
  93. var filepath = orginDir + ".meta";
  94. if (File.Exists(filepath))
  95. {
  96. File.Delete(filepath);
  97. AssetDatabase.Refresh();
  98. }
  99. }
  100. }
  101. }
  102. /// <summary>
  103. /// 还原文件到微信目录
  104. /// </summary>
  105. void ReturnToWXDir()
  106. {
  107. string orginDir = Path.Combine(Application.dataPath, wxPackageName);
  108. var filepath = orginDir + ".meta";
  109. CreateDir(orginDir);
  110. CopyDirectory(Path.Combine(mMoveDirPath, wxPackageName), orginDir, true);
  111. FileInfo flinfo = new FileInfo(Path.Combine(mMoveDirPath, wxPackageName + ".meta"));
  112. flinfo.CopyTo(Path.Combine(Application.dataPath, flinfo.Name), true);
  113. AssetDatabase.Refresh();
  114. }
  115. /// <summary>
  116. /// 复制文件夹
  117. /// </summary>
  118. /// <param name="SourcePath"></param>
  119. /// <param name="DestinationPath"></param>
  120. /// <param name="overwriteexisting"></param>
  121. /// <returns></returns>
  122. private bool CopyDirectory(string SourcePath, string DestinationPath, bool overwriteexisting)
  123. {
  124. bool ret = false;
  125. var separator = Path.DirectorySeparatorChar;
  126. var ignoreFiles = new List<string>() { };// { "unityNamespace.js" };
  127. // eventEmitter - 改名为event-emitter
  128. // loading和libs 是可交互视频用到的文件,先下掉可交互方案
  129. var ignoreDirs = new List<string>() { };//{ "eventEmitter", "loading", "libs" };
  130. try
  131. {
  132. if (Directory.Exists(SourcePath))
  133. {
  134. if (Directory.Exists(DestinationPath) == false)
  135. {
  136. Directory.CreateDirectory(DestinationPath);
  137. }
  138. else
  139. {
  140. // 已经存在,删掉目录下无用的文件
  141. foreach (string filename in ignoreFiles)
  142. {
  143. var filepath = Path.Combine(DestinationPath, filename);
  144. if (File.Exists(filepath))
  145. {
  146. File.Delete(filepath);
  147. }
  148. }
  149. foreach (string dir in ignoreDirs)
  150. {
  151. var dirpath = Path.Combine(DestinationPath, dir);
  152. if (Directory.Exists(dirpath))
  153. {
  154. Directory.Delete(dirpath);
  155. }
  156. }
  157. }
  158. foreach (string fls in Directory.GetFiles(SourcePath))
  159. {
  160. FileInfo flinfo = new FileInfo(fls);
  161. //if (flinfo.Extension == ".meta" || ignoreFiles.Contains(flinfo.Name))
  162. //{
  163. // continue;
  164. //}
  165. flinfo.CopyTo(Path.Combine(DestinationPath, flinfo.Name), overwriteexisting);
  166. }
  167. foreach (string drs in Directory.GetDirectories(SourcePath))
  168. {
  169. DirectoryInfo drinfo = new DirectoryInfo(drs);
  170. if (ignoreDirs.Contains(drinfo.Name))
  171. {
  172. continue;
  173. }
  174. if (CopyDirectory(drs, Path.Combine(DestinationPath, drinfo.Name), overwriteexisting) == false)
  175. ret = false;
  176. }
  177. }
  178. ret = true;
  179. }
  180. catch (Exception ex)
  181. {
  182. ret = false;
  183. UnityEngine.Debug.LogError(ex);
  184. }
  185. return ret;
  186. }
  187. # region 一些IO操作
  188. public static void CreateDir(string srcPath)
  189. {
  190. if (!Directory.Exists(srcPath))
  191. {
  192. DirectoryInfo dir = new DirectoryInfo(srcPath);
  193. CreateDir(dir.Parent.ToString());
  194. Directory.CreateDirectory(srcPath);
  195. }
  196. return;
  197. }
  198. public static void DelectDir(string srcPath)
  199. {
  200. if (!Directory.Exists(srcPath))
  201. {
  202. return;
  203. }
  204. try
  205. {
  206. DirectoryInfo dir = new DirectoryInfo(srcPath);
  207. FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); //返回目录中所有文件和子目录
  208. foreach (FileSystemInfo i in fileinfo)
  209. {
  210. if (i is DirectoryInfo) //判断是否文件夹
  211. {
  212. DirectoryInfo subdir = new DirectoryInfo(i.FullName);
  213. subdir.Delete(true); //删除子目录和文件
  214. }
  215. else
  216. { //如果 使用了 streamreader 在删除前 必须先关闭流 ,否则无法删除 sr.close();
  217. File.Delete(i.FullName); //删除指定文件
  218. }
  219. }
  220. }
  221. catch (Exception e)
  222. {
  223. throw e;
  224. }
  225. }
  226. private static bool IsInMacOS
  227. {
  228. get
  229. {
  230. return UnityEngine.SystemInfo.operatingSystem.IndexOf("Mac OS") != -1;
  231. }
  232. }
  233. private static bool IsInWinOS
  234. {
  235. get
  236. {
  237. return UnityEngine.SystemInfo.operatingSystem.IndexOf("Windows") != -1;
  238. }
  239. }
  240. public static void ShowInExplorer(string path)
  241. {
  242. if (IsInWinOS)
  243. {
  244. OpenInWin(path);
  245. }
  246. else if (IsInMacOS)
  247. {
  248. OpenInMac(path);
  249. }
  250. else // couldn't determine OS
  251. {
  252. OpenInWin(path);
  253. OpenInMac(path);
  254. }
  255. }
  256. private static void OpenInMac(string path)
  257. {
  258. bool openInsidesOfFolder = false;
  259. // try mac
  260. string macPath = path.Replace("\\", "/"); // mac finder doesn't like backward slashes
  261. if (Directory.Exists(macPath)) // if path requested is a folder, automatically open insides of that folder
  262. {
  263. openInsidesOfFolder = true;
  264. }
  265. if (!macPath.StartsWith("\""))
  266. {
  267. macPath = "\"" + macPath;
  268. }
  269. if (!macPath.EndsWith("\""))
  270. {
  271. macPath = macPath + "\"";
  272. }
  273. string arguments = (openInsidesOfFolder ? "" : "-R ") + macPath;
  274. try
  275. {
  276. System.Diagnostics.Process.Start("open", arguments);
  277. }
  278. catch (System.ComponentModel.Win32Exception e)
  279. {
  280. // tried to open mac finder in windows
  281. // just silently skip error
  282. // we currently have no platform define for the current OS we are in, so we resort to this
  283. e.HelpLink = ""; // do anything with this variable to silence warning about not using it
  284. }
  285. }
  286. private static void OpenInWin(string path)
  287. {
  288. bool openInsidesOfFolder = false;
  289. // try windows
  290. string winPath = path.Replace("/", "\\"); // windows explorer doesn't like forward slashes
  291. if (Directory.Exists(winPath)) // if path requested is a folder, automatically open insides of that folder
  292. {
  293. openInsidesOfFolder = true;
  294. }
  295. try
  296. {
  297. System.Diagnostics.Process.Start("explorer.exe", (openInsidesOfFolder ? "/root," : "/select,") + winPath);
  298. }
  299. catch (System.ComponentModel.Win32Exception e)
  300. {
  301. // tried to open win explorer in mac
  302. // just silently skip error
  303. // we currently have no platform define for the current OS we are in, so we resort to this
  304. e.HelpLink = ""; // do anything with this variable to silence warning about not using it
  305. }
  306. }
  307. #endregion
  308. }
  309. public class PackageEnter : EditorWindow
  310. {
  311. [MenuItem("Tools/自动设置工具")]
  312. static void ShowAutoPackWindow()
  313. {
  314. PlatformSetting window = EditorWindow.GetWindow<PlatformSetting>();
  315. window.Show();
  316. }
  317. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/83082
推荐阅读
相关标签
  

闽ICP备14008679号