Unity WebGL C#端调用js(.jslib)代码读写Cookie(单个不超4k)

//代码段一
mergeInto(LibraryManager.library, {
	SetCookie: function(name, value, exdays,url) {
		var urr = UTF8ToString(url);
		var cname = UTF8ToString(name);
		var cvalue = UTF8ToString(value);
		var d = new Date();
		d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
		var expires = "expires=" + d.toGMTString();
		var ull="domain="+urr;
		var msg = cname + "=" + cvalue + "; " + expires+";"+ull+";path=/";
		document.cookie = msg;
	},
	GetCookie: function(zname) {
		var cname = UTF8ToString(zname);
		var returnArg;
		var name = cname + "=";
		var ca = document.cookie.split(';');
		for (var i = 0; i < ca.length; i++) {
			var c = ca[i].trim();

			if (c.indexOf(name) == 0) {
				returnArg = c.substring(name.length, c.length);
				var bufferSize = lengthBytesUTF8(returnArg) + 1;
				var buffer = _malloc(bufferSize);
				stringToUTF8(returnArg, buffer, bufferSize);
				return buffer;
			}
		}
		return "";
	},
	ClearCookies: function() {
		let cookies = document.cookie.split(";");
		cookies.forEach(cookie => {
			let cookieName = cookie.split("=")[0];
			document.cookie = cookieName + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/";
		});
	},
	CloseWindow: function() {
		if (navigator.userAgent.indexOf("Firefox") != -1 || navigator.userAgent.indexOf("Chrome") != -1) {
			window.location.href = "about:blank";
			window.close();
		} else {
			window.opener = null;
			window.open("", "_self");
			window.close();
		}
	},

	LoadImageFile: function() {
		var fileInput = document.createElement('input');
		fileInput.type = 'file';
		fileInput.accept = ".jpg,.png";
		fileInput.style.position = 'fixed';
		fileInput.style.left = '-100vw';
		fileInput.onchange = function(e) {
			var url = URL.createObjectURL(fileInput.files[0]);
			window.unityInstance.Module.SendMessage('PlayerCenter', 'SetPath', url);
		};
		document.body.appendChild(fileInput);
		fileInput.click();
		setTimeout(function() {
				document.body.removeChild(fileInput);
			},
			100);
	},
	GetUrlToken:function(){
		var queryString = window.location.search;
		var urlParams = new URLSearchParams(queryString);
		var token = urlParams.get('token');
		if (token){
			var bufferSize = lengthBytesUTF8(token) + 1;
			var buffer = _malloc(bufferSize);
			stringToUTF8(token, buffer, bufferSize);
			return buffer;
			}
		else
			return "";
	}
});
//代码段二
mergeInto(LibraryManager.library, {
    WebGL_OpenURL: function (urlPtr) {
        var url = UTF8ToString(urlPtr);
        window.location.href = url;
    },

    WebGL_ClosePage: function () {
        try {
            window.open('', '_self').close();
        } catch (e) {
            console.log("Window close blocked:", e);
        }
    },

    WebGL_RedirectBlank: function () {
        window.location.href = "about:blank";
    },

    // 用户点击按钮触发全屏
    WebGL_RequestFullscreen: function () {
        let canvas = document.getElementById("unity-canvas");
        if (canvas.requestFullscreen) {
            canvas.requestFullscreen();
        } else if (canvas.webkitRequestFullscreen) {
            canvas.webkitRequestFullscreen();
        } else if (canvas.msRequestFullscreen) {
            canvas.msRequestFullscreen();
        } else {
            console.log("Fullscreen API not supported");
        }
    }
});

代码段一LoadImageFile需要在index页面有unityInstance的地方添加 window.unityInstance=unityInstance才能正常使用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值