如何在获得焦点时禁用 UI.InputField 的 SelectAll() 文本? [英] How to disable SelectAll() text of UI.InputField when it gets focus?
问题描述
UI InputField 获得焦点时会突出显示其中的所有文本.我想将插入符号移动到文本的末尾,以便用户可以继续在其中写入文本.目前我有一个 hack 解决方案,它有效,但是当文本突出显示时仍然很短.这是我的黑客:
The UI InputField when it gets focus highlights all text inside. I would like to move the caret to the end of the text so the user can keep writing the text within. Currently I have a hack solution, which works, however there it is still a short moment when the text is highlighted. Here is my hack:
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class TextFieldBehaviour : MonoBehaviour, ISelectHandler
{
private InputField inputField;
private bool isCaretPositionReset = false;
void Start()
{
inputField = gameObject.GetComponent<InputField>();
}
public void OnSelect (BaseEventData eventData)
{
isCaretPositionReset = false;
}
void Update()
{
if(inputField.isFocused == true && isCaretPositionReset == false)
{
inputField.caretPosition = inputField.text.Length;
isCaretPositionReset = true;
}
}
}
我还查看了 输入字段的源代码.但是我无法在没有 SelectAll() 函数的情况下创建自定义的.由于 UnityEngine.UI.SetPropertyUtility
的保护级别,我收到了一堆错误.
I was also checking out the source code of InputField. But I have trouble trouble creating a custom one without the SelectAll() function. I am getting a bunch of errors due to the protection level of the UnityEngine.UI.SetPropertyUtility
.
推荐答案
有一个技巧可以禁用文本突出显示的短暂时刻.我设法在没有 Update()
函数的情况下重做.
There is a trick to disable the short moment when the text is highlighted. I managed to redo this without the Update()
function.
1.获取 InputField.selectionColor
的颜色.将其 alpha 设置为 0
.
1.Get the Color of the InputField.selectionColor
. Set its alpha to 0
.
2.将 #1 中的新颜色应用到 InputField
.
2.Apply the new color from #1 to the InputField
.
3.等待一帧.你必须,因为 Unity caret 会等待一帧出现.
3.Wait for one frame. You must because Unity caret waits for one frame to appear.
4.更改 InputField 插入符号位置.
4.Change the InputField caret position.
5.将 InputField.selectionColor
alpha 改回 1
.
5.Change the InputField.selectionColor
alpha back to 1
.
public class TextFieldBehaviour : MonoBehaviour, ISelectHandler
{
private InputField inputField;
private bool isCaretPositionReset = false;
void Start()
{
inputField = gameObject.GetComponent<InputField>();
}
public void OnSelect(BaseEventData eventData)
{
StartCoroutine(disableHighlight());
}
IEnumerator disableHighlight()
{
Debug.Log("Selected!");
//Get original selection color
Color originalTextColor = inputField.selectionColor;
//Remove alpha
originalTextColor.a = 0f;
//Apply new selection color without alpha
inputField.selectionColor = originalTextColor;
//Wait one Frame(MUST DO THIS!)
yield return null;
//Change the caret pos to the end of the text
inputField.caretPosition = inputField.text.Length;
//Return alpha
originalTextColor.a = 1f;
//Apply new selection color with alpha
inputField.selectionColor = originalTextColor;
}
}
注意:
将插入符号移到文本末尾的最佳方法是使用 MoveTextEnd
函数代替 inputField.caretPosition
.如果您的文本很长,您会注意到 inputField.caretPosition
的一个错误.
The best way to move caret to the end of the text is with the MoveTextEnd
function instead of inputField.caretPosition
. You will notice a bug with inputField.caretPosition
if your text is long.
如果您关心这个,请将上面代码中的 inputField.caretPosition = inputField.text.Length;
替换为 inputField.MoveTextEnd(false);
.
If you care about this, replace inputField.caretPosition = inputField.text.Length;
with inputField.MoveTextEnd(false);
in the code above.
这篇关于如何在获得焦点时禁用 UI.InputField 的 SelectAll() 文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!