点击输入字段时如何禁用文本突出显示?

时间:2019-09-10 15:32:45

标签: c# unity3d input user-input mobile-devices

我知道之前曾有人问过这个问题,但是所有解决方案似乎都无法在移动设备上运行。我发现here的解决方案可以在我的PC上完美运行,但不幸的是,在我的平板电脑上效果不佳。我对解决方案进行了评论,以查看他们是否可以提供帮助,但似乎他们已经一年没有使用SO了。因此,我在这里试试运气,看看是否有人知道如何使其解决方案在移动设备上运行?

这是我的代码:

using System.Collections;
using System.Collections.Generic;
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)
    {
         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;

        if (inputField.touchScreenKeyboard.canSetSelection)
            inputField.touchScreenKeyboard.selection = new RangeInt(0, 0);
    }
}

1 个答案:

答案 0 :(得分:1)

如果您正在写本机屏幕键盘,则可以尝试this

panel-heading

RangeInt的参数是所选内容的起始位置和长度;

请记住,并非所有平台都支持此功能。有关所有TouchScreenKeyboard功能,请参见文档herehere