您当前的位置: 首页 >  苍狼王unity学院 unity

unity的ui跟随鼠标移动

苍狼王unity学院 发布时间:2022-09-14 09:45:30 ,浏览量:8

1、挂在需要跟随鼠标的UI上(Image,Text等)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UIFollowMouse : MonoBehaviour
{
    ///
    /// 需要跟随的物体
    ///
    private GameObject go;
    private void Awake()
    {
        go = gameObject;
    }
    private void OnEnable()
    {
        ChoosePivot();
        go.transform.position = Input.mousePosition;
    }
    ///
    /// 根据鼠标位置选择中心点,避免出现UI到屏幕外的情况
    ///
    private void ChoosePivot()
    {
        float width = Screen.width / 2;
        float height = Screen.height / 2;
        if (Input.mousePosition.x < width)
        {
         

关注
打赏
查看更多评论

苍狼王unity学院

暂无认证

  • 8浏览

    0关注

    252博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录