So you guys may have noticed me making posts and trying to get help on an issues with my sprites not loading up correctly and scaling up in size. I didn’t get any responses but I decided to try and solve the problem myself because the code in the tutorial was all correct and looked through it comparing it to my own code. I then typically went and noticed that my spacing didn’t match the code that was written.
I know how precise strings are especially when it involves the quotation marks, it’s not like other C# code where the formatting is really only helpful for reading purposes and doesn’t affect functionality. However when it comes to string am I right in thinking in that even an extra space or not can make a big different especially when you’re converting different variables to string?
I’ve still got to get everything set up again as I’m going through this but I think this will be what was responsible for my scaling issues because when I was just saving my gameobject positions it worked absolutely fine before.
Here’s the type of code I’m thinking of.
value = value.Trim (new char[] { '(', ')' }); // Before on this line I had no spaces at all.
value = value.Replace (" ", "");
string [] pos = value.Split (',');
These 2 strings are the same despite spacing in code.
var str = "hello";
var str2="hello";
These 2 strings are different because of spacing in the string.
var str = " hello ";
var str2="hello";
From the look of your code you appear to be taking a string that is formatted as (##,##,##…), and you want to parse out the numbers at ##. BUT there might be spaces between the numbers and the commas.
Is this throwing something off down the line? Is there something in your code where those spaces existing breaks your code?
Also, why are you reading in these values as a string? Is this part of your ‘game save’ format? Why not use a serialization engine that deals with all this ambiguity for you… like JsonUtility can convert serializable structs/classes to json for you easy peasy.
I’m just following this guys tutorials here and thanks for the post, I’m still getting used to this sort of code, but yes, there weren’t any spaces where there should have been so it could well have been that which was fucking things up. What this guy is doing is he’s taking out the commas from a set of coordinates, position, rotation and scale, then he’s packing them up without any commas and so on to be loaded up later.
I honestly had a careful look at Serializer assets and they just didn’t seem to do what I wanted, all I wanted literally was to just have this type of data saved and then load it up again through UI buttons.
I think it’s important to learn this type of thing anyway because if you want to just save specific types of data and so on Unity assets aren’t necessarily that customisable to allow that depending on who’s made them and so on, plus, it doesn’t help that the documentation and tutorials on a lot of the stuff I tried was really bad so it’s not like I could use them easily anyway.
ewwwww… yeah… nooooooo… don’t implement saving in that manner.
OK, unity added features to make this SUPER easy now a days…
Lets say you have a type that you want to save:
public struct SomeType
{
public string Name;
public float Score;
public Vector3 Location;
}
All you have to do is mark it serializable, convert to json string, and save.
[System.Serializable()]
public struct SomeType
{
public string Name;
public float Score;
public Vector3 Location;
}
//else where when you save:
//create some data
var data = new SomeType()
data.Name = "Smith";
data.Score = 1000000;
data.Location = new Vector3(1f,2f,3f);
//convert to json
string sdata = JsonUtility.ToJson(data);
//save it to a file
System.IO.File.WriteAllText(PATH_TO_SAVEFILE, sdata);
Loading is just as easy:
string sdata = System.IO.File.ReadAllText(PATH_TO_SAVEFILE);
var data = JsonUtility.FromJson<SomeType>(sdata);
Now, lets say you have some type that is third party that you want to save the state of. You don’t have the control of marking it serializable.
OK, create a token for it.
//class you can't serialize because not marked
public class ThirdPartyClass
{
public Vector3 Position;
public float Size;
}
[System.Serializable()]
public struct ThirdPartyClassToken
{
public Vector3 Position;
public float Size;
}
//else where:
ThirdPartyClass obj = *the object you want to serialize*;
var token = new ThirdPartyClassToken();
token.Position = obj.Position;
token.Size = obj.Size;
//convert to json
string sdata = JsonUtility.ToJson(data);
//save it to a file
System.IO.File.WriteAllText(PATH_TO_SAVEFILE, sdata);
Honestly, you usually create a large token for all your save data…
[System.Serializable()]
public struct SaveToken
{
public SomeType ScoreAndName;
public ThirdPartyClassToken[] AllThirdPartyClassObjects;
}
Or something similar.
JsonUtility.ToJson:
JsonUtility.FromJson:
Of course there are OTHER serialization engines that are even more robust.
Nice thing about that one, you don’t even have to mark your types as serializable… it’ll do its best job at figuring out what should and shouldn’t be serialized. While including attributes to fine tune it. It’s a very nice library…
There is also many built into the .net framework. I usually use the BinaryFormatter myself.
There’s also the Grfon by Joe Strout, a member of the forums here:
But honestly, the JsonUtility class included in unity is straight forward, fairly simple to use, and doesn’t require any extra libraries to be imported. Furthermore, since it’s officially part of the unity api, you’ll find more people knowledgable in it here on the forums.
Thanks, I’ll check it out lol WHY IS THIS DOCUMENTATION SO HARD TO FIND?! Absolutely typical that somebody else knows the right keywords and vocabulary to get the right stuff easily.
Guess I’ve got some more learning to do now but thanks for the very detailed response.
Finding your way around API documentation is a skill that takes years to learn.
It sucks when you have to deal with documentation that is put together rather poorly.
Unity is sort of in between… they have a lot of great documentation, but they also have a lot of ehhhhh documentation scattered around (looking at the UnityEditor documentation… ugh).
With practice you’ll learn, especially as you muck around with more and more APIs and their documentation. As of right now you should be getting intimate with the Unity API documentation:
And the MSDN .net API documentation:
Usually when googling for stuff for either I just say:
And read the top articles from unity themselves, of which the 6th one is:
In the end… I honestly would say the ability to research documentation, and the all important google-fu, is THE critical skill as a programmer. Languages are a dime a dozen, I know 20+, but my job stands primarily on my ability to read documentation effectively.
Welcome aboard! It’s worth keeping a set of spare mouses around for throwing.
One of the things to realise is that JSON utility is pretty new. So many people aren’t aware of it. Which leads to a bunch of old resources sitting around that are no longer in date.
But do future users a solid and go mention JSON utility in the comments to the video.
Okay, just a quick question, because I’m starting to work all of this out now thankfully, and you’re right, it does look much easier to do and even works in C#.
How will I encase the save and load parts of JSON into a public void function? That will pretty much nail what I want to do I think because of course then I’ll be able to put this into buttons. I don’t understand why the Unity staff hasn’t done a simple tutorial on this where you save the data on a cube or something hmm…
Oh and will JSON be comfortable with saving material changes and deleting gameobjects during runtime?
I think what I need is just a decent tutorial that goes into the basics of how to save a simple cube’s transform and texture -_-