UI布局相关
需求-卡牌游戏
实现鼠标移动到卡片时放大,移出时恢复原状,可拖动,可判定拖动至不同ui区域,在手牌区域放置后会自动回到原来的位置
csharp
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using DG.Tweening;
using System;
[Serializable]
public class CardInfo
{
public string name;
public string description;
public int value;
public CardType cardType;
public Sprite sprite;
}
public enum CardType
{
Atk, AtkUp, AtkDown, DefUp, DefDown, Sleep, None
}
public class CardItem : MonoSington<CardItem>, IEndDragHandler, IDragHandler, IPointerEnterHandler, IPointerExitHandler, IPointerClickHandler
{
public CardInfo _cardInfo;
// 假设你已经有了四个区域边界的RectTransform引用
public Transform _orignTrans;
public Transform _hightlightTrans;
RectTransform _mainCanvas;
public int _index;
private void Awake()
{
//_cardInfo = new CardInfo();
_cardInfo.name = "卡牌"+_index;
_cardInfo.value = _index;
_mainCanvas = GameObject.Find("Canvas").GetComponent<RectTransform>();
}
public void OnDrag(PointerEventData eventData)
{
Vector3 globalMousePos;
if (RectTransformUtility.ScreenPointToWorldPointInRectangle
(_mainCanvas, eventData.position, eventData.pressEventCamera, out globalMousePos))
{
this.transform.position = globalMousePos;
this.transform.rotation = _mainCanvas.rotation;
}
}
public void OnEndDrag(PointerEventData eventData)
{
// 转换当前UI元素的RectTransform到Canvas的坐标系中
//将选中的点转换为Image区域内的本地点
//判断是否在手牌区域,是则恢复,不做处理
Vector2 localPoint;
RectTransformUtility.ScreenPointToLocalPointInRectangle(_orignTrans.GetComponent<RectTransform>()
, eventData.position, null, out localPoint);
Vector2 pivot = _orignTrans.GetComponent<RectTransform>().pivot;
Vector2 normalizedLocal =
new Vector2(pivot.x + localPoint.x / _orignTrans.GetComponent<RectTransform>().sizeDelta.x
, pivot.y + localPoint.y / _orignTrans.GetComponent<RectTransform>().sizeDelta.y);
if ((normalizedLocal.x >= 0 && normalizedLocal.x <= 1) && ((normalizedLocal.y >= 0 && normalizedLocal.y <= 1)))
{
// 假设rectTransform是包含需要刷新布局的UI元素的RectTransform
this.Back2OriginPanel();
LayoutRebuilder.ForceRebuildLayoutImmediate(_orignTrans.GetComponent<RectTransform>());
return;
}
//能到这里意味着玩家打出了牌,开始进行判定
BattleSystemMgr.Instance?.HandleCard(this._cardInfo);
LayoutRebuilder.ForceRebuildLayoutImmediate(_orignTrans.GetComponent<RectTransform>());
}
public void OnPointerExit(PointerEventData eventData)
{
this.Back2OriginPanel();
}
public void OnPointerEnter(PointerEventData eventData)
{
this.ToHightlightPanel();
}
public void OnPointerClick(PointerEventData eventData)
{
Debug.Log("123");
}
private void Back2OriginPanel()
{
this.transform.DOScale(1f, 0.3f);
this.transform.SetParent(_orignTrans);
this.transform.SetSiblingIndex(_index);
this.GetComponentInParent<HorizontalLayoutGroup>().enabled = true;
}
private void ToHightlightPanel()
{
this.transform.DOScale(1.2f, 0.3f);
this.GetComponentInParent<HorizontalLayoutGroup>().enabled = false;
this.transform.SetParent(_hightlightTrans);
}
}
csharp
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class BattleSystemMgr : MonoSington<BattleSystemMgr>
{
public PlayerInfo _playerInfo;
public EnermyInfo _enermyInfo;
public Text text;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void HandleCard(CardInfo cardInfo)
{
switch (cardInfo.cardType)
{
case CardType.Atk:
//直接造成伤害
//计算伤害
int temAtk = _playerInfo._curAtk;
_enermyInfo._curHP -= temAtk;
string log = $"玩家使用卡片对敌人造成了{cardInfo.value}点的伤害";
Debug.Log(log);
text.text = log;
break;
case CardType.AtkUp:
_playerInfo._curAtk += cardInfo.value;
log = $"玩家使用卡片提升自身{cardInfo.value}点的攻击力";
Debug.Log(log);
text.text = log;
break;
case CardType.AtkDown:
_enermyInfo._curAtk -= cardInfo.value;
log = $"玩家使用卡片降低敌方{cardInfo.value}点的攻击力";
Debug.Log(log);
text.text = log;
break;
case CardType.DefUp:
_playerInfo._curDef += cardInfo.value;
log = $"玩家使用卡片提升自身{cardInfo.value}点的防御力";
Debug.Log(log);
text.text = log;
break;
case CardType.DefDown:
_enermyInfo._curDef -= cardInfo.value;
log = $"玩家使用卡片降低敌方{cardInfo.value}点的防御力";
Debug.Log(log);
text.text = log;
break;
case CardType.Sleep:
log = $"玩家使用卡片让敌方睡眠跳过一回合";
Debug.Log(log);
text.text = log;
break;
case CardType.None:
log = $"玩家使用卡片....无事发生";
Debug.Log(log);
text.text = log;
break;
}
}
}
[Serializable]
public class PlayerInfo
{
public int _id;
public string _name;
public string _description;
public int _level;
public int _maxHP;
public int _curHP;
public int _oriAtk;
public int _curAtk;
public int _oriDef;
public int _curDef;
}
[Serializable]
public class EnermyInfo
{
public int _id;
public string _name;
public string _description;
public int _level;
public int _maxHP;
public int _curHP;
public int _oriAtk;
public int _curAtk;
public int _oriDef;
public int _curDef;
}