unity2d实现两张图片,完成鼠标拖动图片正反面旋转的效果
时间: 2024-05-16 07:12:30 浏览: 16
以下是一种实现方法:
1. 创建一个空的 GameObject,将两张图片作为该 GameObject 的子物体,分别命名为 "front" 和 "back"。
2. 给 "back" 图片添加一个旋转组件,将其旋转 180 度,使其面朝背面。
3. 编写一个脚本,挂载在该 GameObject 上,实现鼠标拖动图片进行旋转的效果。
```csharp
public class CardController : MonoBehaviour
{
private bool isDragging = false;
private Vector3 startPosition;
private Vector3 startRotation;
private void OnMouseDown()
{
isDragging = true;
startPosition = Input.mousePosition;
startRotation = transform.rotation.eulerAngles;
}
private void OnMouseDrag()
{
if (isDragging)
{
Vector3 currentPosition = Input.mousePosition;
Vector3 delta = currentPosition - startPosition;
// 在 x 轴上旋转
float angle = delta.x * 0.5f;
transform.rotation = Quaternion.Euler(startRotation.x, startRotation.y + angle, startRotation.z);
}
}
private void OnMouseUp()
{
isDragging = false;
}
}
```
4. 将该脚本挂载在 GameObject 上,即可实现鼠标拖动图片进行旋转的效果。