Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by NiklasBorglund · Aug 10, 2012 at 12:22 AM · loadingresources

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
5
Best Answer

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

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eruure · Sep 09, 2013 at 12:30 AM 0
Share

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 :)

avatar image Eruure · Sep 09, 2013 at 12:38 AM 0
Share

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.

avatar image WiedemannD · Apr 27, 2015 at 04:20 PM 0
Share

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)

avatar image subaNicx · May 17, 2017 at 07:20 PM 0
Share

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).

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to load multiple wav files 5 Answers

iPhone build doesn't copy existing files in Application.dataPath or .persistantDataPath?! 0 Answers

Loading a resource file from external plugin dll 0 Answers

Loading Audioclip from Resources fails in Build 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges