|
|
using System.Collections;
|
|
|
using System.Collections.Generic;
|
|
|
using UnityEngine;
|
|
|
using UnityEngine.EventSystems;
|
|
|
|
|
|
|
|
|
public class CameraOp : MonoBehaviour, IBeginDragHandler, IDragHandler,IPointerDownHandler
|
|
|
{
|
|
|
public Transform tagertTrns;
|
|
|
public Camera mainCamera;
|
|
|
public float scaleSpeed = 0.5f;
|
|
|
public float rotateSpeed = 0.05f;
|
|
|
public float moveSpeed = 0.01f;
|
|
|
// Start is called before the first frame update
|
|
|
void Start()
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
// Update is called once per frame
|
|
|
void Update()
|
|
|
{
|
|
|
float offset = Input.GetAxis("Mouse ScrollWheel");
|
|
|
if (Input.GetAxis("Mouse ScrollWheel") != 0)
|
|
|
{
|
|
|
Debug.Log("ScrollWheel<0");
|
|
|
float scale = Input.GetAxis("Mouse ScrollWheel")*scaleSpeed;
|
|
|
// float z = mainCamera.transform.position.z + scale;
|
|
|
//z = Mathf.Clamp(z, 1.52f, 3.3f);
|
|
|
mainCamera.transform.position += mainCamera.transform.forward*scale;
|
|
|
//tagertTrns.transform.localScale = new Vector3(scale, scale, scale);
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private Vector2 startPostion;
|
|
|
public void OnBeginDrag(PointerEventData eventData)
|
|
|
{
|
|
|
startPostion = eventData.position;
|
|
|
}
|
|
|
|
|
|
public void OnDrag(PointerEventData eventData)
|
|
|
{
|
|
|
if (eventData.button == PointerEventData.InputButton.Left)
|
|
|
{
|
|
|
tagertTrns.Rotate(new Vector3(0, -eventData.delta.x, 0) * rotateSpeed, Space.World);
|
|
|
//mainCamera.transform.RotateAround(tagertTrns.position, Vector3.up, eventData.delta.x * rotateSpeed);
|
|
|
mainCamera.transform.RotateAround(tagertTrns.position, Vector3.right, -eventData.delta.y * rotateSpeed);
|
|
|
}
|
|
|
else if (eventData.button == PointerEventData.InputButton.Right)
|
|
|
{
|
|
|
mainCamera.transform.position += new Vector3(-eventData.delta.x, -eventData.delta.y, 0) * moveSpeed;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//data是鼠标在rawImage上交互的PointEventData数据
|
|
|
//rectTransform是RenderTexture所在的rawImage的RectTransform
|
|
|
//rtCamera是渲染3D模型的摄像机
|
|
|
//返回的obj就是轮胎,窗、门等物体
|
|
|
public GameObject GetHitObj(PointerEventData data)
|
|
|
{
|
|
|
GameObject obj = null;
|
|
|
var rect= this.gameObject.GetComponent<RectTransform>();
|
|
|
var pos = (data.position - (Vector2)rect.position) / rect.lossyScale - rect.rect.position;
|
|
|
|
|
|
var rate = pos / rect.rect.size;
|
|
|
var ray = mainCamera.ViewportPointToRay(rate);
|
|
|
RaycastHit raycastHit;
|
|
|
|
|
|
if (Physics.Raycast(ray, out raycastHit))
|
|
|
{
|
|
|
Debug.Log(raycastHit.transform.name);
|
|
|
obj = raycastHit.transform.gameObject;
|
|
|
}
|
|
|
Debug.DrawRay(mainCamera.transform.position, ray.direction,Color.red);
|
|
|
return obj;
|
|
|
|
|
|
}
|
|
|
|
|
|
public void OnPointerDown(PointerEventData eventData)
|
|
|
{
|
|
|
|
|
|
// GetHitObj(eventData);
|
|
|
}
|
|
|
}
|