137 lines
4.7 KiB
C#
137 lines
4.7 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
using UnityEngine.UI;
|
|
|
|
public class GameMainPanel_XJMH : UIBase
|
|
{
|
|
bool isOpenSetting;
|
|
public Transform[] tra_SettingListPos;
|
|
public Transform[] tra_SettingListComponent;
|
|
public Button btn_SettingList;
|
|
public GameObject obj_CurrTurret;
|
|
public void Start()
|
|
{
|
|
btn_SettingList.onClick.AddListener(() =>
|
|
{
|
|
if (isOpenSetting)
|
|
{
|
|
StartCoroutine(CloseSettingList());
|
|
|
|
}
|
|
else
|
|
{
|
|
StartCoroutine(OpenSettingList());
|
|
}
|
|
});
|
|
}
|
|
#region 打开关闭右上角设置选项
|
|
private IEnumerator OpenSettingList()
|
|
{
|
|
tra_SettingListComponent[tra_SettingListComponent.Length - 1].localScale = Vector3.one + Vector3.left * 2;
|
|
isOpenSetting = true;
|
|
int n = 0;
|
|
while (true)
|
|
{
|
|
if (isOpenSetting == false)
|
|
{
|
|
yield break;
|
|
}
|
|
yield return null;
|
|
for (int i = 0; i < tra_SettingListPos.Length; i++)
|
|
{
|
|
tra_SettingListComponent[i].position = Vector3.Lerp(tra_SettingListComponent[i].position, tra_SettingListPos[i].position, 0.5f);
|
|
if (tra_SettingListComponent[i].position == tra_SettingListPos[i].position)
|
|
{
|
|
n++;
|
|
if (n == tra_SettingListPos.Length)
|
|
{
|
|
yield break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
private IEnumerator CloseSettingList()
|
|
{
|
|
tra_SettingListComponent[tra_SettingListComponent.Length - 1].localScale = Vector3.one;
|
|
isOpenSetting = false;
|
|
int n = 0;
|
|
while (true)
|
|
{
|
|
if (isOpenSetting)
|
|
{
|
|
yield break;
|
|
}
|
|
yield return null;
|
|
for (int i = 0; i < tra_SettingListPos.Length; i++)
|
|
{
|
|
tra_SettingListComponent[i].position = Vector3.Lerp(tra_SettingListComponent[i].position, tra_SettingListPos[tra_SettingListPos.Length - 1].position, 0.5f);
|
|
if (tra_SettingListComponent[i].position == tra_SettingListPos[i].position)
|
|
{
|
|
n++;
|
|
if (n== tra_SettingListPos.Length)
|
|
{
|
|
yield break;
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
public float attackTime=0.5f;
|
|
float clickTime = 0;
|
|
public GameObject bullet_Test;
|
|
public GameObject bulletParent;
|
|
public void Update()
|
|
{
|
|
if (Touchscreen.current?.primaryTouch != null)
|
|
{
|
|
if (Touchscreen.current.primaryTouch.press.wasPressedThisFrame)
|
|
{
|
|
clickTime = attackTime;
|
|
}
|
|
if (Touchscreen.current.primaryTouch.press.isPressed)
|
|
{
|
|
// 获取触摸位置(屏幕坐标)
|
|
Vector2 touchPos = Touchscreen.current.primaryTouch.position.ReadValue();
|
|
// 转换Canvas坐标
|
|
Vector2 worldPos;
|
|
RectTransformUtility.ScreenPointToLocalPointInRectangle(obj_CurrTurret.transform.parent as RectTransform, touchPos, null, out worldPos);
|
|
|
|
// 计算方向
|
|
Vector2 dir = worldPos - obj_CurrTurret.GetComponent<RectTransform>().anchoredPosition;
|
|
// 计算角度
|
|
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg - 90f;
|
|
obj_CurrTurret.transform.rotation = Quaternion.Euler(Vector3.forward * angle);
|
|
clickTime += Time.deltaTime;
|
|
if (clickTime>attackTime)
|
|
{
|
|
clickTime = 0;
|
|
//计算方向
|
|
Vector2 startV2;
|
|
startV2 = MonsterMoveLine.AnchoredPos_ToCanvas(obj_CurrTurret.transform.GetChild(1).GetComponent<RectTransform>(), UIManager.Instance.uiRoot.GetComponent<Canvas>());
|
|
startV2 = MonsterMoveLine.CanvasToWorld(startV2, UIManager.Instance.uiRoot.GetComponent<RectTransform>(),Camera.main);
|
|
|
|
//Vector2 endV2 = touchPos;
|
|
//var vectorTarget = endV2 - startV2;
|
|
//var a = Mathf.Atan2(vectorTarget.y, vectorTarget.x) * Mathf.Rad2Deg;
|
|
Instantiate(bullet_Test, startV2,Quaternion.Euler(Vector3.forward * angle),bulletParent.transform).GetComponent<BullitMove>().playerID=GameManager.Instance.gameData.currUser;
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|