Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to make inventory system for mobile game

Discussion in 'Scripting' started by Kalita2127, Oct 10, 2016.

  1. Kalita2127

    Kalita2127

    Joined:
    Dec 6, 2014
    Posts:
    247
    Hi guys so I currently make an inventory system from this tutorial. He is using json (LitJSON) in his inventory system. It worked well on pc, and then I tried it for mobile game but unluckily it didn't work. The item doesn't show in the slots. But I think we can use json on android.
    Anyone know why his inventory system didn't work on android ? Or maybe you guys have any recommendation to make an inventory system for android ? Thanks.
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    do ScriptableObjects work on mobile?
     
  3. Kalita2127

    Kalita2127

    Joined:
    Dec 6, 2014
    Posts:
    247
    hmm.. I don't think ScriptableObjects can be used for in game save purpose. From what I know ScriptableObject is for Editor only
     
  4. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,950
    Yes, scriptableobjects work on mobile. No, they are not editor only. No, they aren't to be used for saving (directly). JSON works on everything.

    What doesn't work? Have you profiled it? Are there errors? Gonna need some more details to provide any usable advice.
     
    alleed_8149 likes this.
  5. Kalita2127

    Kalita2127

    Joined:
    Dec 6, 2014
    Posts:
    247
    I don't have any errors on my script. The items didn't show up on slots in mobile but it shows up in pc version. In his tutorial, the tutor create the json file in a folder named StreamingAssets and that folder placed under Assets folder. And when I build it for pc, there is a folder also named StreamingAssets that has a json file on it.
    I don't have any experience in using json for mobile, but I'm guessing that maybe for mobile we need to create a json file via script and place it using
    Code (CSharp):
    1. Application.persistentDataPath
    . Am I guessing right ?
    If so how do I create a json file via script ?
     
    Last edited: Oct 11, 2016
  6. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,950
    Streaming assets are handled differently for different platforms:
    https://docs.unity3d.com/Manual/StreamingAssets.html

    But as long as you use Application.streamingAssetsPath, it will point to the right location.
    If you are not seeing errors, you should add some debugging to your application, to help you find out what is going on. It can help you find out exactly where things are going wrong, like is the file loading, is it parsing, is it parsing the way you expect. Profiling it will help isolate the problem.
     
  7. Kalita2127

    Kalita2127

    Joined:
    Dec 6, 2014
    Posts:
    247
    To be honest I just knew that StreamingAssets is a special folder from Unity. I thought that's just a normal folder.
    So if I placed json file under that folder, I can access it using Application.streamingAssetsPath ? I will give it a try now.
    I'm sorry I'm afraid that I don't know how to debug it on mobile.
     
  8. Kalita2127

    Kalita2127

    Joined:
    Dec 6, 2014
    Posts:
    247
    So I've tried to get the json file in StreamingAssets folder using Application.streamingAssetsPath. It worked on the Editor but it still didn't work on android. I have no clue what's cause this problem. :(
     
  9. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,950
  10. Kalita2127

    Kalita2127

    Joined:
    Dec 6, 2014
    Posts:
    247
    I have tried so many times to attach the debugger by following the instruction you give to me but I still can't connect my device with adb until know. No luck at all.
     
  11. Kalita2127

    Kalita2127

    Joined:
    Dec 6, 2014
    Posts:
    247
    I come back sir. So I have checked if the json file is exist or not in StreamingAssets folder and show it using Text UI. And the result is false. There are 2 probabilities, the file is moved to another directory or the file is destroyed when it was installed.
    And then I found this guy faced similar problem with json and solved it. But I tried his solution and still failed.
     
  12. Kalita2127

    Kalita2127

    Joined:
    Dec 6, 2014
    Posts:
    247
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using LitJson;
    4. using System.Collections.Generic;
    5. using System.IO;
    6.  
    7. public class ItemDatabase : MonoBehaviour {
    8.  
    9.     private List<Item> _database = new List<Item>();
    10.     private JsonData _itemData;
    11.  
    12.     void Start() {
    13.         Debug.Log(Application.streamingAssetsPath);
    14.         if(Application.isMobilePlatform){
    15.             StartCoroutine("Mobile");
    16.         }
    17.         else if(Application.isEditor){
    18.             if(File.Exists(Application.streamingAssetsPath + "/Items.json")){
    19.                 Debug.Log("Exists");
    20.             }
    21.             _itemData = JsonMapper.ToObject(File.ReadAllText(Application.streamingAssetsPath + "/Items.json"));
    22.         }
    23.         ConstructItemDatabase();
    24.     }
    25.  
    26.     IEnumerator Mobile() {
    27.         WWW loadDb = new WWW(Application.streamingAssetsPath + "/Items.json");
    28.         while(!loadDb.isDone){
    29.             yield return loadDb;
    30.             File.WriteAllBytes(Application.persistentDataPath + "/Items.json", loadDb.bytes);
    31.         }
    32.         _itemData = JsonMapper.ToObject(File.ReadAllText(Application.persistentDataPath + "/Items.json"));
    33.     }
    34.  
    35.     public Item FetchItemByID(int id) {
    36.         for(int i = 0; i < _database.Count; i++){
    37.             if(_database[i].ID == id){
    38.                 return _database[i];
    39.             }
    40.         }
    41.         return null;
    42.     }
    43.  
    44.     void ConstructItemDatabase() {
    45.         for(int i = 0; i < _itemData.Count; i++){
    46.             _database.Add(new Item(
    47.                 (int)_itemData[i]["id"], _itemData[i]["title"].ToString(),
    48.                 (int)_itemData[i]["value"], _itemData[i]["description"].ToString(),
    49.                 (bool)_itemData[i]["stackable"], _itemData[i]["slug"].ToString()));
    50.         }
    51.     }
    52. }
    53.  
    54. public class Item {
    55.  
    56.     public int ID { get; set; }
    57.     public string Title { get; set; }
    58.     public int Value { get; set; }
    59.     public string Description { get; set; }
    60.     public bool Stackable { get; set; }
    61.     public string Slug { get; set; }
    62.  
    63.     public Item(int id, string title, int value, string description, bool stackable, string slug) {
    64.         this.ID = id;
    65.         this.Title = title;
    66.         this.Value = value;
    67.         this.Description = description;
    68.         this.Stackable = stackable;
    69.         this.Slug = slug;
    70.     }
    71.  
    72.     public Item() {
    73.         this.ID = -1;
    74.     }
    75. }
    By the way this is the script that I used to communicate with json file.