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

C# 如何在点击输入字段时禁用文本突出显示?,c#,unity3d,input,user-input,mobile-devices,C#,Unity3d,Input,User Input,Mobile Devices,我知道以前有人问过这个问题,但这些解决方案似乎都不适用于移动设备。我发现这个解决方案在我的电脑上运行得很好,但不幸的是在我的平板电脑上却不行。我已经对解决方案发表了评论,看看他们是否能提供帮助,但他们似乎已经一年多没有这样做了。因此,我在这里碰碰运气,看看是否有人知道如何在移动设备上使用他们的解决方案 这是我的代码: using System.Collections; using System.Collections.Generic; using UnityEngine; using Unity

我知道以前有人问过这个问题,但这些解决方案似乎都不适用于移动设备。我发现这个解决方案在我的电脑上运行得很好,但不幸的是在我的平板电脑上却不行。我已经对解决方案发表了评论,看看他们是否能提供帮助,但他们似乎已经一年多没有这样做了。因此,我在这里碰碰运气,看看是否有人知道如何在移动设备上使用他们的解决方案

这是我的代码:

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);
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.EventSystems;
使用UnityEngine.UI;
公共类TextFieldBehavior:MonoBehavior,ISelectHandler
{
专用输入字段输入字段;
私有bool isCaretPositionReset=false;
void Start()
{ 
inputField=gameObject.GetComponent();
}
public void OnSelect(BaseEventData eventData)
{
start例程(disableHighlight());
}
IEnumerator disableHighlight()
{
Log(“选中!”);
//获取原始选择颜色
Color originalTextColor=inputField.selectionColor;
//移除阿尔法
originalTextColor.a=0f;
//应用不带alpha的新选择颜色
inputField.selectionColor=原始文本颜色;
//等待一帧(必须执行此操作!)
收益返回空;
//将插入符号位置更改为文本的结尾
inputField.caretPosition=inputField.text.Length;
//返回阿尔法
originalTextColor.a=1f;
//使用alpha应用新的选择颜色
inputField.selectionColor=原始文本颜色;
if(inputField.touchScreenKeyboard.canSetSelection)
inputField.touchScreenKeyboard.selection=新范围int(0,0);
}
}

如果您正在撰写关于本机屏幕键盘的文章,您可以尝试:

RangeInt的参数是选择的起始位置和长度


请记住,并非所有平台都支持这一点。有关所有触摸屏主板功能,请参阅文档和。

非常感谢您的评论。你知道你的解决方案将如何插入我在我的文章链接到的代码中吗?谢谢:)在OnSelect方法中放置此代码,而不是start startcroutine。如果这对你不起作用,让我知道,我会自己试试:)谢谢@Dave。我把代码放在你说的地方,但我得到一个错误,说“TouchScreenKeyboard不包含“canSetSelection”的定义?我可以看到此选项在Unity 2018及更高版本中。你们有旧版本吗?好的,我在Unity 2019中打开了它。我现在得到了以下错误:非静态字段、方法或属性“TouchScreenKeyboard.canSetSelection”需要对象引用。它工作正常Dave!!!!非常感谢你!!!我会将你的帖子标记为答案,并奖励你50+声望点!!!非常感谢。
if (inputField.touchScreenKeyboard != null && inputField.touchScreenKeyboard.active && inputField.touchScreenKeyboard.canSetSelection)
    inputField.touchScreenKeyboard.selection = new RangeInt(0, 0);