一:前言
获取到选中的文字:哈哈
二:实现
UGUI的InputField提供了selectionAnchorPosition和selectionFocusPosition,开始选择时的光标下标和当前光标下标文章来源:https://www.toymoban.com/news/detail-663602.html
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using System;
public class GameInputField : InputField
{
private string m_SelectedText;//当前选中的文本
public string SelectedText
{
get
{
return m_SelectedText;
}
}
private int m_StartSelectedIndex;//起始选中的下标
public int StartSelectedIndex
{
get
{
return m_StartSelectedIndex;
}
}
private int m_EndSelectedIndex;//结束选中的下标
public int EndSelectedIndex
{
get
{
return m_EndSelectedIndex;
}
}
private void Update()
{
if (Input.GetMouseButtonDown(0) && EventSystem.current.currentSelectedGameObject == gameObject)
{
m_StartSelectedIndex = selectionAnchorPosition;
}
if (Input.GetMouseButtonUp(0) && EventSystem.current.currentSelectedGameObject == gameObject)
{
m_EndSelectedIndex = selectionFocusPosition;
if (m_EndSelectedIndex < m_StartSelectedIndex)
{
int temp = m_StartSelectedIndex;
m_StartSelectedIndex = m_EndSelectedIndex;
m_EndSelectedIndex = temp;
}
m_SelectedText = text.Substring(m_StartSelectedIndex, m_EndSelectedIndex - m_StartSelectedIndex);
}
}
}
文章来源地址https://www.toymoban.com/news/detail-663602.html
到了这里,关于Unity中实现获取InputField选中的文字的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!