Skip to content

Instantly share code, notes, and snippets.

@smallrice45
Created May 22, 2015 11:03
Show Gist options
  • Save smallrice45/925c12a70ec714eebd89 to your computer and use it in GitHub Desktop.
Save smallrice45/925c12a70ec714eebd89 to your computer and use it in GitHub Desktop.
Sample Backpack
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class BackpackManager : MonoBehaviour {
public List<ItemData.ItemAttribute> _BackpackItem = new List<ItemData.ItemAttribute>();
public List<ItemData.ItemAttribute> m_BackpackItem {
get {
return _BackpackItem;
}
set {
_BackpackItem = value;
}
}
private BackpackUIManager m_BackpackUIManager;
// Use this for initialization
void Start () {
m_BackpackUIManager = GetComponent <BackpackUIManager>();
AddItemCount(1,3);
AddItemCount(6,3);
AddItemCount(6,-3);
}
void AddItemCount(int itemUid, int addCount){
int itemIndex = m_BackpackItem.FindIndex(_item => _item.uid==itemUid);
if (itemIndex <0){
int theLastIndex = m_BackpackItem.Count;
AddItemToList(itemUid, theLastIndex);
m_BackpackItem[theLastIndex].count += addCount;
// Send Add Item UI
m_BackpackUIManager.AddItemToList(m_BackpackItem[theLastIndex]);
}else{
m_BackpackItem[itemIndex].count += addCount;
// When Count <=0 RemoveItem
if (m_BackpackItem[itemIndex].count <= 0){
m_BackpackUIManager.RemoveItemFormList(m_BackpackItem[itemIndex]);
m_BackpackItem.Remove(m_BackpackItem[itemIndex]);
}
}
}
void AddItemToList(int itemUid, int theLastIndex){
ItemData.ItemAttribute newItemData = GetItemData(itemUid);
m_BackpackItem.Add(new ItemData.ItemAttribute(){uid = itemUid});
}
ItemData.ItemAttribute GetItemData(int itemUid){
ItemData.ItemAttribute _itemData = new ItemData.ItemAttribute()
{
uid = itemUid,
name = "None Name",
image = null,
};
return _itemData;
}
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class BackpackUIManager : MonoBehaviour {
[System.Serializable]
public class _BackpackItem{
public int ItemUid;
public GameObject ItemButton;
}
public List<_BackpackItem> m_BackpackItem = new List<_BackpackItem>();
public GameObject ItemButtonPerfabs;
public Transform BackpackList;
// Update is called once per frame
public void AddItemToList (ItemData.ItemAttribute itemInfo) {
GameObject theItemButton = Instantiate(ItemButtonPerfabs) as GameObject;
theItemButton.transform.SetParent(BackpackList,true);
m_BackpackItem.Add(new _BackpackItem(){ItemUid = itemInfo.uid, ItemButton = theItemButton});
}
public void RemoveItemFormList(ItemData.ItemAttribute itemInfo){
int theItemUid = m_BackpackItem.FindIndex(_Item => _Item.ItemUid == itemInfo.uid);
Destroy(m_BackpackItem[theItemUid].ItemButton);
m_BackpackItem.Remove(m_BackpackItem[theItemUid]);
}
}
using UnityEngine;
using System.Collections;
public class ItemData{
[System.Serializable]
public class ItemAttribute{
public int _uid;
public Sprite _image;
public string _name;
public int _count;
public string _explanation;
public int uid{
get {
return _uid;
}
set {
_uid = value;
}
}
public Sprite image{
get {
return _image;
}
set {
_image = value;
}
}
public string name{
get {
return _name;
}
set {
_name = value;
}
}
public int count{
get {
return _count;
}
set {
_count = value;
if (_count <=0){
_count = 0;
}
}
}
public string explanation{
get {
return _explanation;
}
set {
_explanation = value;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment