本文共 1918 字,大约阅读时间需要 6 分钟。
这个吧,纠结了我一段时间。 下面直接上脚本吧。
using UnityEngine;using UnityEngine.UI;using UnityEngine.EventSystems;using System.Collections;using System.Collections.Generic;public class UICameraTouchController : MonoBehaviour, IDragHandler, IPointerDownHandler, IPointerUpHandler{ public CameraController _camera;// 相机 private RectTransform rectTransform;// 要是一个 全屏大小的image private Dictionarytouches = new Dictionary (); private void Awake() { rectTransform = this.GetComponent (); } public void OnPointerDown(PointerEventData eventData) { touches[eventData.pointerId] = eventData.position; } public void OnPointerUp(PointerEventData eventData) { if (_camera != null) { _camera.EndDrag(); } touches.Remove(eventData.pointerId); } public void OnDrag(PointerEventData eventData) { if(touches.Count == 1) { touches[eventData.pointerId] = eventData.position; Vector2 delta = new Vector2(eventData.delta.x / rectTransform.rect.width * 180.0f, eventData.delta.y / rectTransform.rect.height * -90.0f); if (_camera != null ) { _camera.RotateCamera(delta); } } }}
using System.Collections;using UnityEngine;public class CameraController : MonoBehaviour{ public Transform _cameraContainer;// 相机 void Awake() { _cameraContainer = ObjectCommon.GetChildComponent(gameObject, "CameraContainer"); } void Clear() { IsDrag = false; } void Update() { } bool IsDrag = true;// 是否在拖拽中 public void EndDrag() { IsDrag = false; } public void RotateCamera(Vector2 delta) { IsDrag = true; float y = this._cameraContainer.localEulerAngles.y + delta.x; UITools.SetTransformRotation(this._cameraContainer,new Vector3 (0,y,0)); }}
转载地址:http://leszb.baihongyu.com/