x


Change contents of TextAsset

I'm writing a custom editor that I want to edit TextAsset contents. What I have right now is a reference to a TextAsset, and what I would love to do is edit textAsset.text, and then call EditorUtility.SetDirty(textAsset) to write the changes to disk. Unfortunately, testAsset.text is readonly. I can't figure out a way to convert from a TextAsset to a system file path, either. Does anyone know how to do this? Any way that I could figure out the local file path of an object selected in the Project panel would be helpful as well.

Thanks!

more ▼

asked Feb 24 '10 at 06:54 AM

Azound gravatar image

Azound
668 19 22 39

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

I was not 100% clear if you were talking about editing the text asset from inside the editor or not. The below information is for modifying files on a run-time build (I did see that your tags included the editor)...

As far as I know the TextAssets are compiled into the same assets files as your other content; which means once the application is deployed it would be compiled into something that looks like this "sharedassets0.assets". There would be no easy way to change the text asset directly without some clever file manipulation.

An easier option would be to maybe store your text assets in another directory local to your project and instead of visually linking them up as assets you would just use the .NET IO API to load the text dynamically and then you could easily save any text changes you needed back to that file. This solution would prevent you from visibly linking them in the editor, but if you needed to assign them in the editor the easiest way would be to just have a string field where you put the file name for the text file and then your application would use that filename when loading your file from code.

more ▼

answered Feb 24 '10 at 07:04 PM

Shawn gravatar image

Shawn
323 5 6 13

If you are attempting to do this in the editor and would like to get the editor path to the raw text asset you can use the AssetDatabase.GetAssetPath() method:

http://unity3d.com/support/documentation/ScriptReference/AssetDatabase.GetAssetPath.html

Feb 24 '10 at 07:17 PM Shawn

AssetDatabase.GetAssetPath() is exactly what I needed. Thanks! Yes, this is for in-editor.

Feb 25 '10 at 05:49 AM Azound
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1679
x43

asked: Feb 24 '10 at 06:54 AM

Seen: 2499 times

Last Updated: Feb 24 '10 at 06:54 AM