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 /
avatar image
0
Question by lTimesl · Jun 21, 2019 at 05:30 PM · unity 5serializationloadingjson

unity 3d loading data from json problem?!

i am trying to practice on loading data from json files my problem is about when i try to load the sprite path from the json file to load the image icon from the floder that contain it, i manged to do that but by hard code the path but when i try to use variable to hold the path its failed to load any one can help me in that please

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 
 public class InventoryItemDataBase : MonoBehaviour
 {
     public allitemData itemsData = new allitemData();
     // Start is called before the first frame update
     void Start()
     {
         
         TextAsset asset = Resources.Load("ItemData/ItemDataBase") as TextAsset;
         itemsData = JsonUtility.FromJson<allitemData>(asset.text);
         Debug.Log(fetishitembyid(0).name);
     }
     public ItemBase fetishitembyid(int id)
     {
         for (int i = 0; i < itemsData.ItemBase.Count; i++)
         {
             if (itemsData.ItemBase[i].id == id)
                 return itemsData.ItemBase[i];
         }
         return null;
     }
 }
 
 [System.Serializable]
 public class allitemData
 {
     public List<ItemBase> ItemBase = new List<ItemBase>();
 }
 
 [System.Serializable]
 public class ItemBase
 {
     public int id, Value;
     public string name, description;
     public static string path;
     public static Sprite Icon = Resources.Load<Sprite>("IconS/Teddy"); // this way the image load
      public static Sprite Icon = Resources.Load<Sprite>(path); // this way the image not load
     public Sprite sprite = Icon;
 
     public ItemBase()
     {
         this.id = -1;
     }
 }

json Data

 {
   "ItemBase": [
     {
       "id": "0",
       "Value": "7",
       "name": "Teddy Bear",
       "description": "Teddy",
       "path": "IconS/Teddy"
     },
     {
       "id": "1",
       "Value": "5",
       "name": "UFO",
       "description": "ufoos"
     }]
 }

images below

alt text

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
Best Answer

Answer by Bunny83 · Jun 21, 2019 at 05:45 PM

static fields do not belong to any instance and therefore are never serialized or deserialized by pretty much all serialization systems. Either make it an instance field, or if the path should be the same for all instances you may want to move that field into your "allitemData" class instead. Also you should avoid using the field initializers to access any Unity API. Resources.Load calls should be done from a method that is called at the appropriate time.

Comment
Add comment · Show 2 · 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
avatar image lTimesl · Jun 21, 2019 at 06:35 PM 0
Share

Either make it an instance field, or if the path should be the same for all instances you may want to move that field into your "allitemData" class ins$$anonymous$$d

can you help me and explain how i did it in the right way pls i dont get how i move the field to allitemdata i create this class to fill the list as i learned from online tutorial

i just wan to get the path from the json file to store the image in the list database;

avatar image lTimesl · Jun 21, 2019 at 09:45 PM 0
Share

thank you

you mead me think in other way i was focusing on one way but i mead something foreach in the start method and that solved the problem i just load the sprite Separately here is my final code

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 
 public class InventoryItemDataBase : $$anonymous$$onoBehaviour
 {
     public allitemData itemsData = new allitemData();
     // Start is called before the first frame update
     void Start()
     {
         TextAsset asset = Resources.Load("ItemData/ItemDataBase") as TextAsset;
         itemsData = JsonUtility.FromJson<allitemData>(asset.text);
         foreach (var item in itemsData.Items)
         {
             item.Icon = Resources.Load<Sprite>("IconS/" + item.description);
         }
         Debug.Log(fetishitembyid(0).name);
     }
 
     public ItemBase fetishitembyid(int id)
     {
         for (int i = 0; i < itemsData.Items.Count; i++)
         {
             if (itemsData.Items[i].id == id)
                 return itemsData.Items[i];
         }
         return null;
     }
 }
 
 [System.Serializable]
 public class allitemData
 {
     public List<ItemBase> Items = new List<ItemBase>();
 }
 
 [System.Serializable]
 public class ItemBase
 {
     public int id, Value;
     public string name, description;
     public Sprite Icon;
 
     public ItemBase()
     {
         this.id = -1;
     }
 }

 

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

210 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

Related Questions

How to protect JSON file game data? 3 Answers

JsonUtility doesn't serialize nested mixed var 1 Answer

Can we create new variables or properties from the inspector? 0 Answers

Stop specific fields from being serialized by JSON utility 3 Answers

Json Serialization documentation official Unity website - marking it [Serializable] caused error 1 Answer


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