- Home /
How to make a custom TextAsset?
I simply write in ScriptedImporter
:
var t = new LuaScriptAsset(content);
ctx.AddObjectToAsset("Content", t);
where LuaScriptAsset is:
public class LuaScriptAsset : TextAsset { ... }
Unity looks not recognize the type "LuaScriptAsset".
In MonoBehaviour
script I write
public LuaScriptAsset script;
I can't drag my script asset to the object field in inspector; but using TextAsset
could do.
Answer by Pangamini · Jan 23 at 12:29 AM
I don't think you can extend the TextAsset class in this way, just like you can't extend other Unity's built-in classes (such as Collider). There are only few classes that derive from UnityEngine.Object that you can extend further (and have them properly used by the engine), such as MonoBehaviour, ScriptableObject and those derived from them (such as EditorWindow and StateMachineBehaviour). If you want to create a custom asset type, start from ScriptableObject, if you want to create a custom component, start from MonoBehaviour.
Lua file is text format source file. That's why I'm trying to extend TextAsset. It's neither ScriptableObject asset (yaml format .asset file), nor MonoBehaviour (C# source format .cs sfile). I simply want to attach some configuration to each lua file; Creating a scriptable object for each lua file could be messy.
Oh wait I misunderstand it. I can attach ScriptableObject to Lua file and set it to main... It works well..
It doesn't have to be. Either create a ScriptableObject that has a serialized reference to the lua TextAsset, or have a serialized string property with the actual lua code. Make the creation of them automatic by some editor scripts. But you really can't extend TextAsset class. Anyway, I always considered the benefits of using lua to be mostly that you can change them after the game is built, to load them dynamically (so players can mod the game, for example). Your textAssets will be baked into the build's resource files, and thus won't be present as separate *.lua files. If you want to achieve that, the easiest way is to use Strea$$anonymous$$gAssets and simply load the files ingame using System.IO.File or something.
Your answer
Follow this Question
Related Questions
How to assign variables like Texture2D, TextAsset, Transform, etc. using code? 1 Answer
Loading TextAsset returns null 1 Answer
Importing meshes during runtime from a server 0 Answers
Change pixels per unit for an imported asseet, but only on x axis 1 Answer
Import obj and mtl file with more textures than one? 0 Answers