ar_tourism_flutter_unity/unity/VRProject2/Assets/zhl/zhlScripts/fanYe.cs
2025-05-14 17:04:13 +08:00

173 lines
4.2 KiB
C#

using System.Collections;
using System.Collections.Generic;
//using QFramework;
using UnityEngine;
using UnityEngine.UI;
public class fanYe : MonoBehaviour
{
public Text text;
private float StartMouseLocationX;
private float EndMouseLocationX;
public GameObject xiHuan;
public GameObject xiangQing;
#region
private void OnMouseDown()
{
if (Input.GetMouseButtonDown(0))
{
Debug.Log("鼠标按下");
StartMouseLocationX = Input.mousePosition.x;
//避免在拖动过程中产生的位置变化
EndMouseLocationX = StartMouseLocationX;
}
}
#endregion
#region
private void OnMouseUp()
{
if (Input.GetMouseButtonUp(0))
{
Debug.Log("鼠标抬起");
EndMouseLocationX = Input.mousePosition.x;
}
}
#endregion
#region
private bool IfDrag()
{
if (MouseMoveDistitens(StartMouseLocationX, EndMouseLocationX) < 200)
{
return false;
}
else
{
return true;
}
}
#endregion
#region
private float MouseMoveDistitens(float Startx, float Endx)
{
return Mathf.Sqrt((Startx - Endx) * (Startx - Endx));
}
#endregion
#region
private string MoveDirection()
{
if (EndMouseLocationX - StartMouseLocationX > 0)
{
DataZeroing();
return "Right";
}
else
{
DataZeroing();
return "Left";
}
}
#endregion
#region
private void DataZeroing()
{
StartMouseLocationX = 0;
EndMouseLocationX = 0;
}
#endregion
void Update()
{
Turn();
CantLookButton();
OnMouseDown();
OnMouseUp();
if (IfDrag())
{
if (transform.GetChild(0).childCount != 0)
{
if (transform.GetChild(0).GetChild(0).name.Contains("shiPinPlane"))
{
MyPlane myplane = transform.GetChild(0).GetChild(0).GetComponent<MyPlane>();
if (MoveDirection() == "Left")
{
myplane.Left();
}
else
{
myplane.Right();
Debug.Log("IsRight");
}
}
}
else if (transform.GetChild(1).childCount != 0)
{
if (transform.GetChild(1).GetChild(0).name.Contains("shiPinPlane"))
{
MyPlane myplane = transform.GetChild(1).GetChild(0).GetComponent<MyPlane>();
if (MoveDirection() == "Left")
{
myplane.Left();
}
else
{
myplane.Right();
Debug.Log("IsRight");
}
}
Debug.Log(MoveDirection());
}
}
if(text != null)
{
Vector3 position = transform.position;
text.text = $"X:{position.x:F2}\nY:{position.y:F2}\nZ:{position.z:F2}";
}
}
public void CantLookButton()
{
if (transform.GetChild(0).childCount != 0 || transform.GetChild(1).childCount != 0)
{
xiHuan.SetActive(false);
xiangQing.SetActive(false);
}
else
{
xiHuan.SetActive(true);
xiangQing.SetActive(true);
}
}
public void Turn()
{
if (Input.GetKey(KeyCode.A))
{
transform.Rotate(Vector3.up, -30f * Time.deltaTime);
}
else if (Input.GetKey(KeyCode.D))
{
transform.Rotate(Vector3.up, 30f * Time.deltaTime);
}
else if (Input.GetKey(KeyCode.W))
{
transform.Rotate(Vector3.right, -30f * Time.deltaTime);
}
else if (Input.GetKey(KeyCode.S))
{
transform.Rotate(Vector3.right, 30f * Time.deltaTime);
}
}
}