104 lines
2.9 KiB
C#
104 lines
2.9 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class Controller : MonoBehaviour
|
|
{
|
|
/// <summary>
|
|
/// 提升机模型
|
|
/// </summary>
|
|
public GameObject hoister;
|
|
/// <summary>
|
|
/// 小车模型
|
|
/// </summary>
|
|
public GameObject trolley;
|
|
public Button but;
|
|
private Camera mainCamera;
|
|
private bool isDragging = false;
|
|
private Transform objectToDrag;
|
|
private Vector3 offset;
|
|
void Start()
|
|
{
|
|
//mainCamera = Camera.main;
|
|
//string str = "提升机";
|
|
//but.onClick.AddListener(() =>
|
|
//{
|
|
// Displaymodel(str);
|
|
//});
|
|
}
|
|
|
|
public void Displaymodel(string data)
|
|
{
|
|
if (data == "小车")
|
|
{
|
|
hoister.SetActive(false);
|
|
trolley.SetActive(true);
|
|
}
|
|
else if(data == "提升机")
|
|
{
|
|
trolley.SetActive(false);
|
|
hoister.SetActive(true);
|
|
}
|
|
else
|
|
{
|
|
Debug.Log("错误参数");
|
|
}
|
|
}
|
|
void Update()
|
|
{
|
|
////// 检测鼠标左键按下
|
|
//if (Input.GetMouseButtonDown(1))
|
|
//{
|
|
// Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
|
|
// RaycastHit hit;
|
|
|
|
// // 射线检测物体
|
|
// if (Physics.Raycast(ray, out hit))
|
|
// {
|
|
// if (hit.collider.CompareTag("Car"))
|
|
// {
|
|
// // 检测到的物体是我们想拖动的物体
|
|
// objectToDrag = hit.transform;
|
|
// isDragging = true;
|
|
// // 计算鼠标点击位置与物体中心的偏移
|
|
// offset = objectToDrag.position - GetMouseWorldPosition();
|
|
// }
|
|
// if (hit.collider.CompareTag("Hoister"))
|
|
// {
|
|
// // 检测到的物体是我们想拖动的物体
|
|
// objectToDrag = hit.transform;
|
|
// isDragging = true;
|
|
// // 计算鼠标点击位置与物体中心的偏移
|
|
|
|
// offset = objectToDrag.position - GetMouseWorldPosition();
|
|
// }
|
|
// }
|
|
//}
|
|
|
|
//// 检测鼠标左键释放
|
|
//if (Input.GetMouseButtonUp(1))
|
|
//{
|
|
// isDragging = false;
|
|
// objectToDrag = null;
|
|
//}
|
|
//Vector3 mousePosition = Input.mousePosition;
|
|
//// 如果正在拖动物体,则更新物体位置
|
|
//if (isDragging && objectToDrag != null&& mousePosition.x >= 0 && mousePosition.x <= Screen.width &&
|
|
// mousePosition.y >= 0 && mousePosition.y <= Screen.height)
|
|
//{
|
|
// objectToDrag.position = GetMouseWorldPosition() + offset;
|
|
//}
|
|
|
|
}
|
|
|
|
Vector3 GetMouseWorldPosition()
|
|
{
|
|
// 获取鼠标在世界空间中的位置
|
|
Vector3 mouseScreenPosition = Input.mousePosition;
|
|
mouseScreenPosition.z = mainCamera.WorldToScreenPoint(objectToDrag.position).z;
|
|
return mainCamera.ScreenToWorldPoint(mouseScreenPosition);
|
|
}
|
|
|
|
}
|