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;
}
相关推荐
斑布斑布几秒前
【linux学习2】linux基本命令行操作总结
linux·运维·服务器·学习
鸭鸭梨吖40 分钟前
产品经理笔记
笔记·产品经理
Chef_Chen41 分钟前
从0开始学习机器学习--Day13--神经网络如何处理复杂非线性函数
神经网络·学习·机器学习
齐 飞1 小时前
MongoDB笔记01-概念与安装
前端·数据库·笔记·后端·mongodb
lulu_gh_yu1 小时前
数据结构之排序补充
c语言·开发语言·数据结构·c++·学习·算法·排序算法
丫头,冲鸭!!!2 小时前
B树(B-Tree)和B+树(B+ Tree)
笔记·算法
Re.不晚2 小时前
Java入门15——抽象类
java·开发语言·学习·算法·intellij-idea
听忆.2 小时前
手机屏幕上进行OCR识别方案
笔记
幼儿园老大*3 小时前
走进 Go 语言基础语法
开发语言·后端·学习·golang·go
Selina K3 小时前
shell脚本知识点记录
笔记·shell