Unity WebGL文本输入

插件

kou-yeung/WebGLInput

版本如下图所示:

使用

Unity版本:2019.4.40

使用:文本输入组件对象添加WebGLInput脚本,没有其他额外操作,输入组件正常使用

问题:插件中部分C#语法需要高版本支持,可自行修改。

发布

使用WebGL默认模板发布。

问题:

非全屏模式下,文本输入正常,输入光标跟随移动。

全屏模式下,文本输入异常,输入光标不跟随移动。

解决:

运行插件作者的示例项目(默认),查看网页原代码。

修改index.html文件,覆盖全屏按钮的点击事件。修改如下:

csharp 复制代码
  <script>
    var fullscreenButton = document.querySelector(".fullscreen");
    fullscreenButton.onclick = function () {
      document.makeFullscreen('unityContainer');
    };
  </script>

完整index.html

csharp 复制代码
<!DOCTYPE html>
<html lang="en-us">

<head>
  <meta charset="utf-8">
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <title>Wooden Toy Production Line Webgl</title>
  <link rel="shortcut icon" href="TemplateData/favicon.ico">
  <link rel="stylesheet" href="TemplateData/style.css">
  <script src="TemplateData/UnityProgress.js"></script>
  <script src="Build/UnityLoader.js"></script>
  <script>
    var unityInstance = UnityLoader.instantiate("unityContainer", "Build/WebGL.json", { onProgress: UnityProgress });
  </script>
</head>

<body>
  <div class="webgl-content">
    <div id="unityContainer" style="width: 1008px; height: 567px"></div>
    <div class="footer">
      <div class="webgl-logo"></div>
      <div class="fullscreen" onclick="unityInstance.SetFullscreen(1)"></div>
      <div class="title">Test Text Input</div>
    </div>
  </div>
  <script>
    var fullscreenButton = document.querySelector(".fullscreen");
    fullscreenButton.onclick = function () {
      document.makeFullscreen('unityContainer');
    };
  </script>
</body>

</html>

成功解决

小优化

使用 WebGL 模板

创建一个webgl模板,不需要每次发布后修改index.html

  1. 找到引擎安装路径,在文件夹中查找webgl模板,例如搜索 WebGLTemplates
  2. Assets文件夹下新建文件夹WebGLTemplates,此文件夹中新建文件夹Custom,名字区分不同的模板
  3. 复制默认模板中的内容到Custom文件夹中,修改index.html
  4. 项目设置窗口中选择自定义的模板
相关推荐
babywang016 小时前
Unity笔记--AssetBundle详解
unity·游戏引擎
魔士于安17 小时前
经典Game
游戏·游戏引擎·贴图·技术美术·模型
淡海水17 小时前
01-04-认知篇-Unity内存全景
unity·c#·游戏引擎·.net·gc
598866753@qq.com1 天前
Codex与Unity
unity·游戏引擎
淡海水1 天前
01-03-认知篇-C#内存模型深度解析
unity·c#·游戏引擎·.net·gc
警醒与鞭策2 天前
Direct3D 12 中的内存管理
3d·游戏引擎·direct3d
淡海水2 天前
16-02-C#常用数据结构源码-附录B-源码索引速查
unity·c#·游戏引擎·il2cpp
速易达网络2 天前
无人机远程操控仿真
游戏引擎·cocos2d
郝学胜-神的一滴2 天前
游戏引擎原理与实践 02:揭开3A游戏背后的技术面纱
开发语言·计算机网络·程序人生·游戏·游戏引擎
警醒与鞭策2 天前
【无标题】
android·unity·性能优化·游戏引擎·perforce