Skip to content

Instantly share code, notes, and snippets.

@shanecelis
Last active Aug 21, 2019
Embed
What would you like to do?
Import files with the extensions you specify as text assets in Unity 3D.
/* Original code[1] Copyright (c) 2019 Shane Celis[2]
Licensed under the MIT License[3]
[1]: https://gist.github.com/shanecelis/5c38ce90cca057e857f415108f5ac75a
[2]: https://github.com/shanecelis
[3]: https://opensource.org/licenses/MIT
*/
using UnityEngine;
using UnityEditor.Experimental.AssetImporters;
using System.IO;
/** Do you want to import your own text files? Sure, we all do!
Unity natively supports text extensions: txt, html, htm, xml, bytes, json
csv, yaml, and fnt. These are imported as text assets.
This TextImporter script imports files with the extensions you specify
as text assets.
*/
[ScriptedImporter(0 /* Version number. Increment when script is changed. */,
new [] { "text", "markdown" } /* Filename extensions */)]
public class TextImporter : ScriptedImporter {
public override void OnImportAsset(AssetImportContext ctx) {
var asset = new TextAsset(File.ReadAllText(ctx.assetPath));
ctx.AddObjectToAsset("Main Object" /* This name won't be shown. The filename
will be shown for the main asset. */,
asset);
ctx.SetMainObject(asset);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment