For localizing my game into asian languages I’m finding I need to have my database be encoded in UTF-8. Right now it’s encoded in ANSI.
My code for creating and saving my file (xml) looks like this:
private static void Create_MakeFile(string filePath, dynamic baseObject, Type type)
{
XmlSerializer xmlSerializer = new XmlSerializer(type);
FileStream fileStream = new FileStream(filePath, FileMode.Create);
xmlSerializer.Serialize(fileStream, baseObject);
fileStream.Close();
#if UNITY_EDITOR
AssetDatabase.Refresh();
#endif
}
…but now, you’re not able to use FileStream with UTF-8, are you? Why is this? So, to have a game that will have compatible data with asian languages, I’m going to need to use Unicode only with StreamWriter/Reader right?
Okay, so any future people wanting to find the solution, here’s the code that uses StreamWriter and StreamReader instead of FileStream. FileStream can’t write to UTF-8 encoding that is required for Chinese and Japanese:
Ignore the ‘using MG’ part, that’s just part of my own SDK.
using System;
using System.IO;
using System.Xml.Serialization;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
using MG;
public class Methods_XML
{
public static dynamic ourObject;
public static void Create(string filePath, dynamic baseObject, Type type, bool overwrite)
{
if (overwrite)
{
if (File.Exists(filePath))
File.Delete(filePath);
Create_MakeFile(filePath, baseObject, type);
}
else
{
if (MG_Data.MG_DataFileExists())
{
Debug.LogError("Can not overwrrite MG_Data.data file!");
} else
{
Create_MakeFile(filePath, baseObject, type);
}
}
}
public static object Open(string FilePath, Type objType)
{
try
{
XmlSerializer serializer = new XmlSerializer(objType);
TextReader reader = new StreamReader(FilePath, System.Text.Encoding.UTF8);
object file;
file = (object)serializer.Deserialize(reader);
reader.Close();
return file;
} catch
{
Debug.LogError("Couldn't Open File at path: " + FilePath);
Debug.LogError("With ObjType: " + objType.GetType());
return null;
}
}
public static void Save(dynamic saveObject, string FilePath, bool overwrite)
{
try
{
if (overwrite)
{
if (File.Exists(FilePath))
{
File.Delete(FilePath);
Save_MakeFile(saveObject, FilePath);
}
} else
{
if (File.Exists(FilePath))
{
Debug.LogError("Can't overwrite xml file: " + FilePath);
}
else
{
Save_MakeFile(saveObject, FilePath);
}
}
} catch
{
Debug.LogError("Could not write ''" + saveObject + "'' to file: " + FilePath);
}
}
private static void Create_MakeFile(string filePath, dynamic baseObject, Type type)
{
XmlSerializer xmlSerializer = new XmlSerializer(type);
TextWriter writer = new StreamWriter(filePath, true, System.Text.Encoding.UTF8);
xmlSerializer.Serialize(writer, baseObject);
writer.Close();
#if UNITY_EDITOR
AssetDatabase.Refresh();
#endif
}
private static void Save_MakeFile(dynamic saveObject, string FilePath)
{
XmlSerializer serializer = new XmlSerializer(saveObject.GetType());
TextWriter writer = new StreamWriter(FilePath, true, System.Text.Encoding.UTF8);
serializer.Serialize(writer, saveObject);
writer.Close();
#if UNITY_EDITOR
AssetDatabase.Refresh();
#endif
}
}