unity 学习笔记 UI

UI布局相关

需求-卡牌游戏

实现鼠标移动到卡片时放大,移出时恢复原状,可拖动,可判定拖动至不同ui区域,在手牌区域放置后会自动回到原来的位置

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);
    }

    
}

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
学习Unity3D时,以下是一些重要的笔记: 1. Unity3D基础知识: - 游戏对象(Game Objects)和组件(Components):了解游戏对象的层次结构和组件的作用。 - 场景(Scenes)和摄像机(Cameras):学会如何创建场景并设置摄像机视角。 - 材质(Materials)和纹理(Textures):掌握如何创建和应用材质和纹理。 - 动画(Animations):学习如何创建和控制游戏对象的动画。 2. 脚本编程: - C#语言基础:了解C#语言的基本语法和面向对象编程概念。 - Unity脚本编写:学习如何编写脚本来控制游戏对象的行为和交互。 - 常见组件和功能:掌握常见的Unity组件和功能,如碰撞器(Colliders)、刚体(Rigidbodies)、触发器(Triggers)等。 3. 游戏开发流程: - 设计游戏关卡:了解如何设计游戏场景和关卡,包括布局、道具、敌人等。 - 游戏逻辑实现:将游戏规则和玩家交互转化为代码实现。 - UI界面设计:学习如何设计游戏中的用户界面,包括菜单、计分板等。 - 游戏优化和调试:优化游戏性能,解决常见的错误和问题。 4. 学习资源: - Unity官方文档和教程:官方提供了大量的文档和教程,逐步引导你学习Unity3D。 - 在线教程和视频教程:网上有很多免费和付费的Unity教程和视频教程,可根据自己的需求选择学习。 - 社区论坛和博客:加入Unity开发者社区,与其他开发者交流并获取帮助。 通过系统地学习这些内容,你将能够掌握Unity3D的基础知识并开始开发自己的游戏项目。记得不断实践和尝试,不断提升自己的技能!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

淳杰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值