Unity UI跟随鼠标移动
挂在需要跟随鼠标的UI上(Image,Text等)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UIFollowMouse : MonoBehaviour
{/// <summary>/// 需要跟随的物体/// </summary>private GameObject go;private void Awake(){go = gameObject;}private void OnEnable(){ChoosePivot();go.transform.position = Input.mousePosition;}/// <summary>/// 根据鼠标位置选择中心点,避免出现UI到屏幕外的情况/// </summary>private void ChoosePivot(){float width = Screen.width / 2;float height = Screen.height / 2;if (Input.mousePosition.x < width){go.GetComponent<RectTransform>().pivot = new Vector2(0, go.GetComponent<RectTransform>().pivot.y);}else{go.GetComponent<RectTransform>().pivot = new Vector2(1, go.GetComponent<RectTransform>().pivot.y);}if (Input.mousePosition.y < height){go.GetComponent<RectTransform>().pivot = new Vector2(go.GetComponent<RectTransform>().pivot.x, 0);}else{go.GetComponent<RectTransform>().pivot = new Vector2(go.GetComponent<RectTransform>().pivot.x, 1);}}void Update(){ChoosePivot();go.transform.position = Input.mousePosition;}
}
Unity UI跟随鼠标移动相关推荐
- 魔兽UI跟随鼠标移动
算是自己给自己记录一下, 能看到的人用T也能实现 UI跟随鼠标移动,首先就是获取鼠标位置,DzAPI里面有三个获取鼠标位置函数: // 获取鼠标在游戏内的坐标X native DzGetMouseTe ...
- Unity——指针跟随鼠标旋转,GTA武器菜单选择等效果实现
GTA武器菜单选择 简单实现这种效果 Demo的效果 以下是主要代码 using System.Collections; using System.Collections.Generic; using ...
- unity的ui跟随鼠标移动
1.挂在需要跟随鼠标的UI上(Image,Text等) using System.Collections; using System.Collections.Generic; using UnityE ...
- unity ui界面 鼠标变图片移动
第一步 首先准备1张图(我随便截的)放入unity里,图片转换为Cursor; rso'r 第二步 创建一个Ui界面,在里面创一个按钮: 第三步 创建一个脚本,名字自取(脚本名为TU) sing Sy ...
- Unity UI跟随物体
图上是体力条跟随着主角,主角左移,体力条就跟着左移. 首先我们需要知道主角单位对于屏幕 PlayerScreen = Camera.main.WorldToScreenPoint(Player.tra ...
- unity UI 跟随3D物体移动
using System.Collections; using System.Collections.Generic; using UnityEngine;public class UIFollow3 ...
- unity摄像机跟随鼠标旋转
第一种 float vertical = Input.GetAxis("Mouse X") float horizontal = Input.GetAxis("Mouse ...
- unity 摄像机镜头跟随鼠标拖拽
通过update方法记录变化量然后通过坐标换算移动摄像机 一.获取上一帧到当前帧鼠标位移变化量,计算过相机从上一针位置到当前帧位置移动的距离 /// <summary> / ...
- unity 用四元素实现摄像机的跟随鼠标旋转,缩放
此文章中的摄像机是第三人称游戏摄像机,脚本挂载在游戏人物身上 用四元素实现的unity摄像机跟随鼠标旋转缩放,并且有障碍物遮挡的时候摄像机会自动往前移. float MouseZ = -(Input. ...
最新文章
- 第三章 搜索与图论 【完结】
- 图片和图形之性能和视图层次结构(18)
- 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型...
- Oracle数据库游标操作
- matlab大作业题题单,2011MATLAB大作业-题目-
- Android中的Handler的具体用法
- Python学习-第三天-面向对象编程基础
- python车牌识别系统开源代码_TensorFlow车牌识别完整版代码(含车牌数据集)
- 欧姆龙nb触摸屏通信_欧姆龙触摸屏 NB系列
- 产品更新背后的达维多定律
- 未转变者服务器买车指令,未转变者3.X服务器控制台命令大全_快吧单机游戏
- ios 表情符号 键盘_iOS自定义的emoji表情键盘
- CorelDRAW X8官方正版序列号如何安装使用?
- c语言手写平衡二叉树(一)
- 走火入魔的macfans,披着mac皮的ubuntu macbuntu安装指南
- 醉林疯的OJ 1055: 兔子繁殖问题
- Linux下Qt窗口半透明,Qt实现嵌入桌面的半透明窗口 good
- html点击图片弹出模态框,JS实现图片点击后出现模态框效果
- Newline required at end of file but not found
- 有个程序猿很忧桑:一个命令rm -rf/ ,他把整个公司删没了...