今天我们来完成商人武器系统,这个和前面的商店药品系统比较相像,很多NGUI和脚本非常相像。
public class ShopWeapon : MonoBehaviour {
public static ShopWeapon _instance;
private TweenPosition tween;
public int [] WeaponIdArray;
public UIGrid grid;
public GameObject WeaponItem;
private bool isShow = false;
private GameObject numberDialog;
private UIInput numberInput;
private int buyId;
private void Awake ()
{
_instance = this;
tween = this.GetComponent<TweenPosition> ();
numberDialog = transform.Find ("Panel/NumberDialog").gameObject;
numberInput = transform.Find ("Panel/NumberDialog/NumberInput").GetComponent<UIInput> ();
numberDialog.SetActive (false);
}
[System.Obsolete]
private void Start ()
{
InitWeaponShop ();
}
public void TransformStatus ()
{
if (isShow) {
tween.PlayReverse (); isShow = false;
} else {
tween.PlayForward (); isShow = true;
}
}
public void OnCloseButtonClick ()
{
TransformStatus ();
}
[System.Obsolete]
void InitWeaponShop ()
{//初始化武器商店信息
foreach (int id in WeaponIdArray) {
GameObject ItemGo = NGUITools.AddChild (grid.gameObject, WeaponItem);
grid.AddChild (ItemGo.transform);
ItemGo.GetComponent<WeaponItem> ().SetId (id);
}
}
public void OnOkButtonClick ()
{
int count = int.Parse (numberInput.value);
if(count > 0) {
int price = ObjectsInfo._instance.GetObjectInfoById (buyId).price_buy;
int total_price = price * count;
bool success = Inventory._instance.GetCoin (total_price);
if (success) {
Inventory._instance.GetId (buyId, count);
}
}
buyId = 0;
numberInput.value = "0";
numberDialog.SetActive (false);
}
public void OnBuyClick (int id)
{
buyId = id;
numberDialog.SetActive (true);
numberInput.value = "0";
}
public void OnClick ()
{
numberDialog.SetActive (false);
}
}
public class WeaponItem : MonoBehaviour {
private int id;
private ObjectInfo info;
private UISprite icon_sprite;
private UILabel name_Label;
private UILabel effect_Label;
private UILabel Pricesell_Label;
private void Awake ()
{
icon_sprite = transform.Find ("icon").GetComponent<UISprite> ();
name_Label = transform.Find ("name").GetComponent<UILabel> ();
effect_Label = transform.Find ("effect").GetComponent<UILabel> ();
Pricesell_Label = transform.Find ("price_sell").GetComponent<UILabel> ();
}
//通过调用这个方法更新装备显示
public void SetId (int id)
{
this.id = id;
info = ObjectsInfo._instance.GetObjectInfoById (id);
icon_sprite.spriteName = info.icon_name;
name_Label.text = info.name;
if(info.Attack > 0) {
effect_Label.text = "+伤害 " + info.Attack;
}else if(info.Def > 0) {
effect_Label.text = "+防御 " + info.Def;
} else if(info.Speed > 0){
effect_Label.text = "+速度 " + info.Speed;
}
Pricesell_Label.text = info.price_sell.ToString ();
}
public void OnBuyButtonClick ()
{
ShopWeapon._instance.OnBuyClick (id);
}
}