Cocos3.4.2版本 获取手指滑动到的区域的Label文本并记录文本内容进行判定操作

TypeScript 复制代码
import { _decorator, Component, Event, EventTouch, find, Input, input, Label, Node, Rect, sys, UITransform, Vec2, Vec3 } from 'cc';
import { JsonMgr } from '../Managers/JsonMgr';
import { strings } from '../Struct/GameStruct';
const { ccclass, property } = _decorator;

///用于获取手指滑动区域的文本并记录文本内容
@ccclass('Getstring')
export class Getstring extends Component {   
    label: Label;

    b:boolean=true;
    onLoad() {
        //name为需要找到的物体名称
        let a=Getstring.Search(this.node,"name");
        this.label=a.getComponent(Label);
        //this.label = this.node.getChildByName("name").getComponent(Label);
        input.on(Input.EventType.TOUCH_END, this.onTouchEnd, this);
        input.on(Input.EventType.TOUCH_MOVE, this.onTouchMove, this);
        //this.node.on(Node.EventType.TOUCH_END, this.onTouchEnd, this)
        JsonMgr.getInstance().SetJsonOne(strings.InputTxt,"");  //初始化数据
    }
    start() {

    }

    update(deltaTime: number) {
        //input.on(Input.EventType.TOUCH_START, this.onTouchEnd, this);
    }


    onTouchEnd(event: EventTouch) {     
        this.b=true;      
        //JsonMgr.getInstance().SetJsonOne(strings.InputTxt,"");    //清空数据
        //判断内容
    }

    onTouchMove(event: EventTouch) {
        const touchPos = event.getLocation();
        const localPos = this.node.getComponent(UITransform).convertToNodeSpaceAR(new Vec3(touchPos.x, touchPos.y));
        const size = this.node.getComponent(UITransform).contentSize;
        const rect = new Rect(-size.width / 2, -size.height / 2, size.width, size.height);
        //console.log("位置更新:"+localPos);
        if (rect.contains(new Vec2(localPos.x, localPos.y))) {
            const text = this.label.string;
            if (this.b) {
                // 在这里操作获取到的文本内容                
                JsonMgr.getInstance().SetJsonOne(strings.InputTxt,sys.localStorage.getItem(strings.InputTxt) + text);//字符串连接
                this.b=false;
                let te=sys.localStorage.getItem(strings.InputTxt);  //字符串赋值
                console.log(te);
            }

        }
    }
    //递归获取子物体
    public static Search(targetNode: Node, name: string): Node {
        if (targetNode.name == name) {
            return targetNode;
        }

        for (let i = 0; i < targetNode.children.length; i++) {
            const child = targetNode.children[i];
            let result = this.Search(child, name);
            if (result != null) {
                return result;
            }
        }

        return null;
    }
}

注:可以获取到多个挂载到此脚本的文本内容

例如:每个框都挂载这个脚本时,通过拖动即可获取到其内容

相关推荐
千寻girling6 小时前
计算机组成原理-全通关源码-实验(通关版)---头歌平台
前端·面试·职场和发展·typescript·node.js
沉默金鱼12 小时前
Unity实用技能-模型
unity·游戏引擎
阿里云云原生13 小时前
AgentRun:如何利用 AI Agent 构建现代化的舆情分析解决方案?
人工智能·unity·游戏引擎
老朱佩琪!14 小时前
Unity代理模式
unity·游戏引擎·代理模式
yanghuashuiyue14 小时前
Vue3难以统一的命名规范
前端·vue.js·typescript
技术小甜甜15 小时前
【Godot】【入门】GDScript 快速上手(只讲游戏里最常用的 20% 语法)
android·游戏·编辑器·游戏引擎·godot
老朱佩琪!18 小时前
Unity命令模式
unity·游戏引擎·命令模式
世洋Blog18 小时前
Unity编辑器基础
unity·c#·编辑器·游戏引擎
WarPigs19 小时前
Unity NetCode for GameObject笔记
笔记·unity·游戏引擎
龙智DevSecOps解决方案21 小时前
Perforce《2025游戏技术现状报告》Part 5:创意工作者在用什么工具以及如何看待游戏引擎与生成式AI(附免费下载)
游戏引擎·游戏开发·软件开发·perforce·ai创作·龙智