ResourcesLoad text file not ended with .txt

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

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.

1 Like

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

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.

using System.IO;
using UnityEngine;
using UnityEditor.AssetImporters;

[ScriptedImporter( 1, "foo" )]
public class FooImporter : ScriptedImporter {
    public override void OnImportAsset( AssetImportContext ctx ) {
        TextAsset subAsset = new TextAsset( File.ReadAllText( ctx.assetPath ) );
        ctx.AddObjectToAsset( "text", subAsset );
        ctx.SetMainObject( subAsset );
    }
}
1 Like

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.

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.

3 Likes

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.

2 Likes

:smile:
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”.

2 Likes

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 ^^.

1 Like

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.

2 Likes