using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test_Img : MonoBehaviour
{
public Transform tf_DirTarger;//测算距离的目标
public Vector3 my_Size;//自身size
private Vector3 my_OldSize;//自身原始size
public float f_Dir; //距离
private Vector3 vMovePos,mousePosOnScreen,mousePosInWorld;//存储位置用
// Start is called before the first frame update
void Start()
{
my_Size = GetComponent<RectTransform>().sizeDelta;
my_OldSize = my_Size;
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
//让测算距离的目标 跟随光标
vMovePos =DoctorMng1.instance.UICamera .WorldToScreenPoint(tf_DirTarger.position);
//获取光标In Scence位置
mousePosOnScreen = Input.mousePosition;
//让鼠标坐标的Z= 物体的坐标Z
mousePosOnScreen.z = vMovePos.z;
//将光标Scence位置 转换为世界坐标
mousePosInWorld = DoctorMng1.instance.UICamera .ScreenToWorldPoint(mousePosOnScreen);
//物体跟随鼠标移动
tf_DirTarger.position = mousePosInWorld;
f_Dir = (transform.position - mousePosInWorld).magnitude;
my_Size = my_OldSize;
my_Size.x = my_Size.x + f_Dir;
GetComponent<RectTransform>().sizeDelta = my_Size;
}
}
}
unity骚操作: 让UGUI RectTransform.SizeDelta根据鼠标点击的距离长度而延长
最新推荐文章于 2023-04-20 10:36:01 发布