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?

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:
https://web.archive.org/web/20130613152642/http://www.crywolfstudios.net/medieval-zombies-blog/2012/10/8/how-to-add-localization-to-your-unity3d-game.html

Expanded version, which also supports .bytes

AssetImportRenamer.cs