上期工程实现了购买药品界面的构建,以及四个按钮的点击功能,本期开始实现药品购买功能。
工程需求:
①点击药品列表后面的购买按钮后,下端的数字输入框显示出来;
②再框内输入购买数量后,点击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);
}
}
通过上述代码即可实现物品的购买功能。