Unity Webgl对多线程不完全支持
-
目前所知对Task.Delay不支持,延迟使用协程替换:
// 延时任务服务 public class DelayTaskService : Singleton<DelayTaskService> { public delegate void TaskCallback(); public Coroutine Run(float delayTime, TaskCallback callback) { return StartCoroutine(DelayRun(delayTime, callback)); } public Coroutine Run(TaskCallback callback) { return StartCoroutine(DelayRun(callback)); } public void StopTask(Coroutine coroutine) { StopCoroutine(coroutine); } private IEnumerator DelayRun(float delayTime, TaskCallback callback) { yield return new WaitForSecondsRealtime(delayTime); callback?.Invoke(); } private IEnumerator DelayRun(TaskCallback callback) { yield return null; callback?.Invoke(); } }
Unity Webgl图片空白问题
- 某些图片在图集中,但是某些部分静态使用这些图片(图片直接拖入),打包webgl会导致图片空白
Unity Webgl打包代码剥离设置
-
Player Settings ➜ Other Settings ➜ Strip Engine Code勾上
-
Player Settings ➜ Other Settings ➜ Managed Stripping Level调整为最高
-
打包设置为下图
【注意】这样会打出的包最小,但是会造成某些类的缺失,在Unity/Assets下创建link.xml文件,根据报错的ID通过Unity类ID查找添加到link文件中(相当于代码剥离的白名单),link文件格式如下:
<linker>
<assembly fullname="Unity.Timeline" preserve="all" />
<assembly fullname="DOTween" preserve="all" />
<assembly fullname="Assembly-CSharp" preserve="all" />
<assembly fullname="HotFix" preserve="all" />
<assembly fullname="UnityEngine">
<type fullname="UnityEngine.SphereCollider" preserve="all"/>
<type fullname="UnityEngine.AudioListener" preserve="all"/>
</assembly>
<assembly fullname="UnityEditor">
<type fullname="UnityEditor.PlayerSettings" preserve="all"/>
</assembly>
</linker>