赞
踩
目录
1. 在场景中新建一个按钮,这里使用的是Button(TextMeshPro),如果没有需要更新UNITY版本
2. 将Button的文字内容在这里改为123456以便测试(新建好以后默认的内容是Button)
本文是一个记录贴,在做unity项目中碰到的,记录一下
代码内容如下:
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.EventSystems;
- using TMPro;
-
- public class ButtonName : MonoBehaviour,IPointerClickHandler
- {
- // Start is called before the first frame update
- void Start()
- {
-
- }
-
- // Update is called once per frame
- void Update()
- {
-
- }
-
- public void OnPointerClick(PointerEventData eventData) {
- if (eventData.button == PointerEventData.InputButton.Left) {
- var name = this.GetComponentInChildren<TMP_Text>().text;
- Debug.Log(name);
- }
- }
- }

注意:
(1)头文件引用要加上using UnityEngine.EventSystems和using TMPro
(2)新建class处要加上继承IPointerClickHandler ,此时会报错,需要对应把下面那个方法写上,报错就会消失
(3)代码含义为,鼠标点击左键,获取按钮的子物体的TMP_Text的Component下的text并打印,这里经测试,需要是TMP_Text
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。