- Home /
Resources.Load(), TextAsset, file extension
"Are there any ways to load an asset with Resources.Load() as TextAsset with a file that is not a common file extension for text?"
Hi!
I'm currently working on a small localization system for my web game and a friend told me about .resx files. I kind of liked how easy it was to edit and add text strings with that.(and it's really simple to add another language with this)
I've been trying to get a system around that file format in my game system the last few hours. Since .resx is basically an .xml file, I've been parsing the file as one and I got everything working just fine.
My problem is that I'm currently using the WWW - class to read the file into my system as plain text like this:
WWW www = new WWW("file://" + Application.dataPath + resourceFile + "." + language + ".resx");
This will only work in the editor for now, but to make it work I could host the .resx files on a server and make the www command point there, but I don't really want that.
Since I'm not changing anything in the .resx file at runtime, I would like to load the file in a way that looks something like this:
TextAsset textDocument = Resources.Load("/Localization/GameResources." + language) as TextAsset;
So I only want the .resx file to load from resources as plain text, but Resources.Load() don't like that command.
Does anyone know a way to make that happen?
Answer by NiklasBorglund · Oct 08, 2012 at 04:03 PM
I solved it by creating a .txt file in a subfolder when the .resx file changed. I created this editor script to solve it:
// Creates or rewrites a .txt file for each .resx file in the same folder
// whenever the .resx changes
using UnityEditor;
using UnityEngine;
using System.IO;
public class CustomResxImporter : AssetPostprocessor
{
public static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
{
foreach (string asset in importedAssets)
{
if (asset.EndsWith(".resx"))
{
string filePath = asset.Substring(0, asset.Length - Path.GetFileName(asset).Length) + "Generated Assets/";
string newFileName = filePath + Path.GetFileNameWithoutExtension(asset) + ".txt";
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
StreamReader reader = new StreamReader(asset);
string fileData = reader.ReadToEnd();
reader.Close();
FileStream resourceFile = new FileStream(newFileName, FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter writer = new StreamWriter(resourceFile);
writer.Write(fileData);
writer.Close();
resourceFile.Close();
AssetDatabase.Refresh(ImportAssetOptions.Default);
}
}
}
}
This way, I can still have the .resx functionality that I want. I anyone is interested, I did a small writeup on how to use this with a localization system here: http://www.crywolfstudios.net/medieval-zombies-blog/2012/10/8/how-to-add-localization-to-your-unity3d-game.html
For completeness - how to enable such a script in the Editor? Just adding it to the project doesn't seem to do the magic.
Sorry for a lame question :)
O$$anonymous$$, searched online even more, and found out the script needs to be in the Editor folder (which I have under Assets).
$$anonymous$$aybe useful for someone.
Awesome! Thank you. (Though I still think it's unreasonable not being able to add file extension to the list of 9. In my case it's .plist, which is also an xml file, but XCode only shows the Plist Editor with that extension)
I'm glad for this workaround but it really is ridiculous I can't just read .fods files directly. They're just X$$anonymous$$L files but I need the .fods extension so they can be opened easily in LibreOffice (I don't want to map all .xml file extensions to LibreOffice and have to do that on every developer's PC).