UGUI--背包系统之四--------Item

来源:互联网 发布:知乎的运营模式 编辑:程序博客网 时间:2024/05/19 13:58
using UnityEngine;using System.Collections;using UnityEngine.UI;public class Item : MonoBehaviour{    public ItemBase itemBase { get; set; }    public int Amount { get; set; }    private Image image;    private Image Image    {        get        {            if (image == null)            {                image = GetComponent<Image>();            }            return image;        }    }    private Text text;    private Text Text    {        get        {            if (text == null)            {                text = GetComponentInChildren<Text>();            }            return text;        }    }    private float smoothing = 5;    private float targetScale = 1;    private Vector3 AnimScale = new Vector3(1.3f, 1.3f, 1.3f);    void Update()    {        if (transform.localScale.x != targetScale)        {            float scale = Mathf.Lerp(transform.localScale.x, targetScale, smoothing * Time.deltaTime);            transform.localScale = new Vector3(scale, scale, scale);        }    }    public void SetIcon(ItemBase itemBase, int amount = 1)    {        transform.localScale = AnimScale;        this.itemBase = itemBase;        this.Amount = amount;        //更新UI        Image.sprite = Resources.Load<Sprite>("Items/" + itemBase.Icon);        if (Amount > 1)        Text.text = Amount.ToString();        else        Text.text = "";    }    #region 控制物品数量的方法    public void AddAmount(int amount = 1)    {        transform.localScale = AnimScale;        this.Amount += amount;        Text.text = Amount.ToString();    }    public void ReduceAmount(int amount = 1)    {        transform.localScale = AnimScale;        this.Amount -= amount;        if (Amount > 1)            Text.text = Amount.ToString();        else            Text.text = "";    }    public void SetAmount(int amount)    {        transform.localScale = AnimScale;        this.Amount = amount;        if (Amount > 1)        Text.text = Amount.ToString();        else            Text.text = "";    }    #endregion    #region  给pickItem用方法    public void Show()    {        gameObject.SetActive(true);    }    public void Hide()    {        gameObject.SetActive(false);    }    public void SetLocalPostion(Vector3 pos)    {        transform.localPosition = pos;    }    #endregion}

0 0
原创粉丝点击