unity中用UGUI實現在UI界面鼠標旋轉三維物體
NGUI實現在UI界面下鼠標旋轉三維物體使用的脚本名為SpinWithMouse,然而UGUI中并沒有實現這個效果的方法,那麽該如何實現呢?
下面時相關的設置:
1.首先是Canvas的設置,目的是使攝像機能夠看到你的模型;
2.編寫脚本挂在Canvas上,脚本如下:
using UnityEngine;
using UnityEngine.EventSystems;
public class SpinWithMouse : MonoBehaviour, IDragHandler
{
public Transform target; //要旋轉的三維物體的Transform組件
public float speed = 1f;
void Start()
{
if (target == null) target = transform;
}
public void OnDrag(PointerEventData eventData)
{
target.localRotation = Quaternion.Euler(0f, -0.5f * eventData.delta.x * speed, 0f) * target.localRotation;
}
}
另一种实现方式(UGUI中没试过,但在场景3D物体可以用)
void OnMouseDrag()
{
axisX = -Input.GetAxis("Mouse X");
axisY = Input.GetAxis("Mouse Y");
}
void Update()
{
if (Input.GetMouseButton(0))
{
this.transform.Rotate(new Vector3(-axisY* RotateSpeed, axisX* RotateSpeed, 0) * 0.5f, Space.World);
}
float fov = Camera.main.fieldOfView;
fov+=Input.GetAxis("Mouse ScrollWheel")* Sensitive;
fov = Mathf.Clamp(fov,miniView,maxView);
Camera.main.fieldOfView = fov;
}