173 lines
4.2 KiB
C#
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);
|
|
}
|
|
}
|
|
|
|
}
|
|
|