Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question ResourcesLoad text file not ended with .txt

Discussion in 'Scripting' started by wwu39, Mar 29, 2024.

  1. wwu39

    wwu39

    Joined:
    Nov 21, 2018
    Posts:
    30
    Is it possible to use Resources.Load to load a textasset not ended with .txt
    For example I want to load .ini or .cs files as text
     
  2. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,113
    No, the supported file extensions which are recognised as text assets are listed in the manual. The extension is pretty irrelevant anyways. Just use
    .txt
    or
    .ini.txt
    if you really want. Note that the extension
    .cs
    wouldn't make much sense anyways. C# files need to be compiled and that happens at build time. You can not simply "load" a C# file dynamically. So what's your actual goal?

    You could ship arbitrary files in the StreamingAssets folder. They are shipped with the game unaltered and they can be read through the UnityWebRequest. See the StreamingAssets manual. Though that doesn't change the fact that you have to handle the content of those files yourself.
     
    Ryiah likes this.
  3. wwu39

    wwu39

    Joined:
    Nov 21, 2018
    Posts:
    30
    inis are from some decade-old game containing object stats/rule set/settings that kind of stuff. I'm doing a remake of an old game that still adapt those old inis but I don't want player to touch them easily i.e. via streamassets
     
  4. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,473
    You're off by at least three decades. The oldest instance I'm aware of is MS-DOS in the early 80s. None of that is relevant here though. Unity's text asset importer is looking for specific extensions but once the asset is imported those extensions have no meaning at all and it will just refer to the file by the name.

    If you must have a custom text file format you can do it with a custom importer like this. Just be aware that like the builtin one once the file is imported that extension has no meaning and you won't be referring to it any more by that extension.
    Code (csharp):
    1. using System.IO;
    2. using UnityEngine;
    3. using UnityEditor.AssetImporters;
    4.  
    5. [ScriptedImporter( 1, "foo" )]
    6. public class FooImporter : ScriptedImporter {
    7.     public override void OnImportAsset( AssetImportContext ctx ) {
    8.         TextAsset subAsset = new TextAsset( File.ReadAllText( ctx.assetPath ) );
    9.         ctx.AddObjectToAsset( "text", subAsset );
    10.         ctx.SetMainObject( subAsset );
    11.     }
    12. }
     
    Last edited: Mar 29, 2024
    wwu39 likes this.
  5. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,486
    You could put them in a password protected zip file under StreamingAssets, if you don't mind embedding the zip password in the executable somewhere.

    There are tools that extract assets from a Unity build so I assume you are only looking to prevent the curious user from modifying the assets, not enthusiasts.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,033
    Well there's your problem!!

    You want water, but you don't want it to be wet.

    That's not a thing. And it will never be a thing.

    If you're concerned about the user 'hacking your save files,' or 'cheating in your game,' which is playing on their computer, just don't be.

    There's nothing you can do about it. Nothing is secure, it is not your computer, it is the user's computer.

    If it must be secure, store it on your own server and have the user connect to download it.

    Anything else is a waste of your time and the only person you're going to inconvenience is yourself when you're debugging the game and you have savegame errors. Work on your game instead.

    Remember, it only takes one 12-year-old in Finland to write a script to read/write your game files and everybody else can now use that script. Read about Cheat Engine to see more ways you cannot possibly control this.

    If you still feel compelled to waste your own time making water not wet, by all means be my guest. But no amount of forum posting or klever trickee leet koding will change the fact that water is wet: the user has the data, it's on their computer, they control it, they can read every single byte.
     
    Ryiah, GuirieSanchez and Bunny83 like this.
  7. GuirieSanchez

    GuirieSanchez

    Joined:
    Oct 12, 2021
    Posts:
    453
    You could use encryption to somehow obfuscate the data and deter some players go further, but any determined individual would be able to decrypt it anyway. As Kurt said, your best bet would be using server-side data validation or not caring at all if it's not a big deal for your project.
     
    Ryiah and Bunny83 like this.
  8. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,486
    :D
    Sorry for off-topic but this reminded me of a recent headline from The Times "Volcano in iceland erupting molten lava" and I said, they probably have another headline elsewhere like "Leakage flooded town with wet water".
     
    Ryiah and Kurt-Dekker like this.
  9. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,113
    Ok, so you want to include an ini file in your game and don't want the player to have "easy" access to it (they still can if they want, though). What exactly is your issue renaming the file to
    .txt
    and use it as TextAsset? Is there any reason you want to keep that ini extension inside your Unity project?

    Apart from that you may just convert / translate the information in that ini file into a more convenient format(JSON,ScriptableObject, ...). What game do we talk about? You do know that assets from other games have copyright as well, right? So just grabbing say the rules.ini of C&C Red Alert because it has well balanced units and buildings will just get you in trouble.

    ps: Note that all those settings in C&C:RA were stored internally. The rules.ini could be placed in the main folder to override the built-in values. This was a specific game feature / development feature which let you modify any of those values. With those you could easily cheat the game or even create some weird units like a soldier which has a battleship cannon and can shoot across half the map ^^.
     
    Ryiah likes this.
  10. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,473
    You don't even have to decrypt it yourself as the game will have to do so itself just to be able to use it. It's why when people want to steal textures or models from a game they do so straight from GPU as it can't render an encrypted asset so everything is stored in video memory in decrypted form.
     
    Bunny83 and GuirieSanchez like this.