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;
|
|||
|
|
|||
|
//<2F>Ƿ<EFBFBD><C7B7><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
|
|||
|
public bool isStart=true;
|
|||
|
//<2F><><EFBFBD><EFBFBD>2<EFBFBD><32><EFBFBD><EFBFBD>
|
|||
|
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
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD> UI Ԫ<><D4AA><EFBFBD><EFBFBD>
|
|||
|
if (EventSystem.current.IsPointerOverGameObject())
|
|||
|
{
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> UI Ԫ<><D4AA><EFBFBD>ϣ<EFBFBD>ֱ<EFBFBD>ӷ<EFBFBD><D3B7><EFBFBD>
|
|||
|
return;
|
|||
|
}
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
if (Input.GetMouseButtonDown(0))
|
|||
|
{
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
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)
|
|||
|
{
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
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();
|
|||
|
}
|
|||
|
}
|
|||
|
//<2F><><EFBFBD><EFBFBD>2<EFBFBD><32><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϸ<EFBFBD><CFB7><EFBFBD><EFBFBD>
|
|||
|
if (Input.GetMouseButtonDown(0) && isSkillTwoStart)
|
|||
|
{
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
|
|||
|
RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction, 1000, 1 << LayerMask.NameToLayer("PetObject"));
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><DFBC><EFBFBD>
|
|||
|
if (hit.collider != null)
|
|||
|
{
|
|||
|
//ѡ<>е<EFBFBD>Ŀ<EFBFBD><C4BF>
|
|||
|
PetObject pet = hit.collider.GetComponent<PetObject>();
|
|||
|
GameMgr.Instance.SkillTwoDelPet(pet);
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
#elif UNITY_ANDROID || UNITY_IOS
|
|||
|
//<2F><><EFBFBD>ⴥ<EFBFBD><E2B4A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
if (Input.touchCount > 0)
|
|||
|
{
|
|||
|
Touch touch = Input.GetTouch(0);
|
|||
|
|
|||
|
//<2F><>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>
|
|||
|
if (touch.phase == TouchPhase.Began)
|
|||
|
{
|
|||
|
if (!IsPointerOverUI(touch))
|
|||
|
{
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
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)
|
|||
|
{
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
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
|
|||
|
};
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD>UIԪ<49><D4AA>
|
|||
|
List<RaycastResult> results = new List<RaycastResult>();
|
|||
|
EventSystem.current.RaycastAll(pointerEventData, results);
|
|||
|
|
|||
|
return results.Count > 0; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>UIԪ<49>أ<EFBFBD><D8A3><EFBFBD>true
|
|||
|
}
|
|||
|
|
|||
|
private void ScreenAspect()
|
|||
|
{
|
|||
|
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
Camera cam = GetComponent<Camera>();
|
|||
|
// <20><><EFBFBD>㵱ǰ<E3B5B1><C7B0><EFBFBD>߱<EFBFBD>
|
|||
|
float f1 = (float)Screen.width / (float)Screen.height;
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ο<EFBFBD><CEBF><EFBFBD><EFBFBD>߱<EFBFBD>
|
|||
|
float f2 = 1080f / 1920f;
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļ<EFBFBD><C4BB><EFBFBD>߱ȵ<DFB1><C8B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С
|
|||
|
cam.orthographicSize = (f1 / f2) * 9.5f;
|
|||
|
|
|||
|
obj.localScale = new Vector3(f1 / f2*obj.localScale.x, f1 / f2*obj.localScale.y, obj.localScale.z);
|
|||
|
}
|
|||
|
}
|
|||
|
|