• Benvenuto in Making Videogames!
  • Dai sfogo alla tua fantasia!
  • Crea il tuo Videogioco!
Benvenuto ospite! Login Registrati




Valutazione discussione:
  • 0 voto(i) - 0 media
  • 1
  • 2
  • 3
  • 4
  • 5
Inventario con slot
#6
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?
 
Rispondi
  


Messaggi in questa discussione
Inventario con slot - da 93sunderland - 03-05-2016, 06:46 PM
RE: Inventario con slot - da Aconeron - 03-05-2016, 08:51 PM
RE: Inventario con slot - da 93sunderland - 03-05-2016, 10:25 PM
RE: Inventario con slot - da 93sunderland - 04-05-2016, 11:55 AM
RE: Inventario con slot - da 93sunderland - 05-05-2016, 10:38 AM
RE: Inventario con slot - da 93sunderland - 06-05-2016, 04:34 PM

Discussioni simili
Discussione Autore Risposte Letto Ultimo messaggio
  Grandezza inventario sempre uguale 93sunderland 0 419 16-05-2016, 02:42 PM
Ultimo messaggio: 93sunderland
  Strumenti di selezione dell'inventario 93sunderland 3 564 11-05-2016, 06:12 PM
Ultimo messaggio: 93sunderland

Vai al forum:


Browsing: 1 Ospite(i)