问题
I recently went thru a small issue with my app, which basically downloads a string and an image from my website, now it works perfectly in the Unity Editor, but when I build and run, it doesn't print the image properly anymore! Here is the important code
byte[] data = sendMessage("https://mywebsite.com", values);
string ret = Encoding.Default.GetString(data);
var split = ret.Split(new string[] {"//"}, StringSplitOptions.None);
byte[] texdata = Encoding.Default.GetBytes(split[1]);
varstr = split[0];
texdata is used in the tex.LoadImage(); and varstr is saved for later, now it all works fine and dandy when I'm in the editor but when built, the image shows a big ? (the png is invalid) and varstr doesn't return properly. I am 100% sure it's the encoding, as when just doing
byte[] data = sendMessage("https://mywebsite.com", values);
byte[] texdata = data;
Where sendMessage() only returns the image and not the string, it works perfectly in build and in editor, moreso, I've tried string ret = Convert.ToBase64String(data); but then ret.Split(); returns
IndexOutOfRangeException: Array index is out of range.
So yeah, that's pretty much it, and I hope you have a nice day!
P.S: My website is php and sendMessage() works perfectly
回答1:
If it is an encoding issue within that code, look at Encoding.Default.EncodingName in the editor vs. during runtime. This will tell you if the default encodings are different.
As a potential solution, don't use the default. For example, use Encoding.UTF8. If you find out which encoding the Unity editor is using, you can use that.
回答2:
I did fix it by finding the encoding of the data my website sent me using echo mb_internal_encoding();
which is ISO-8859-1, and then I created Encoding ISO = Encoding.GetEncoding("ISO-8859-1");
and used that instead of Default and it worked just like i wanted it to!
来源:https://stackoverflow.com/questions/31256623/encoding-default-not-working-in-unity-player