Cute_demon_attacks/meng_yao/Assets/script/A_Fight/CharacterClick.cs
2024-12-19 11:48:31 +08:00

95 lines
3.2 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
public class CharacterClick : MonoBehaviour
{
public Role role;
public Character MyCharacter=null;
[Header("点击出现的详情")] public GameObject panelPrefab;
[Header("点击出现的攻击范围")] public GameObject attackRange;
private Vector3 OrigScale;
private bool isShowAttackRange=true;
public WaveData MyWaveData=null;
void Start()
{
OrigScale = attackRange.transform.localScale;
IdForInfo(transform.GetComponent<enemy>().enemyId);
}
private void OnMouseDown()
{
// 确保父节点的碰撞体是 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("点击了父节点");
// 在这里处理点击事件
GameObject instantiatedPanel= GameObject.Instantiate(panelPrefab, GameObject.Find("Canvas").transform);
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_002","0","3","1","测试文档一",role.hp,100f);
}
else
{
panelRect.GetComponent<charainfoPanel>().UpDateShow("1", MyCharacter.Name, MyCharacter.Info, MyCharacter.MaxAttack + "~" + MyCharacter.MinAttack, MyCharacter.AttackRange, MyCharacter.AttackCD);
}
}
}
}
public void ShowAttackRange()
{
if (attackRange!=null&&isShowAttackRange!=true)
{
attackRange.transform.DOScale(OrigScale, 0.3f).SetEase(Ease.InOutBack);
isShowAttackRange = true;
}
else if (attackRange != null && isShowAttackRange)
{
attackRange.transform.DOScale(Vector3.zero, 0.3f).SetEase(Ease.InOutBack);
isShowAttackRange = false;
}
}
public void HideAttackRange()
{
if (attackRange != null )
{
attackRange.transform.DOScale(Vector3.zero, 0.3f).SetEase(Ease.InOutBack);
isShowAttackRange = false;
}
}
void IdForInfo(string id)
{
List<Character> mY = MengyaoInfo.Instance.m_Mengyao;
foreach (Character temp in mY)
{
if (temp.Id==id)
{
MyCharacter=temp;
break;
}
}
}
}