Unity中实现获取InputField选中的文字

一:前言

获取到选中的文字:哈哈


二:实现

UGUI的InputField提供了selectionAnchorPosition和selectionFocusPosition,开始选择时的光标下标和当前光标下标

cs 复制代码
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);
        }
    }
}
相关推荐
Jet_582 小时前
神庙逃亡(Temple Run)IL2CPP 逆向实战:从 APK 到 Frida 实现角色无敌
unity·il2cpp·逆向工程·frida·android逆向·hook技术·游戏逆向
Dr.勿忘15 小时前
MUMU模拟器adb连接失败:cannot connect to 127.0.0.1:16384: 由于目标计算机积极拒绝,无法连接。 (10061)
游戏·unity·adb·游戏程序·调试·模拟器
垂葛酒肝汤1 天前
Unity中的协程的原理
unity·游戏引擎
垂葛酒肝汤1 天前
Unity第一个项目
unity·游戏引擎
Sator11 天前
Unity的InputSystem常见问题和疑惑解答
java·unity·游戏引擎
郝学胜-神的一滴1 天前
QtOpenGL多线程渲染方案深度解析
c++·qt·unity·游戏引擎·godot·图形渲染·unreal engine
IMPYLH1 天前
Lua 的 Table 模块
开发语言·笔记·后端·junit·游戏引擎·lua
Howrun7772 天前
虚幻引擎_控制角色移动的三种方法
游戏引擎·虚幻
速冻鱼Kiel2 天前
GASP笔记01
笔记·ue5·游戏引擎·虚幻
孟无岐2 天前
【Laya】Animator2D 使用指南
typescript·游戏引擎·游戏程序·laya