06-05-2016, 04:34 PM
Codice:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
public class Inventory_System : MonoBehaviour {
public int maxSlots;
public GameObject inventorySlot;
public GameObject inventoryIcon;
public List<Item> itemObj = new List<Item> ();
public List<GameObject> slots = new List<GameObject> ();
private GameObject inventoryPanel;
private GameObject slotPanel;
private Item_Database database;
void Start ()
{
database = GetComponent<Item_Database> ();
inventoryPanel = GameObject.Find ("InvPanel");
slotPanel = inventoryPanel.transform.FindChild ("InvSlot").gameObject;
for (int i = 0; i < maxSlots; i++)
{
itemObj.Add (new Item());
slots.Add (Instantiate (inventorySlot));
slots [i].transform.SetParent (slotPanel.transform);
}
AddItem (1);
}
public void AddItem(int id)
{
Item itemToAdd = database.FetchItemByID (id);
for (int i = 0; i < itemObj.Count; i++)
{
if (itemObj [i].ID == -1)
{
itemObj [i] = itemToAdd;
GameObject objects = Instantiate (inventoryIcon);
objects.transform.SetParent (slots[i].transform);
objects.transform.localPosition = Vector2.zero;
break;
}
}
}
}
Codice:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using LitJson;
public class Item_Database : MonoBehaviour {
public List<Item> items = new List<Item> ();
public JsonData itemList;
void Start ()
{
itemList = JsonMapper.ToObject (File.ReadAllText (Application.dataPath + "/Database/Items.json"));
ConstructionDatabase ();
}
void ConstructionDatabase()
{
for (int i = 0; i < itemList.Count; i++)
{
items.Add (new Item ((int)itemList [i] ["id"], itemList [i] ["name"].ToString (), itemList [i] ["desc"].ToString ()));
}
}
public Item FetchItemByID (int id)
{
for (int i = 0; i < items.Count; i++)
{
if (items [i].ID == id)
{
return items [i];
}
}
return null;
}
}
public class Item
{
public int ID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public Item(int id, string name, string desc)
{
this.ID = id;
this.Name = name;
this.Description = desc;
}
public Item()
{
this.ID = -1;
}
}
Codice:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using LitJson;
public class ItemData : MonoBehaviour {
public enum itemIndex { MainEntranceKey = 0, Flashlight = 1}
public itemIndex itemNameFromList;
public enum itemType { Tool = 0, Key = 1, Consumable = 2}
public itemType tipology;
public float maxDistance = 5.0F;
public List<Item> itemGet = new List<Item> ();
private int itemID;
private int itemsJsonIndex;
private string itemTitle;
private string itemDesc;
private Inventory_System invDat;
private Ray raycast;
void Start ()
{
GameObject inv = GameObject.Find ("Inventory");
Item_Database itemDat = inv.GetComponent<Item_Database> ();
invDat = inv.GetComponent<Inventory_System> ();
itemDat.itemList = JsonMapper.ToObject (File.ReadAllText (Application.dataPath + "/Database/Items.json"));
itemDat.items.Add (new Item ((int)itemDat.itemList [itemsJsonIndex] ["id"], itemDat.itemList [itemsJsonIndex] ["name"].ToString (), itemDat.itemList [itemsJsonIndex] ["desc"].ToString ()));
if (itemNameFromList == itemIndex.MainEntranceKey) {
itemsJsonIndex = 0;
} else if (itemNameFromList == itemIndex.Flashlight) {
itemsJsonIndex = 1;
}
itemID = itemDat.items [itemsJsonIndex].ID;
itemTitle = itemDat.items [itemsJsonIndex].Name;
itemDesc = itemDat.items [itemsJsonIndex].Description;
}
void Update ()
{
RaycastHit hit;
raycast = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
float distance = Vector3.Distance (transform.position, Camera.main.transform.position);
if (Input.GetButtonDown ("Interact") && Physics.Raycast (raycast, out hit) && distance < maxDistance && hit.transform.name == name)
{
TakeItem ();
}
}
void TakeItem ()
{
Destroy (gameObject, 0.0F);
invDat.AddItem (itemID);
}
}
Questi sono gli script che ho creato fin'ora per il sistema dell'inventario e di raccogliere gli oggetti.
Come vi sembrano?