游戏开发中经常出现武器商店,接下来为你们带来武器装备商店系统的具体解决办法。(后续出UGUI+Json版本,敬请期待)
武器道具的具体逻辑:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EquipmentItem : MonoBehaviour
{
private UISprite sprite;
public int id;
private bool isHover=false;
//卸下面板
public UISprite tipWindow2;
public UIButton btntipOk;
public UIButton btntipCancel;
private void Awake()
{
sprite= this.GetComponent<UISprite>();
}
private void Update()
{
if(isHover)
{
//鼠标在装备栏前 检测点击
if(Input.GetMouseButtonDown(1))
{
tipWindow2.gameObject.SetActive(true);
btntipOk.onClick.Add(new EventDelegate(() =>
{
EquipmentUI._instance.TakeOff(id,this.gameObject);
//Inventory._instance.GetId(id);
//GameObject.Destroy(this.gameObject);
//tipWindow.gameObject.SetActive(false);
}));
btntipCancel.onClick.Add(new EventDelegate(() =>
{
tipWindow2.gameObject.SetActive(false);
}));
}
}
}
public void SetId(int id)
{
this.id=id;
ObjectInfo info=ObjectsInfo._instance.GetObjectInfoById(id);
SetInfo(info);
}
public void SetInfo(ObjectInfo info)
{
this.id= info.id;
sprite.spriteName= info.icon_name;
}
public void OnHover(bool isOver)
{
isHover=isOver;
}
}
这个你还需要结合我前面的笔记里面的信息管理系统一起使用才能发挥成效(在背包系统那篇笔记之中)
控制装备系统的逻辑:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using static UISpriteCollection;
//一个用于控制装备面板的类
public class EquipmentUI : MonoBehaviour
{
public static EquipmentUI _instance;
private TweenPosition tween;
private bool isShow=false;
//关联装备按钮
public UIButton btnEquipment;
//关联关闭按钮
public UIButton btnClose;
//关联物品装备格子
public GameObject headgear;
public GameObject armor;
public GameObject rightHand;
public GameObject leftHand;
public GameObject shoe;
public GameObject accessory;
private PlayerStatus ps;
public GameObject equipmentItem;
//关联提示信息
public UISprite uiTip;
public UILabel uiTipText;
private TweenPosition tween2;
public int attack = 0;
public int def = 0;
public int speed = 0;
void Awake()
{
_instance= this;
tween=this.GetComponent<TweenPosition>();
ps=GameObject.FindGameObjectWithTag(Tags.player).GetComponent<PlayerStatus>();
tween2 = uiTip.GetComponent<TweenPosition>();
}
void Start()
{
btnEquipment.onClick.Add(new EventDelegate(() =>
{
TransformState();
}));
btnClose.onClick.Add(new EventDelegate(() =>
{
TransformState();
}));
}
public void TransformState()
{
if(isShow==false)
{
tween.PlayForward();
isShow=true;
}
else
{
tween.PlayReverse();
isShow=false;
}
}
//穿戴处理Tip
public void DressAbout()
{
uiTip.enabled = true;
uiTipText.enabled = true;
tween2.PlayForward();
Invoke("SprTipHide", 1);
}
void SprTipHide()
{
uiTip.enabled = false;
uiTipText.enabled = false;
tween2.PlayReverse();
}
public bool Dress(int id)
{
ObjectInfo info=ObjectsInfo._instance.GetObjectInfoById(id);
if(info.type!=ObjectType.Equip)
{
uiTipText.text = "穿戴失败";
DressAbout();
return false;//穿戴不成功
}
if(ps.heroType==HeroType.Magician)
{
if(info.applicationType==ApplicationType.Swordman)
{
uiTipText.text = "穿戴失败";
DressAbout();
return false;
}
}
if(ps.heroType==HeroType.Swordman)
{
if(info.applicationType==ApplicationType.Magician)
{
uiTipText.text = "穿戴失败";
DressAbout();
return false;
}
}
GameObject parent = null;
switch(info.dressType)
{
case DressType.Headgear:
parent = headgear;
break;
case DressType.Armor:
parent = armor;
break;
case DressType.RightHand:
parent = rightHand;
break;
case DressType.LeftHand:
parent = leftHand;
break;
case DressType.Shoe: parent = shoe;
break;
case DressType.Accessory: parent = accessory;
break;
}
EquipmentItem item = parent.GetComponentInChildren<EquipmentItem>();
if(item!=null)
{
//把已经穿戴的卸下
Inventory._instance.GetId(item.id);
//已经穿戴同一类型的装备
item.SetInfo(info);
}
else
{
//没有穿戴同样类型的装备
GameObject itemGo = NGUITools.AddChild(parent, equipmentItem);
itemGo.transform.localPosition= Vector3.zero;
itemGo.GetComponent<EquipmentItem>().SetInfo(info);
}
UpdatePropetry();
uiTipText.text = "穿戴成功";
DressAbout();
return true;
}
public void TakeOff(int id,GameObject go)
{
Inventory._instance.GetId(id);
GameObject.Destroy(go);
UpdatePropetry();//更新属性
}
void UpdatePropetry()
{
this.attack = 0;
this.def = 0;
this.speed= 0;
EquipmentItem headgearItem =headgear.GetComponentInChildren<EquipmentItem> ();
PlusPropertry(headgearItem);
EquipmentItem armorItem=armor.GetComponentInChildren<EquipmentItem> ();
PlusPropertry(armorItem);
EquipmentItem leftHandItem=leftHand.GetComponentInChildren<EquipmentItem> ();
PlusPropertry(leftHandItem);
EquipmentItem rightHandItem=rightHand.GetComponentInChildren<EquipmentItem> ();
PlusPropertry(rightHandItem);
EquipmentItem shoeItem=shoe.GetComponentInChildren<EquipmentItem> ();
PlusPropertry(shoeItem);
EquipmentItem accessoryItem=accessory.GetComponentInChildren<EquipmentItem> ();
PlusPropertry(accessoryItem);
}
void PlusPropertry(EquipmentItem item)
{
if (item != null)
{
ObjectInfo equipInfo = ObjectsInfo._instance.GetObjectInfoById(item.id);
this.attack += equipInfo.attack;
this.def += equipInfo.def;
this.speed += equipInfo.speed;
}
}
}
具体思路和核心方法全在代码里讲述了,如果你有什么不懂的,欢迎来问我,我很高兴为您解答!(有一些代码结合了其它系统,你可以直接修改它们),如果对你有一些帮助就点个赞支持一下吧!