- 拾取操作的模擬
- 揹包的顯示與隱藏
- 物品提示信息
5.4 拾取模擬
- 查找所有物品中是否存在該物品
- 如果存在,num+1
- 如果不存在,查找空的網格,把新建的Inventory放入此方格中
public GameObject InventoryItem;
void Update()
{
if (Input.GetKeyDown (KeyCode.X)) //每次按下X都隨機拾取一種藥品
{
GetId(Random.Range(1001,1004));
}
}
public void GetId(int id)
{
InventoryItemGrid grid = null;
foreach (InventoryItemGrid temp in itemGridList) //這個循環判斷temp.id是否等於當前網格的id,若是, grid = temp,否則grid爲null
{
if(temp.id == id)
grid = temp;break;
}
if (grid != null) //如果存在該物品
{
grid.PlusNumber();
}
else //不存在
{
foreach(InventoryItemGrid temp in itemGridList)
{
if(temp.id == 0)
{
grid = temp;break;
}
}
if(grid != null)
{
GameObject itemGO = NGUITools.AddChild(grid.gameObject,InventoryItem);
itemGO.transform.localPosition = Vector3.zero; //每個物體在網格中的相對座標都是0
grid.SetId(id);
}
}
}
itemGO.GetComponent<UISprite>().depth = 8; //通過訪問UISprite元素以改變depth的值
5.5 揹包的顯示與隱藏
void Awake(){ //用Awake而不用Start是因爲Awake是在腳本對象實例化時被調用,而Start是在第一幀被調用
_instance = this;
tween = this.GetComponent<TweenPosition> ();
tween.AddOnFinished (this.OnTweenPlayFinished); //通過AddOnFinished監聽動畫是否播放完成,若完成,則isShow = false,隱藏揹包欄
this.gameObject.SetActive (false);
}
private bool isShow = false; //初始化isShow爲false,表示初始時不顯示
void Show()
{
isShow = true;
this.gameObject.SetActive (true);
tween.PlayForward ();
}
void Hide()
{
isShow = false;
tween.PlayReverse ();
}
void OnTweenPlayFinished() //播放完畢後,隱藏
{
if (isShow == false)
{
this.gameObject.SetActive(false);
}
}
public void TransformState()
{
if (isShow == false)
{
Show ();
}
else
{
Hide ();
}
}
5.6 揹包物品的提示信息
- 更新DesLabel的描述
- 更新InventoryDes的位置
public class InventoryDes : MonoBehaviour {
public static InventoryDes _instance; //描述設置爲實例
private UILabel label;
// Use this for initialization
void Awake () {
_instance = this;
label = this.GetComponentInChildren<UILabel> ();
}
// Update is called once per frame
void Update () {
}
public void Show(int id)
{
ObjectsInfo.ObjectInfo info = ObjectsInfo._instance.GetObjectInfoFromDict (id); //通過Show()傳遞的id獲取info信息。
string des = "";
switch (info.type) { //判斷info中的物品種類
case ObjectsInfo.ObjectType.Drug:
des = GetDrugDes(info);
break;
}
label.text = des;
}
string GetDrugDes(ObjectsInfo.ObjectInfo info) //具體描述,返回一個string
{
string str = "";
str += "名稱:" + info.name + "\n";
str += "回覆血量值:" + info.hp + "\n";
str += "回覆魔法值:" + info.mp +"\n";
str += "出售價:" + info.price_sell + "\n";
str += "購買價:" +info.price_buy + "\n";
return str;
}
}
public void OnHoverOver()
{
print ("enter");
}
public void OnHoverOut()
{
print ("exit");
}
private int id;
private bool isHover = false; //isHover作爲標誌位
public void OnHoverOver()
{
isHover = true;
}
public void OnHoverOut()
{
isHover = false;
}
void Update()
{
if (isHover)
{
InventoryDes._instance.Show (id); //若isHover爲true,顯示信息
}
}
this.gameObject.SetActive (true);
timer1 = 0.1f;
transform.position = UICamera.currentCamera.ScreenToWorldPoint (Input.mousePosition);
this.gameObject.SetActive (false);
void Update () {
if (this.gameObject.activeInHierarchy == true) //當前提示框是否處於隱藏狀態,true表示顯示
{
timer1 -= Time.deltaTime; //將timer1減0.1秒
}
if (timer1 <= 0) //當計時器小於0時,即鼠標離開後,隱藏提示框
{
this.gameObject.SetActive(false);
}
}
至此,就實現了提示框跟隨鼠標移動的功能。