unity中 UI物体跟随鼠标移动,根据鼠标滑动方向改变物体方向在X轴。 移动端同样实现

using UnityEngine;

public class FollowMouseHorizontal : MonoBehaviour

{

private RectTransform rectTransform;

private Vector2 lastPosition;

public float speed = 5.0f;

void Start()

{

rectTransform = GetComponent<RectTransform>();

lastPosition = rectTransform.anchoredPosition;

}

void Update()

{

#if UNITY_EDITOR || UNITY_STANDALONE

HandleMouseInput();

#elif UNITY_IOS || UNITY_ANDROID

HandleTouchInput();

#endif

}

void HandleMouseInput()

{

Vector2 currentMousePosition = Input.mousePosition;

Debug.Log(Input.mousePosition);

MoveTowards(new Vector2(currentMousePosition.x, rectTransform.position.y));

}

void HandleTouchInput()

{

if (Input.touchCount > 0)

{

Touch touch = Input.GetTouch(0);

Vector2 currentTouchPosition = touch.position;

MoveTowards(new Vector2(currentTouchPosition.x, rectTransform.position.y));

}

}

void MoveTowards(Vector2 targetPosition)

{

Vector2 direction = targetPosition - (Vector2)rectTransform.position;

// 平滑移动

rectTransform.position = Vector2.Lerp(rectTransform.position, targetPosition, Time.deltaTime * speed);

// 改变UI物体的左右方向

if (direction.x > 0)

{

rectTransform.localScale = new Vector3(1, 1, 1); // 面向右

}

else if (direction.x < 0)

{

rectTransform.localScale = new Vector3(-1, 1, 1); // 面向左

}

}

}

Canvas 的 Render Mode 是Screen Space - Camera:

using UnityEngine;

public class FollowMouseHorizontalCmaera : MonoBehaviour

{

private RectTransform rectTransform;

public Camera uiCamera;

public float speed = 5.0f;

void Start()

{

rectTransform = GetComponent<RectTransform>();

}

void Update()

{

#if UNITY_EDITOR || UNITY_STANDALONE

HandleMouseInput();

#elif UNITY_IOS || UNITY_ANDROID

HandleTouchInput();

#endif

}

void HandleMouseInput()

{

Vector3 currentMousePosition = Input.mousePosition;

currentMousePosition.z = uiCamera.nearClipPlane; // 设置 z 值为相机的近剪切面

Vector3 worldMousePosition = uiCamera.ScreenToWorldPoint(currentMousePosition);

MoveTowards(new Vector2(worldMousePosition.x, rectTransform.position.y));

}

void HandleTouchInput()

{

if (Input.touchCount > 0)

{

Touch touch = Input.GetTouch(0);

Vector3 currentTouchPosition = touch.position;

currentTouchPosition.z = uiCamera.nearClipPlane; // 设置 z 值为相机的近剪切面

Vector3 worldTouchPosition = uiCamera.ScreenToWorldPoint(currentTouchPosition);

MoveTowards(new Vector2(worldTouchPosition.x, rectTransform.position.y));

}

}

void MoveTowards(Vector2 targetPosition)

{

Vector2 direction = targetPosition - (Vector2)rectTransform.position;

// 平滑移动

rectTransform.position = Vector2.Lerp(rectTransform.position, targetPosition, Time.deltaTime * speed);

// 改变UI物体的左右方向

if (direction.x > 0)

{

rectTransform.localScale = new Vector3(1, 1, 1); // 面向右

}

else if (direction.x < 0)

{

rectTransform.localScale = new Vector3(-1, 1, 1); // 面向左

}

}

}

相关推荐
Xing20174 小时前
unity打包ios Xcode问题记录
unity·游戏引擎
MasterNeverDown7 小时前
WPF 使用iconfont
hadoop·ui·wpf
学游戏开发的8 小时前
UE求职Demo开发日志#8 强化前置条件完善,给物品加图标
游戏引擎
不惑_21 小时前
深度学习 · 手撕 DeepLearning4J ,用Java实现手写数字识别 (附UI效果展示)
java·深度学习·ui
墨笺染尘缘1 天前
Unity——鼠标是否在某个圆形Image范围内
unity·c#·游戏引擎
Хайде1 天前
Qt Desiogn生成的ui文件转化为h文件
ui
Thomas_YXQ1 天前
Unity3D项目开发中的资源加密详解
游戏·3d·unity·unity3d·游戏开发
qq_428639611 天前
虚幻基础-1:cpu挑选(14600kf)
游戏引擎·虚幻
资深设备全生命周期管理1 天前
以Python 做服务器,N Robot 做客户端,小小UI,拿捏
服务器·python·ui
杀死一只知更鸟debug1 天前
Unity自学之旅05
unity·游戏引擎