Unity3D ZFBrowser (EmbeddedBrowser) 插件嵌入网页无法输入中文问题

  网页嵌入插件最好的应该就是ZFBrowser了, 可是使用起来也是问题多多, 现在最要命的是网页输入不能打中文, 作者也没打算接入IME, 只能自己想办法了...

  搞了半天只想到一个办法, 就是通过Unity的IME去触发中文输入, 然后传入网页, 也就是说做一个透明的 InputField 盖住网页的输入文本框, 然后在 Update 或是 onValueChanged 中把内容传给网页, 这样基本就能实现中文输入了.

  因为对前端不熟悉, 我就做了一个简单网页做测试:

1<html> 2 3<head> 4 <title>My first page</title> 5 <style> 6 body { 7 margin: 0; 8 } 9 </style> 10</head> 11 12<body> 13 <h1>Test Input</h1> 14 Field1: <input type="text" id="field1"> 15 Field2: <input type="text" id="field2"> 16 <br> 17 <br> 18 <script> 19 function SetInputValue(id, str) { 20 document.getElementById(id).value = str; 21 } 22 function SubmitInput(str) 23 { 24 document.getElementById("field2").value = "Submited : " + str; 25 } 26 </script> 27 28</body> 29 30</html>

  这里网页有两个Text Area, 左边作为输入, 右边作为回车后的调用测试:

  然后Unity中直接用一个InputField放到 Field1 的位置上, 设置为透明, 通过Browser类提供的CallFunction方式调用就可以了:

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.UI; 5using UnityEngine.EventSystems; 6 7namespace UIModules.UITools 8{ 9 public class BrowserInputField : MonoBehaviour 10 { 11 [SerializeField] 12 public ZenFulcrum.EmbeddedBrowser.Browser browser; 13 [SerializeField] 14 public InputField input; 15 16 [Space(10.0f)] 17 [Header("设置网页 input 函数名称")] 18 [SerializeField] 19 public string SetInputFuncName = "SetInputFuncName"; 20 [Header("设置网页 submit 函数名称")] 21 [SerializeField] 22 public string SubmitFuncName = "SubmitFuncName"; 23 [Header("网页 input id")] 24 [SerializeField] 25 public string InputElementID = "InputElementID"; 26 27 public bool inited { get; private set; } 28 29 private void Awake() 30 { 31 this.RequireComponent<CanvasGroup>().alpha = 0.01f; 32 Init(); 33 } 34 35 public void Init() 36 { 37 if(input && (false == inited)) 38 { 39 inited = true; 40 input.RequireComponent<IME_InputFollower>(); // IME 跟随 41 42 StartCoroutine(CaretAccess((_caret) => 43 { 44 if(_caret) 45 { 46 var group = _caret.RequireComponent<CanvasGroup>(); 47 group.alpha = 1f; 48 group.ignoreParentGroups = true; 49 } 50 })); 51 } 52 } 53 54 IEnumerator CaretAccess(System.Action<Transform> access) 55 { 56 if(input) 57 { 58 var caret = input.transform.Find("InputField Input Caret"); 59 while(caret == false && input) 60 { 61 caret = input.transform.Find("InputField Input Caret"); 62 yield return null; 63 } 64 access.Invoke(caret); 65 } 66 } 67 68 void Update() 69 { 70 if(browser && input) 71 { 72 browser.CallFunction(SetInputFuncName, new ZenFulcrum.EmbeddedBrowser.JSONNode[2] 73 { 74 new ZenFulcrum.EmbeddedBrowser.JSONNode(InputElementID), 75 new ZenFulcrum.EmbeddedBrowser.JSONNode(input.isFocused ? input.text : (string.IsNullOrEmpty(input.text)?input.placeholder.GetComponent<Text>().text: input.text)) 76 }); 77 } 78 } 79 } 80}

  这里InputField它会自动生成 Caret 就是输入标记, 为了让他能显示出来, 需要等待到它创建出来之后设置透明度即可. 这里省掉了IME输入法跟随的代码, 那是其它功能了.

  恩, 因为字体大小不一样, 所以Caret位置不准确, 反正是能输入了.

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

一篇文章带你了解JavaScript日期

日期对象允许您使用日期(年、月、日、小时、分钟、秒和毫秒)。一、JavaScript的日期格式一个JavaScript日期可以写为一个字符串:ThuFeb02201909:59:51GMT0800(中国标准时间)或者是一个数字:1486000791164写数字的日期,指定的毫秒数自1970年1月1日00:00:00到现在。1\.显示日期使用