Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by AvatarWukong · May 24, 2018 at 06:27 AM · androidunity 5android buildjsonitems

can you read json file on android ? I have a thesis and it's not going well.

I have these codes for my item database, it works on pc, but doesn't work on my android phone

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using LitJson;
 using System.IO;
 
 
 public class ItemDatabase : MonoBehaviour
 {
     private List<Weapon> WeaponDatabase = new List<Weapon>();
     private List<Armor> ArmorDatabase = new List<Armor>();
     private List<Potion> PotionDatabase = new List<Potion>();
     private JsonData WeaponData, ArmorData, PotionData;
 
 
     void Start()
     {
         WeaponData = JsonMapper.ToObject(File.ReadAllText(Application.dataPath + "/Scripts/Weapons.json"));
         ArmorData = JsonMapper.ToObject(File.ReadAllText(Application.dataPath + "/Scripts/Armors.json"));
         PotionData = JsonMapper.ToObject(File.ReadAllText(Application.dataPath + "/Scripts/Potions.json"));
 
         ConstructItemDatabase();
 
     }
 
     public Weapon FetchWeaponbyID(int id)
     {
         for (int i = 0; i < WeaponDatabase.Count; i++)
         {
             if (WeaponDatabase[i].ID == id)
             {
                 return WeaponDatabase[i];
             }
         }
         return null;
     }
 
     public Armor FetchArmorbyID(int id)
     {
         for (int i = 0; i < ArmorDatabase.Count; i++)
         {
             if (ArmorDatabase[i].ID == id)
             {
                 return ArmorDatabase[i];
             }
         }
         return null;
     }
 
     public Potion FetchPotionbyID(int id)
     {
         for (int i = 0; i < PotionDatabase.Count; i++)
         {
             if (PotionDatabase[i].ID == id)
             {
                 return PotionDatabase[i];
             }
         }
         return null;
     }
 
 
     void ConstructItemDatabase()
     {
         for (int x = 0; x < WeaponData.Count; x++)
         {
             WeaponDatabase.Add(new Weapon((int)WeaponData[x]["ID"], WeaponData[x]["ItemName"].ToString(),
                 (int)WeaponData[x]["Damage"], (bool)WeaponData[x]["Stackable"], (bool)WeaponData[x]["Equipable"],
                 WeaponData[x]["Slug"].ToString(), WeaponData[x]["Description"].ToString()));
         }
 
         for (int x = 0; x < ArmorData.Count; x++)
         {
             ArmorDatabase.Add(new Armor((int)ArmorData[x]["ID"], ArmorData[x]["ItemName"].ToString(),
                 (int)ArmorData[x]["MaxHealth"], (bool)ArmorData[x]["Stackable"], (bool)ArmorData[x]["Equipable"],
                 ArmorData[x]["Slug"].ToString(), ArmorData[x]["Description"].ToString()));
         }
 
         for (int x = 0; x < PotionData.Count; x++)
         {
             PotionDatabase.Add(new Potion((int)PotionData[x]["ID"], PotionData[x]["ItemName"].ToString(),
                 (int)PotionData[x]["HealAmount"], (bool)PotionData[x]["Stackable"], (bool)PotionData[x]["Equipable"],
                 PotionData[x]["Slug"].ToString(), PotionData[x]["Description"].ToString()));
         }
 }
 
 public class Weapon
 {
     public int ID { get; set; }
     public string ItemName { get; set; }
     public int Damage { get; set; }
     public bool Stackable { get; set; }
     public bool Equipable { get; set; }
     public string Description { get; set; }
     public string Slug { get; set; }
     public Sprite Sprite { get; set; }
 
 
     public Weapon(int _id, string _itemName, int _damage, bool _stackable, bool _equipable, string _slug, string _description)
     {
         this.ID = _id;
         this.ItemName = _itemName;
         this.Stackable = _stackable;
         this.Equipable = _equipable;
         this.Damage = _damage;
         this.Slug = _slug;
         this.Description = _description;
         this.Sprite = Resources.Load<Sprite>("Sprites/Weapons/" + this.Slug);
     }
 
 
 
     public Weapon()
     {
         this.ID = -1;
     }
 
 
 }
 
 public class Armor
 {
     public int ID { get; set; }
     public string ItemName { get; set; }
     public int MaxHealth { get; set; }
     public bool Stackable { get; set; }
     public bool Equipable { get; set; }
     public string Description { get; set; }
     public string Slug { get; set; }
     public Sprite Sprite { get; set; }
 
 
     public Armor(int _id, string _itemName, int _maxhealth, bool _stackable, bool _equipable, string _slug, string _description)
     {
         this.ID = _id;
         this.ItemName = _itemName;
         this.Stackable = _stackable;
         this.Equipable = _equipable;
         this.MaxHealth = _maxhealth;
         this.Slug = _slug;
         this.Description = _description;
         this.Sprite = Resources.Load<Sprite>("Sprites/Armors/" + this.Slug);
     }
 
 
 
     public Armor()
     {
         this.ID = -1;
     }
 
 
 }
 
 public class Potion
 {
     public int ID { get; set; }
     public string ItemName { get; set; }
     public bool Stackable { get; set; }
     public bool Equipable { get; set; }
     public string Description { get; set; }
     public string Slug { get; set; }
     public int HealAmount { get; set; }
     public Sprite Sprite { get; set; }
 
 
     public Potion(int _id, string _itemName, int _healamount, bool _stackable, bool _equipable, string _slug, string _description)
     {
         this.ID = _id;
         this.ItemName = _itemName;
         this.Stackable = _stackable;
         this.Equipable = _equipable;
         this.HealAmount = _healamount;
         this.Slug = _slug;
         this.Description = _description;
         this.Sprite = Resources.Load<Sprite>("Sprites/Potions/" + this.Slug);
     }
 
     public Potion()
     {
         this.ID = -1;
     }
 
 
 }

 
 
 

How can I make it better, please, I need an answer. please help me. the images for the items in the inventory are showing correctly on my pc, but for android its null.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Hoorza · May 24, 2018 at 07:41 AM

Hi, there is two things I can see different from our saving solution. One is that the way you save JSon file. Our solution is working fine on the Android even with encryption on top of it. You are using:

 WeaponData = JsonMapper.ToObject(File.ReadAllText(Application.dataPath + "/Scripts/Weapons.json"));

I have not used JsonMapper.ToObject before, but this is how our code looks like:

Define the path 1st with:

  filePath = Path.Combine(Application.persistentDataPath, fileName);

Than save it using string:

 string content = JsonUtility.ToJson(totalProgress, true);                 
 File.WriteAllText(filePath, content);    


Maybe this method is not compatible with the Android. The other thing that I see is that you are using Application.dataPath instead of Application.persistentDataPath. Quick search returned this question: dataPath vs persistentDataPath. First one is read only. Hope that helps.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

315 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

"No Android Device Found" on Build and Run 1 Answer

AndroidManifest-main.xml merging error 6 Answers

mobile device restarts when game is uninstalled 0 Answers

Unity 2017.2.0b9 ARCore build&run with Pixel XL (Android 8.0) Failed 0 Answers

[8 days stuck at it] I upgrade project from 5.4 to 5.5 and 5.6 giving Failed assemblies stripper. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges