最新判断Unity Webgl 运行在PC端还是手机端方法
首先:注意了! 该方法需要unitywebgl导出后才能看到效果!!!(特别注意,在unity中运行 会显示 大概意思是 找不到HelloFloat 方法)文章来源地址https://www.toymoban.com/news/detail-839459.html
- 首先需要写一个JS的脚本,主要是调用mergeInto();方法,第一个参数不用变,第二个参数就是JS的方法集合。写完之后将这个文件的后缀改为.jslib,名称就叫做myjs,放到Plugins文件夹中(没有就新建一个)
主要代码如下:
mergeInto(LibraryManager.library, {
GetUA: function () {
var uA = navigator.userAgent.toLowerCase();
var ipad = uA.match(/ipad/i) == "ipad";
var iphone = uA.match(/iphone os/i) == "iphone os";
var midp = uA.match(/midp/i) == "midp";
var uc7 = uA.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
var uc = uA.match(/ucweb/i) == "ucweb";
var android = uA.match(/android/i) == "android";
var windowsce = uA.match(/windows ce/i) == "windows ce";
var windowsmd = uA.match(/windows mobile/i) == "windows mobile";
if (!(ipad || iphone || midp || uc7 || uc || android || windowsce || windowsmd)) {
// PC 端 返回1
return 1;
}else{
// 移动端 返回2
return 2;
}
},
});
- 在unity中脚本这样写的
using UnityEngine;
using System.Runtime.InteropServices;
using UnityEngine.SceneManagement;
public class PingTai : MonoBehaviour
{
[DllImport("__Internal")]
private static extern int GetUA();
void Awake()
{
int a = GetUA();
if (a == 1)
{
SceneManager.LoadScene(1);
}
if (a == 2)
{
SceneManager.LoadScene(2);
}
}
}
- 新建三个场景,做一定区分,即可打包出来看到判断后的结果
文章来源:https://www.toymoban.com/news/detail-839459.html
到了这里,关于Unity Webgl项目判断当前是手机还是电脑运行的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!