maltadirk 发表于 2022-6-13 11:41

unity app 转webgl 坑位

最近需求是 老项目转webgl,在修改过程中遇见了一些坑
1.UnityLoader.js:4 To use dlopen, you need to use Emscripten's linking support, see https://github.com/kripken/emscripten/wiki/Linking



锁定问题:DllImport 所致,意思是不能直接使用dll问题
//举例
//会出错

private static extern bool InternetGetConnectedState(ref int Flag, int dwReserved);

//应该关闭其他平台的dll 导入
#if !UNITY_WEBGL
   
        private static extern bool InternetGetConnectedState(ref int Flag, int dwReserved);
#endif

//例如 tolua webgl
#if(UNITY_IPHONE || UNITY_WEBGL)
      public const string LUADLL = "__Internal";
#else
      public const string LUADLL = "tolua";
#endif
      /*
      ** third party library
      */
      




tolua webgl

2. 多线程,子线程不工作

unity打包后 IL2cpp, 最后就是js, JavaScript不支持多线程, 网上可查
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{
    // Start is called before the first frame update
    IEnumerator Start()
    {
      yield return 0;
      Debug.Log("test....1");
      yield return 0;
      Thread test = new Thread(OnTest);
      test.Start(213);
      Debug.Log("test....2");
    }

    private void OnTest(object obj)
    {//在编辑器、ios、Android、windows下会打印 ‘213’,但是webgl下 不会有这个打印
       Debug.Log(obj.ToString());
    }
}
webgl 输出:test....1
                  test....2
3. System.IO 操作

webgl 其实就是网页,它是不能直接读取硬盘的,我们平时使用的同步File.ReadLines、 File.WriteAllText等等文件读写操作是不能完成的,读取文件 直接是unitywebrequest 异步操作,至于保存文件 需要后台服务.assetbundle 也不能使用 ,下图的工作原理和File类似

页: [1]
查看完整版本: unity app 转webgl 坑位