unity 学习笔记 UI

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;
}
相关推荐
机智的叉烧1 小时前
前沿重器[57] | sigir24:大模型推荐系统的文本ID对齐学习
人工智能·学习·机器学习
量子-Alex2 小时前
【多模态聚类】用于无标记视频自监督学习的多模态聚类网络
学习·音视频·聚类
吉大一菜鸡2 小时前
FPGA学习(基于小梅哥Xilinx FPGA)学习笔记
笔记·学习·fpga开发
CCSBRIDGE4 小时前
Magento2项目部署笔记
笔记
爱吃西瓜的小菜鸡5 小时前
【C语言】判断回文
c语言·学习·算法
小A1595 小时前
STM32完全学习——SPI接口的FLASH(DMA模式)
stm32·嵌入式硬件·学习
亦枫Leonlew5 小时前
微积分复习笔记 Calculus Volume 2 - 5.1 Sequences
笔记·数学·微积分
神码编程5 小时前
【Unity功能集】TextureShop纹理工坊(五)选区
unity·游戏引擎·shader·ps选区
岁岁岁平安5 小时前
spring学习(spring-DI(字符串或对象引用注入、集合注入)(XML配置))
java·学习·spring·依赖注入·集合注入·基本数据类型注入·引用数据类型注入
武昌库里写JAVA5 小时前
Java成长之路(一)--SpringBoot基础学习--SpringBoot代码测试
java·开发语言·spring boot·学习·课程设计