Hot answers tagged

51

Go with the second approach, simply due to the fact that you can introduce new resource types or items at any time without having to rewrite or update code (data driven development). Edit: To elaborate a bit more on why this is in general good practice, even if you're 100% sure some value won't ever change. Let's take the console game example mentioned in ...


29

A rule of thumb is that you use different classes when objects require different code and instances of the same class when the objects only require different values. When the resources have different game mechanics which are unique to them, it might make sense to represent them with classes. For example, when you have Plutonium which has a half-life time ...


14

I would like to add there are two extra options: Interface: you can consider it if the resource class would be just a "storage" for 5 integers and each other entity would have different logic regarding resources (e.g. player spend/loot, city produces resource). In that case you might not want a class at all - you merely wanted to expose that some entity has ...


9

I am Andreas Loew - creator of TexturePacker and PhysicsEditor. We define the export formats for our tools depending on the needs of the framework developers - in most cases we simply adapt to formats that are already available. Most of TexturePacker's data formats are template based - you can adjust it to whatever you need. We also have 3 generic data ...


7

The problem with JsonUtility, is that it doesn't save properties, just public fields and fields with the [SerializeField] attribute. You can solve this with another class and some conversion magic, like so: Json Serializable Class struct JsonDateTime { public long value; public static implicit operator DateTime(JsonDateTime jdt) { Debug....


4

