Last active
August 21, 2019 15:46
-
-
Save shanecelis/5c38ce90cca057e857f415108f5ac75a to your computer and use it in GitHub Desktop.
Import files with the extensions you specify as text assets in Unity 3D.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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