当前位置: 首页 > news >正文

unity打包web,发送post请求,获取地址栏参数,解决TypeError:s.replaceAll is not a function

发送post请求

public string url = "http://XXXXXXXXX";// 请求数据public string postData = "{\"user_id\": 1}";// Start is called before the first frame updatevoid Start(){// Post();StartCoroutine(PostRequestCoroutine(url, postData));}private IEnumerator PostRequestCoroutine(string url, string jsonData){// 创建 UnityWebRequestUnityWebRequest request = new UnityWebRequest(url, "POST");byte[] jsonToSend = new System.Text.UTF8Encoding().GetBytes(jsonData);// 设置请求体和头部request.uploadHandler = new UploadHandlerRaw(jsonToSend);request.downloadHandler = new DownloadHandlerBuffer();request.SetRequestHeader("Content-Type", "application/json");// 发送请求yield return request.SendWebRequest();// 检查响应if (request.result == UnityWebRequest.Result.Success){Debug.Log("Response: " + request.downloadHandler.text);}else{Debug.LogError("Error: " + request.error);}}

如果请求是http协议,需要配置

打包web后会受到跨域的影响

获取地址栏参数

打包后,会有一个index.html文件,原理是通过js获取地址栏参数,然后使用unity的js实例,向游戏内的一个对象发送消息,调用一个方法并传递参数。

在index.html里,添加获取地址栏参数方法:

function getUrlParameter(name) {name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');var results = regex.exec(location.search);return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
}

在创建unity的js实例后,添加调用方法:

script.onload = () => {createUnityInstance(canvas, config, (progress) => {document.querySelector("#unity-progress-bar-full").style.width = 100 * progress + "%";}).then((unityInstance) => {document.querySelector("#unity-loading-bar").style.display = "none";document.querySelector("#unity-fullscreen-button").onclick = () => {unityInstance.SetFullscreen(1);};// 获取参数var param1 = getUrlParameter('uid');var param2 = getUrlParameter('t');// 调用游戏对象Controller上的ReceiveParameters方法,传递字符串参数unityInstance.SendMessage('Controller', 'ReceiveParameters', param1 + ',' + param2);}).catch((message) => {alert(message);});};

解决TypeError:s.replaceAll is not a function

 如果浏览器版本较低,运行时,会报错:TypeError:s.replaceAll is not a function

解决方法很简单,在index.html的js代码最开始,直接为字符串类型定义replaceAll方法

String.prototype.replaceAll = function(search, replacement) {const pattern = search.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'); // 转义字符const reg = new RegExp(pattern, 'g'); // 创建全局正则表达式return this.replace(reg, replacement);
};

其他一些错误解决方法:.gzip

http://www.lryc.cn/news/501033.html

相关文章:

  • java+ssm+mysql校园物品租赁网
  • Spring Boot中实现JPA多数据源配置指南
  • 服务器加固
  • 探索CSS中的背景图片属性,让你的网页更加美观
  • Oracle的打开游标(OPEN_CURSORS)
  • 数值分析—数值积分
  • 克服大规模语言模型限制,构建新的应用方法——LangChain
  • 计算机网络 —— HTTPS 协议
  • React第十七章(useRef)
  • React第十五节useReducer使用详解差异
  • NanoLog起步笔记-5-客户端简要描述
  • Flink:入门介绍
  • 目标跟踪领域经典论文解析
  • 网络编程 | TCP套接字通信及编程实现经验教程
  • SAP导出表结构并保存到Excel 源码程序
  • Linux下redis环境的搭建
  • REDMI瞄准游戏赛道,推出小屏平板
  • springai结合ollama
  • React第十三节开发中常见问题之(视图更新、事件处理)
  • 【Appium报错】安装uiautomator2失败
  • DataSophon集成CMAK KafkaManager
  • Ubuntu22.04深度学习环境安装【显卡驱动安装】
  • 21届秋/校招面经
  • 相机动态/在线标定
  • MySQL 8.0 新特性汇总
  • Resnet C ++ 部署 tensort 部署(四)
  • 《Java核心技术I》对并发散列映射的批操作
  • 记录一次使用git无权限的问题排查
  • appium学习之二:adb命令
  • Linux Vi/Vim使用 ⑥