Your JSON firstly is faulty and secondly does not match the data model. To break down your JSON: { "series": [ {, "points":[ { "ts": "1473850836254", "value": "11.27" }, { "ts": "1473851256637", "value": "...


4

if I recall correctly, when you serialize a C# class to JSON, it serializes members, not properties (even if by {get; set;} the compiler creates the members). You can try by marking the members as serializable with [SerializeField] or public access (you can still use C#'s properties to encapsulate your class/structure): [Serializable] public class Mission { ...


3

https://docs.unity3d.com/Manual/JSONSerialization.html The documentation has very detailed explaination. In your case the classes/structures (that store data ) should look like: [Serializable] public class Payload { public string imageUrl; public string text; } [Serializable] public class Data { public int id; public Payload payload; } ...


3

It won't find the path because Android has a different path when it comes to streaming assets. Your code won't work, whereas the below will: if (full_path.Contains("://") || full_path.Contains(":///")) { UnityWebRequest file = new UnityWebRequest(full_path); while (!file.isDone) { } json = Convert.ToString(file.downloadHandler.data).Trim(); } ...


3

Well I fixed my issue that I was having. Instead of using libGDX Json to write to file from the class I used JSONObject and used put() and surrounded it with try and catch to write the classes now the file is pretty and complete with the data that I want to pass. if(!file.exists()) { JSONObject jobj = new JSONObject(); try ...


3

com.badlogic.gdx.utils.GdxRuntimeException: File not found: skin\glassy-ui.json (Classpath) The json reader can't find your file, you can't fix that by changing the files content. The first problem is that you're using the classpath file locator, it's described as "should be avoided" in the wiki: They have their purpose, but should be avoided if ...


2

Expanded post on comment:"JSON is just a means of storing text information right? Why not just hold a path to the sprite in the JSON file? Then whenever you obtain the information in the JSON file, call Resources.Load (jsonPath); ?" The JSON language cannot directly load Sprites into Unity3D. However, as JSON can deal with strings, we can quite easily hold ...


2

You should use option 1, which has 3 advantages: It's easier to instantiate a resource - instead of having to pass the type of the resource as a parameter, you would just do, for example, new Gold(50) If you need to give special behaviour to a resource, you can by changing it's class. It's easier to check if a resource is a certain type - resource ...


2

You say you want to use data.series.points[0].ts but then you... don't, you use data.series.points.get_Item[0].ts The thing you say you want to use works perfectly fine so let's dig into why the other thing is not working: get_Item is a function, not a field or property. So you need to call it with (0) and not with [0] get_Item is a magical function that ...


2

The strings are on initial value because you never set data.payload.option to anything. You set up a variable Payload2 data2, but you never assign it in any way to anything of the variable data. Try replacing data2.option = new Option () { with data.payload.option = new Option () { By the way: I have no idea what the line string dataValue =data+ data2....


2

to remove class information, use fileJson.setTypeName(null) to display all infomation including default value (in your case "false" is default of boolean), use fileJson.setUsePrototypes(false); see more in documents


2

JSON is a string-based format. When you change the length of something in a JSON file (for example by changing "CurrentHP": 99 to "CurrentHP": 101 everything which comes after that in the JSON file needs to be rewritten because it's now at the wrong offset. That's how string-based file formats work. For that reason JSON libraries rarely support in-place ...


2

1: TOTAL overhead. Like driving a 40 ton truck to your weekly shopping for you and your spouse. Adds a lot of memory eating high end functionality into what is essentially a configuration file. If you consider that the best fit to store some small amount of data - you really need a reality check. Or have thousands of weapons. 3: you start hating it the ...


2

This is the keyboard input method that I have been using in javascript. First define the keyboard function, which I have shamelessly copied from somewhere (I'm honestly not who the original author was but it appears on StackOverflow a few times). function keyboard(keyCode) { var key = {}; key.code = keyCode; key.isDown = false; key.isUp =...


2

It will simply done with JsonUtility.FromJson<JsonClass>(jsonString) Use [Serializable] before each class for serialization and use Array[] instead of List<>. According to given json your class should like this using System; [Serializable] public class JsonClass { public Data[] data; } [Serializable] public class Data { public int ...


2

Let's assume somewhere in your code you do something like this to open the external JSON file, read its text, and parse it into a structured Dialogue object representation in memory. DialogueData dialogue = DialogueUtility.ParseJSONFromPath(filepath); Since you already have the file I/O, parsing, and validation covered above, I'll elide those parts in this ...


1

Currently Unity 5's JSON utility is not fully capable of doing all the neat tricks you might expect. For instance, it can't do an array of objects that contain other arrays. You can hack the json string with wrapper classes to make it work for that case. For your Dictionary class it may be the same issue. I don't know how to hack that to make it work, but ...


1

I was able to find a solid solution at this link I put the static method in my server public static string GetXMLFromObject(object o) { StringWriter sw = new StringWriter(); XmlTextWriter tw = null; try { XmlSerializer serializer = new XmlSerializer(o.GetType()); tw = new XmlTextWriter(sw); serializer.Serialize(tw, o)...


1

public class GameController : MonoBehaviour { public static GameController instance; //public InterstitialAd interstitialAd; TextAsset textAsset; DataReader dataReader; List<float> xList = new List<float>(); private void Awake() { textAsset = Resources.Load<TextAsset>("level1"); print("...


1

Remove the curly braces from your values. { "x": [1,1,1,1], "y": [1,1,1,1], "z": [1,2,3,4] } Though I'd also recommend for sanity sake to group by point and not by axis. [Serializable] public class Point { public float x; public float y; public float z; } [Serializable] public class DataReader { public Point[] points; } { "points": [ { ...


1

What you are looking for is probably an Array, unless you want to name each of your wall parts. { "walls": [ { "x": 1, "y": 1, "z": 1 }, { "x": 2, "y": 1, "z": 1 }, { "x": 3, "y": 1, "z": 1 }, { "x": 3, "y": 1, "z": 2 } ] } Couple of quick tools to ...


1

Directly converting these objects using JSONUtil won't work in this case. What you could do instead is parse it into an intermediate data-structure first. Create a struct which directly mimics the structure of that JSON code (with a category variable which is another struct with the fields type and subtype). You can then use the data in that structure to ...


1

This is definitely overkill. I'm going to liken this to drinking a glass of water out of 5 gallon bucket. Because I think unlike TomTom's answer, although apt, I'd like to get the point across that this is technically a valid and at times even the best solution, in the circumstance that you need to drink the entire 5 gallons. Lets suppose that based on you ...


1

I had a NavMeshAgent attached to my player from a previous prototype of a movement type for the game and it was interfering with where the player was positioned.


Only top voted, non community-wiki answers of a minimum length are eligible