Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Drag file with file extenion other than .txt to TextAsset in inspector

Discussion in 'Scripting' started by Issun, Jul 25, 2018.

  1. Issun

    Issun

    Joined:
    Nov 7, 2013
    Posts:
    17
    When having a TextAsset accessible from the inspector you are able to drag a .txt file into that and access that file in the script.

    I was wondering if it is possible to have this exact same functionality with a file that would otherwise be a .txt file but has my own custom file extension, lets say.yak for example, is it possible to drag my .yak file to the inspector and treat it just as a TextAsset object? Can this be done with some CustomEditor tinkering?

    Not sure if this Scripting section was best place for this question, please move if it is not, thank you.
    Thanks in advance.
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,575
    What happens if you drag that other yak file there instead? Have you tried?
     
  3. Issun

    Issun

    Joined:
    Nov 7, 2013
    Posts:
    17
    Just retested it to be sure in Unity 2018.20f2 you are only able to drag a .txt file to the TextAsset field in the inspector, tried my .yak file, other files, even a .rtf file is not able to be dragged there, the mouse cursor will turn into a black circle with a cross through it. (Windows 10)
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    No instant solutions. But a couple of hints.

    One trick I've seen is to just define the reference as broadly as possible, then use OnValidate to check if the asset actually belongs there. Something like UnityEngine.Object may work.

    The other option is to reference the asset via its filepath. Its likely you are going to want to do this eventually anyway (otherwise why care about the extension). You can use System.IO to open and manipulate any file.

    You could also build a tool to change the extension on all .yak files to .txt :p
     
  5. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,575
    As Kiwasi suggest, I would perhaps leave public text field in inspector to type in file name, (with, or without extension) is up to you. Then in the script, process that text input (file name) with IO module, to read/save file. You can automatically deal with your *.yak(s) extensions. Sounds like yolk.:)
     
  6. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    TextAsset only supports the following file extentions:

    .txt, .html, .htm, .xml, .bytes, .json, .csv, .yaml, .fnt

    If you don't want to rename your text files to match one of the extensions on that list (that would be the route I would take), I'd just use system IO to read them at runtime. But if you do that they will be external files you'll need to include with your build, rather than being serialized into your build as a TextAsset.

    https://docs.unity3d.com/Manual/class-TextAsset.html
    https://msdn.microsoft.com/en-us/library/ms143368(v=vs.110).aspx
     
    Laiken, Issun and Kiwasi like this.