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;
}
相关推荐
ozawacai37 分钟前
Element UI入门笔记(个人向)
笔记·elementui·vue
念威42 分钟前
unity导入半透明webm + AE合成半透明视频
unity·游戏引擎·音视频·web
liangbm31 小时前
数学建模笔记—— 整数规划和0-1规划
笔记·python·数学建模·matlab·线性规划·整数规划·0-1规划
攸攸太上1 小时前
Java面试题·解释题·单例模式、工厂模式、代理模式部分
java·学习·单例模式·代理模式·简单工厂模式
youzjuer1 小时前
triton之ttir学习
学习
Chambor_mak1 小时前
stm32单片机个人学习笔记1(简单介绍)
stm32·单片机·学习
学会沉淀。1 小时前
大二上学期详细学习计划
学习
阿赵3D2 小时前
Unity制作角色溶解变成光点消失
java·unity·游戏引擎·shader·溶解·消失
仙魁XAN2 小时前
Unity 之 【Android Unity FBO渲染】之 [Unity 渲染 Android 端播放的视频] 的一种方法简单整理
android·unity·共享纹理·fbo·视频渲染