Cute_demon_attacks/meng_yao/Assets/script/A_Fight/CharacterClick.cs
2025-01-03 18:03:35 +08:00

168 lines
5.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using UnityEngine.EventSystems;
using System.Threading.Tasks;
public class CharacterClick : MonoBehaviour
{
public Role role;
public Character MyCharacter = null;
[Header("点击出现的详情")] public GameObject panelPrefab;
[Header("点击出现的攻击范围")] public GameObject attackRange;
[Header("是否已经显示面板")] public bool isShowPanel = false;
[Header("显示的信息面板")] public GameObject instantiatedPanel;
public Vector3 OrigScale;
public float StartSizeX;
public float StartSizeY;
public float OrSizeX;
public float OrSizeY;
private bool isShowAttackRange=true;
public WaveData MyWaveData=null;
void Start()
{
if (attackRange!=null)
{
OrigScale = attackRange.transform.localScale;
OrSizeX = attackRange.GetComponent<SpriteRenderer>().bounds.size.x;
OrSizeY = attackRange.GetComponent<SpriteRenderer>().bounds.size.y;
StartSizeX = OrSizeX;
StartSizeY = OrSizeY;
role.attackClass.SetAttackRange();
attackRange.transform.DOScale(new Vector2(OrSizeX, OrSizeY), 0.3f).SetEase(Ease.InOutBack);
//attackRange.transform.localScale=new Vector3(OrSizeX, OrSizeX,1);
//ShowAttackRange(UIContorl.instance.NowAttackRange);
}
if (transform.GetComponent<enemy>()!=null)
{
IdForInfo(transform.GetComponent<enemy>().enemyId);
}
}
private void OnMouseDown()
{
// 检查鼠标是否点击在UI元素上
if (EventSystem.current.IsPointerOverGameObject())
{
return; // 如果点击的是UI直接返回避免穿透到2D对象
}
// 确保父节点的碰撞体是 Trigger 类型
Collider2D parentCollider = GetComponent<Collider2D>();
if (parentCollider != null && parentCollider.isTrigger)
{
// parentCollider.enabled = true;
// 射线检测鼠标点击的物体是父节点
Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast(mousePosition, Vector2.zero);
if (hit.collider != null && hit.collider.gameObject == gameObject)
{
//ShowAttackRange();
Debug.Log("点击了父节点");
// 在这里处理点击事件
ShowInfoPanel();
}
}
}
public void ShowInfoPanel()
{
if (instantiatedPanel == null)
{
if (UIContorl.instance.NowShowInfo!=null)
{
Destroy(UIContorl.instance.NowShowInfo._closePanel);
HideAttackRange(UIContorl.instance.NowAttackRange);
}
instantiatedPanel = GameObject.Instantiate(panelPrefab, GameObject.Find("Canvas").transform);
UIContorl.instance.NowShowInfo = instantiatedPanel.GetComponent<charainfoPanel>();
UIContorl.instance.NowAttackRange = attackRange;
ShowAttackRange(UIContorl.instance.NowAttackRange);
instantiatedPanel.GetComponent<charainfoPanel>().closeBtn.onClick.AddListener(() => {
HideInfoPanel();
HideAttackRange(UIContorl.instance.NowAttackRange);
});
RectTransform panelRect = instantiatedPanel.GetComponent<RectTransform>();
// 确保生成的面板不会挡住父物体
panelRect.SetAsLastSibling(); // 将面板放到 Canvas 层级的最上层
if (role.camp == Camp.Enemy)
{
//string eveText,string name,string def,string speed,string shanghai,string info,int hp,int maxHp
panelRect.GetComponent<charainfoPanel>().UpDateShow("1", "Enemy_001", "0", "3", "1", "测试文档一", role.hp, role.maxHp);
panelRect.GetComponent<charainfoPanel>().role = role;
}
else
{
panelRect.GetComponent<charainfoPanel>().UpDateShow(role.Level.ToString(), role.Name, role.Info, role.MinAttack + "~" + role.MaxAttack, role.AttackRange.ToString(), role.AttackCD.ToString());
panelRect.GetComponent<charainfoPanel>().role = role;
}
}
else
{
//HideAttackRange();
}
}
public async void ShowAttackRange(GameObject attackRange)
{
if (attackRange!=null)
{
attackRange.transform.DOScale(new Vector2(OrSizeX, OrSizeY), 0.3f).SetEase(Ease.InOutBack);
}
}
public void HideAttackRange(GameObject attackRange)
{
if (attackRange != null )
{
attackRange.transform.DOScale(Vector3.zero, 0.3f).SetEase(Ease.InOutBack);
}
}
void IdForInfo(string id)
{
List<Character> mY = MengyaoInfo.Instance.m_Mengyao;
foreach (Character temp in mY)
{
if (temp.Id==id)
{
MyCharacter=temp;
break;
}
}
}
public void HideInfoPanel()
{
if (UIContorl.instance.NowShowInfo!=null)
{
UIContorl.instance.NowShowInfo.Id = -1;
UIContorl.instance.NowShowInfo.role = null;
if (instantiatedPanel != null && instantiatedPanel.GetComponent<charainfoPanel>()._closePanel != null)
{
Destroy(instantiatedPanel.GetComponent<charainfoPanel>()._closePanel);
}
}
//Debug.Log("关闭攻击范围");
}
}