Unity 3D

来源:互联网 发布:国际象棋对弈软件下载 编辑:程序博客网 时间:2024/06/04 17:56

Unity 3D - UGUI 显示道具信息 :

显示效果 :

字符串排版处理 - 链接

这里写图片描述

Demo 实现效果 :

鼠标放到道具上 , 会显示出图中提示信息 , 提示信息面板会跟随鼠标移动 , 鼠标离开道具时 , 提示信息面板会自动隐藏 .

实现过程 :

先在道具脚本上继承接口 IPointerEnterHandler 和 IPointerExitHandler .
在鼠标移动到道具上时 , 通过委派调用 OnEnter 方法 , 传入transform .

C# 代码 :

using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.EventSystems;using System;public class GridUI : MonoBehaviour, IPointerEnterHandler,IPointerExitHandler{    public static Action<Transform> OnEnter;    public static Action OnExit;    public void OnPointerEnter(PointerEventData eventData)    {        if (OnEnter != null)         {            OnEnter (transform);        }    }    public void OnPointerExit(PointerEventData eventData)    {        if (OnExit != null)         {            OnExit ();        }    }}

Manager类处理 .

//先添加回调函数void Awake(){    GridUI.OnEnter += GridUI_OnEnte;    GridUI.OnExit += GridUI_OnExit;}public void GridUI_OnEnte(Transform gridTransfrom){    //获取道具信息    Item item = ItemModel.GetItem (gridTransfrom.name);    //通过道具信息得到需要显示的字符串    string str = GetItemInfoStr (item);    //显示提示框    isShow = true;    itemtip.gameObject.SetActive (true);    //用字符串刷新提示框里文本信息    itemtip.refreshInfo (str);}public void GridUI_OnExit(){    //隐藏提示框    isShow = false;    itemtip.gameObject.SetActive (false);}

然后在Update中 , 让提示框跟随移动 .

void Update (){    //如果处理显示状态    if (isShow == true)     {        //获取鼠标位置        Vector2 position;        RectTransform rect = GameObject.Find ("Canvas").transform as RectTransform;        Vector2 point = Input.mousePosition;        RectTransformUtility.ScreenPointToLocalPointInRectangle (rect, point, null, out position);        //设置提示框位置        itemtip.transform.localPosition = position;    }}
1 0
原创粉丝点击