zoukankan      html  css  js  c++  java
  • Unity3d

    上期工程建立了ObjectsInfo类,用于设置物品的属性和将物品属性读取至内存中。现在开始构建背包系统UI。

    创建背包系统思路:①创建背包背景(Inventory);②创建背包格子(Inventory_item_grid);③创建金币显示spriate和金币数量Label;④添加显示及隐藏方法。

    界面创建跟前期的任务系统一样,在此不做赘述了,下面开始总结如何实现背包的功能。

    我们需要明确一个思路:那就是背包背景/背包格子/背包物品三者需要分离控制。

    一、背包背景

    背包背景:负责界面显示/隐藏、搭载背包格子以及显示金币数量,因为后期需要多次调用该类,且该类唯一,那么将其做成单例模式。

    ①显示与隐藏:非常好实现,使用Attach添加position移动动画就可以了。

    ②搭载背包格子:使用List泛型建立,使List中的元素与背包格子一一对应,方便后期控制。(注:此处需要对背包格子加入一个类:Inventory_item_grid作为泛型的标识类)

    ③显示金币数量,读取UI中的label就可以了。

    根据上述思路,脚本如下:

    Class Inventory

    {

        public static Inventory _instance;

        private TweenPosition tween;

        public List<Inventory_item_grid> itemList = new List<Inventory_item_grid>();

        private int Coin_Count = 1000;

        public UILabel label;

        void Awake()

        {

            _instance = this;

            tween = GetCompnent<TweenPosition>();

        }

        public void Show()

        {

            tween.gameobject.setActive(true);

            tween.PlayForward();

        }

        public void Hide()

        {

            tween.PlayReverse();

        }

    }

    以上就是初步对背包系统进行了构建。

    二、背包格子

    背包格子用于搭载物品,在拖动物品时,需要检测拖动时鼠标与界面的碰撞,因此为格子加入BoxCollider,为其Tag加入Inventory_item_grid;然后就是对背包格子进行管理,管理的思路如下:

    ①显示物品图标;②显示物品的数量。

    显示物品的图标及数量脚本如下:

    Class Inventory_item_grid

    {

        private int id = 0;

        private ObjectInfo info = null;

        private UILabel label;

        private int num = 0;

        void start( )

        {

            label = GetCompnentInChildRen<UILabel>( );

        }

        public void SetId( int id,int mun =1 )

        {

            info = ObjectsInfo._instance.GetObjectById( id );

            Inventoryitem item = GetCompnent<Inventoryitem>( );

            item.SetIcon_Name(info.icon_name);

            this.num = num;

            label.gameobject.SetActive(true); 

        }

       

       pulic void ClearInfo( )

        {

            id = 0;

            info = null;

            num = 0;

            label.gameobject.SetActive(false); 

        }

    }

    三、物品

    现阶段物品主要有两个功能需求:①可拖动;②物品的icon需要根据实际需要变更。

    首先在格子内创建一个物品,制作为Prefab,其可拖动功能继承与UI的UIDragDropitem类,其Icon的改变直接读取前期做好的ObjectInfo类

    脚本如下:

    Class InventoryItem

    {

        private UISprite sprite;

        void Awake( )

        {

            sprite = GetCompnent<UISprite>();

        }

        protected override void OnDropRelease(GameObject surface)

        {

            base.OnDropRelease(surface);

        }

        public void SetId(int id )

        {

            ObjectInfo info = Objects._instance.GetObjetcById( id );

            sprite.SpriteName = info.icon_name;

        }

        public void SetIcon_Name(string icon_Name)

        {

            sprite.SpriteName = icon_Name;

        }

    }

    以上就实现了物品的拖动以及根据物品的icon变化。

  • 相关阅读:
    Hard Rock
    Codeforces Round #416 (Div. 2) B. Vladik and Complicated Book
    codeforces 793B. Igor and his way to work
    codeforces 1B Spreadsheets
    HDU 1069 Monkey and Banana
    codeforces 2B The least round way
    【机器学习】 通俗说拟合
    python-八皇后问题
    python-核心知识思维导图
    python-@property 属性
  • 原文地址:https://www.cnblogs.com/yanbenxin/p/5822793.html
Copyright © 2011-2022 走看看