zoukankan      html  css  js  c++  java
  • Unity3d

    上期工程实现了购买药品界面的构建,以及四个按钮的点击功能,本期开始实现药品购买功能。

    工程需求:

    ①点击药品列表后面的购买按钮后,下端的数字输入框显示出来;

    ②再框内输入购买数量后,点击OK按钮进行购买;

    ③如果背包内金币不足,无效果;如果背包内金币充足,则扣除金币后在背包内增加相应药品。

    思路:

    建立DrugShop脚本,对上述4个按钮编写对应注册的方法,脚本如下:

    首先要在Inventory中建立消费的方法,脚本如下:

    Class Inventory

    {

        public bool GetCoin( int coin_need )

        {

             if( coinCount >= coin_need )

            {

                 coinCount -= coin_need;

                 CoinNumber.text = coinCount.ToString( );

                 return true;

            }

            return false;

        }

    }

    Class DrugShop

    {

        public GameObject drugList;

        private UIInput input;

        private int id;

        void Start( )

        {

            input = transform.Find("NumberInput").GetCompnent<UILabel>( );

        }

        void OnOkButton( )

        {

            int count = Int.Parse(input.value);

            Object info = ObjectsInfo._instance.GetObjectInfoById(id);

            int priceTotal = count*info.price_buy;

            bool success  = Inventory._instance.GetCoin( priceTotal );

            if(success)

            {

                Inventory._instance.GetId(info.id,count);

            }

            druglist.SetActive(false);

        }

        void Buy(int id)

        {

            druglist.SetActive(true);

            input.value = 0.ToString( );

            id = this.id;

        }

        public void OnBuyButton1001( )

        {

            Buy(1001);

        }

        public void OnBuyButton1002( )

        {

             Buy(1002);

        }

        public void OnBuyButton1003( )

        {

             Buy(1003);

        }

    }

    通过上述代码即可实现物品的购买功能。

  • 相关阅读:
    移动端小tips
    是否该放弃东莞的工作
    读书有感-learn html5 and javascript for ios
    eNSP多路由实现互联互通(华为路由E口直连)
    iTextSharp操作表格排版问题
    我今天开通博客
    12年的女程序员重新回归
    前端css样式规划
    前端神器-webstorm2017
    远程桌面资料共享
  • 原文地址:https://www.cnblogs.com/yanbenxin/p/5861756.html
Copyright © 2011-2022 走看看