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;
}
相关推荐
孞㐑¥22 分钟前
Linux之基础知识
linux·服务器·经验分享·笔记·centos
XYN6126 分钟前
【嵌入式学习3】多任务编程
开发语言·笔记·嵌入式硬件·学习
Wallace Zhang6 小时前
STM32F103_LL库+寄存器学习笔记06 - 梳理串口与串行发送“Hello,World“
笔记·stm32·学习
jingjingjing11116 小时前
笔记:代码随想录算法训练营day62:108.冗余连接、109.冗余连接II
笔记
计算机毕设定制辅导-无忧学长6 小时前
从入门到精通:HTML 项目实战中的学习进度(一)
前端·学习·html
一味做笔记8 小时前
C语言学习笔记(抱佛脚版)
笔记·学习
qq_589568108 小时前
java学习笔记——多线程
java·笔记·学习·intellij-idea
Warolitbos8 小时前
Redis学习笔记
redis·笔记·学习
雨出8 小时前
算法学习第十七天:LRU缓存与布隆过滤器
学习·算法·缓存
V---scwantop---信9 小时前
复古卡通纹理噪点印刷打印照片效果PS特效样机 Texturizer Original Design Effect
笔记