176 lines
5.3 KiB
C#
176 lines
5.3 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using Unity.VisualScripting;
|
||
using UnityEngine;
|
||
using UnityEngine.EventSystems;
|
||
|
||
public class PlayerMain : MonoBehaviour
|
||
{
|
||
private static PlayerMain instance;
|
||
public static PlayerMain Instance => instance;
|
||
private PetObject petObj;
|
||
public Transform obj;
|
||
|
||
//是否开始点击
|
||
public bool isStart=true;
|
||
//技能2开启
|
||
public bool isSkillTwoStart = false;
|
||
|
||
// Start is called before the first frame update
|
||
void Awake()
|
||
{
|
||
instance = this;
|
||
}
|
||
|
||
void Start()
|
||
{
|
||
GameMgr.Instance.init();
|
||
ScreenAspect();
|
||
}
|
||
|
||
// Update is called once per frame
|
||
void Update()
|
||
{
|
||
#if UNITY_EDITOR
|
||
// 检查鼠标指针是否在 UI 元素上
|
||
if (EventSystem.current.IsPointerOverGameObject())
|
||
{
|
||
// 如果在 UI 元素上,直接返回
|
||
return;
|
||
}
|
||
//鼠标左键点击
|
||
if (Input.GetMouseButtonDown(0))
|
||
{
|
||
//创建射线
|
||
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
|
||
RaycastHit2D hit =
|
||
Physics2D.Raycast(ray.origin, ray.direction, 1000, 1 << LayerMask.NameToLayer("PetObject"));
|
||
if (hit.collider != null)
|
||
{
|
||
petObj = hit.collider.gameObject.GetComponent<PetObject>();
|
||
if (isStart)
|
||
{
|
||
GameMgr.Instance.IsHightLight(petObj);
|
||
}
|
||
else if (isSkillTwoStart)
|
||
{
|
||
GameMgr.Instance.SkillTwoDelPet(petObj);
|
||
}
|
||
}
|
||
}
|
||
|
||
if (Input.GetMouseButtonUp(0) && isStart)
|
||
{
|
||
//创建射线
|
||
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
|
||
RaycastHit2D hit =
|
||
Physics2D.Raycast(ray.origin, ray.direction, 1000, 1 << LayerMask.NameToLayer("PetObject"));
|
||
|
||
|
||
if (hit.collider == null || hit.collider.gameObject.GetComponent<PetObject>().isHightLight == false)
|
||
{
|
||
GameMgr.Instance.CancelHightLight();
|
||
}
|
||
else
|
||
{
|
||
GameMgr.Instance.DelHightLight();
|
||
}
|
||
}
|
||
//技能2检测游戏对象
|
||
if (Input.GetMouseButtonDown(0) && isSkillTwoStart)
|
||
{
|
||
//创建射线
|
||
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
|
||
RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction, 1000, 1 << LayerMask.NameToLayer("PetObject"));
|
||
|
||
// 射线检测
|
||
if (hit.collider != null)
|
||
{
|
||
//选中的目标
|
||
PetObject pet = hit.collider.GetComponent<PetObject>();
|
||
GameMgr.Instance.SkillTwoDelPet(pet);
|
||
}
|
||
|
||
}
|
||
|
||
#elif UNITY_ANDROID || UNITY_IOS
|
||
//检测触摸输入
|
||
if (Input.touchCount > 0)
|
||
{
|
||
Touch touch = Input.GetTouch(0);
|
||
|
||
//手指按下
|
||
if (touch.phase == TouchPhase.Began)
|
||
{
|
||
if (!IsPointerOverUI(touch))
|
||
{
|
||
//创建射线
|
||
Ray ray = Camera.main.ScreenPointToRay(touch.position);
|
||
RaycastHit2D hit =
|
||
Physics2D.Raycast(ray.origin, ray.direction, 1000, 1 << LayerMask.NameToLayer("PetObject"));
|
||
|
||
if (hit.collider != null)
|
||
{
|
||
petObj = hit.collider.gameObject.GetComponent<PetObject>();
|
||
if (isStart)
|
||
{
|
||
GameMgr.Instance.IsHightLight(petObj);
|
||
}
|
||
else if (isSkillTwoStart)
|
||
{
|
||
GameMgr.Instance.SkillTwoDelPet(petObj);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
if (touch.phase == TouchPhase.Ended)
|
||
{
|
||
//创建射线
|
||
Ray ray = Camera.main.ScreenPointToRay(touch.position);
|
||
RaycastHit2D hit =
|
||
Physics2D.Raycast(ray.origin, ray.direction, 1000, 1 << LayerMask.NameToLayer("PetObject"));
|
||
if (hit.collider == null || hit.collider.gameObject.GetComponent<PetObject>().isHightLight == false)
|
||
{
|
||
GameMgr.Instance.CancelHightLight();
|
||
}
|
||
else
|
||
{
|
||
GameMgr.Instance.DelHightLight();
|
||
}
|
||
}
|
||
}
|
||
#endif
|
||
|
||
}
|
||
|
||
private bool IsPointerOverUI(Touch touch)
|
||
{
|
||
PointerEventData pointerEventData = new PointerEventData(EventSystem.current)
|
||
{
|
||
position = touch.position
|
||
};
|
||
|
||
// 检测UI元素
|
||
List<RaycastResult> results = new List<RaycastResult>();
|
||
EventSystem.current.RaycastAll(pointerEventData, results);
|
||
|
||
return results.Count > 0; // 如果检测到UI元素,则返回true
|
||
}
|
||
|
||
private void ScreenAspect()
|
||
{
|
||
// 获取相机组件
|
||
Camera cam = GetComponent<Camera>();
|
||
// 计算当前宽高比
|
||
float f1 = (float)Screen.width / (float)Screen.height;
|
||
// 计算参考宽高比
|
||
float f2 = 1080f / 1920f;
|
||
// 根据屏幕宽高比调整相机的正交大小
|
||
cam.orthographicSize = (f1 / f2) * 9.5f;
|
||
|
||
obj.localScale = new Vector3(f1 / f2*obj.localScale.x, f1 / f2*obj.localScale.y, obj.localScale.z);
|
||
}
|
||
}
|
||
|