Unturned

Unturned

Not enough ratings
UPDATED 11/2020: Referencing Scripts on Game Objects
By olie
This shows how to reference scripts from Unturned's assembly so you can make things like my throwing knife proof of concept. No writing code required but some experience needed.
   
Award
Favorite
Favorited
Unfavorite
Info
UPDATE:
If you just want to take advantage of this simply add the scripts found here[github.com] to your Unity project and then add them to the game object you want them on and configure them accordingly. If you want to find new scripts not covered in the repository follow the rest of the tutorial

P.S. Big thanks to Vlasoof[github.com] for bringing my guide and Unity's Animation event hook systems to Nelson! https://github.com/SmartlyDressedGames/Unturned-3.x-Community/issues/435

Here is the example setup for the KillVolume Ninja Star https://github.com/olie304/UnturnedScripts/blob/master/ThrowingStarExample.unitypackage

-----------------------------------------------------------------------------------------------------------------------------------

You may need some minor coding experience with object based programming before starting this guide. You will not be writing code during this tutorial, only reading it.

Some things you should know:
The main reason why this technique works is because Unity only keeps a single copy of compiled scripts in the actual assembly of the game and references the script whenever it is used. This means that when you add a script to a Game Object in the editor the script is not packaged with the Game Object; instead hashes of the signatures of the class and its exposable members are.

If you have ever done scripting with Game Objects in Unity you will know that public fields (and annotated private fields) can be initialized with values defined in the editor; I'm not entirely sure how these values are stored but they are integrated into the same data containing the hashed list of classes and members on the Game Object component.

Lastly, the part of Unity that does the script hashing is not done during the compilation of the script but is done when the asset is being compressed into the asset bundle.

All this means is that if you make a script with signatures identical to the compiled version, it will function the same as if it were packaged with the original game's asset bundle(s) and, if you set values on the exposed fields they will also be correctly packaged with your own assets.


A couple of interesting side notes:
The script will still work even if you do not include all of the signatures for the class members (unless of course it would run into an error for having one of them undefined). The only thing that matters is that the file name, class name and namespace match the original's exactly.

You can use types that are not just the Unity and primitive types (For an example Steamworks.CSteamID) as long as you create the script and add it the same way as the other ones. To use CSteamID as an example, since it has a public ulong field m_SteamID which contains the Steam ID for a player, we can define the m_SteamID field, add the script to a Game Object, set the m_SteamID field in the inspector to some value and then drag the script component into the field of another script which uses it such as SDG.Unturned.Rocket, which uses it to identify the killer of who ever is killed by the rocket. Think of creating a script component as initializing an object of the type of the script and that object can be used in other scripts.

Setting up your tools
You will need this tool[ilspy.net], or another C# decompiler, in order to locate the scripts that you want to use. Once it is downloaded open it up and do:
- File>Open
- "Steam Directory\steamapps\common\Unturned\Unturned_Data\Managed\Assembly-CSharp.dll"

Your screen should now look something like this:

Find the namespace "SDG.Unturned" and expand it.
What can I (not) use? And what's in it for me?
NOTE: You are allowed to skip this section and the next if you are not confused, find yourself adept at C#, are familiar with the Unity Engine, and know a bit about Unturned's source.

As far as I know you should be able to do things like add multiple features to objects as long as they don't require two overlapping tags in their .dat files. This means that for whatever reason you can make a mod where you have a bed that generates power! You should look for classes that automatically assign things to reasonable values. I got lucky when using KillVolumes to make my knife mod. As stated before you can probably use classes like InteractableTrap on InteractableFire types. Doing something like having an InteractableTrap on a VehicleAsset type would not work because the information that needs to be read in the .dat file is not remotely similar. Back to the KillVolume type, It is a really useful reference on what you can and can't use because it seems to be universal; you can put a KillVolume on anything you want.
The Unity part
Start by selecting your game Object in the scene.
Now do: Add component>New Script (Name it the EXACT same name as the script you want to use)

Next copy and past the entire class into this script you just added.

It is important that you ignore any errors! You must also never change the contents of the script or else it will not work! You are not writing a script, only preparing the file to pass a hash check Unity does to make sure the script you want is the same as this script it has.


Add the scripts from here[github.com] instead

If you are adding new scripts simply strip everything except for the definitions for non-static, non-constant public variables and enums.

It should look like this:


Lets fix it:
Voilà!
Please leave feedback and add any scripts you find to my repository https://github.com/olie304/UnturnedScripts Thank you.
12 Comments
Underwater Assassin 6 Sep, 2023 @ 9:48am 
sent a pull req to the github fixing a problem and adding new properties
Ongjming 15 May, 2023 @ 2:54am 
Sorry only till now I realised sticky grenade isn't actually sticky but it froze itself upon contact with something. So the sticky grenade script is actually working as it is (for items that can be thrown anyways, haven't tried others yet).
Ongjming 14 Oct, 2022 @ 2:43am 
@olie Not sure if you still view the message here, was wondering if you could give an example of where stickygrenade.cs might be usable at? I don't know how it could be used if it needed the item to be thrown and the only class that allows throwing is the throwables, and if it doesn't, does that mean I can stick the barricade in front/back of the vehicle and once it moves and hits an object/barricade/structure/ground with it the whole vehicle is stuck there?
Spebby 21 Aug, 2020 @ 5:31pm 
Underrated guide
Olie 24 Mar, 2020 @ 4:44pm 
Wait you're me
飘丝王 23 Mar, 2020 @ 5:11am 
Thank you so much. I've been looking for tutorials on how to make mods like this...
olie  [author] 5 Mar, 2020 @ 4:48pm 
@Kapec then you did it very wrong!
uberhunterprime 7 Dec, 2017 @ 3:17pm 
@olie Can you be a little more clear on the Unity setup, because what you mean by "copy and paste the entire class into the script", what I looking for, maybe add a more picture between steps.
uberhunterprime 7 Dec, 2017 @ 1:39pm 
@ThΞϾubicNoobik What is the class for NightLight?
CyberAndrii 14 Nov, 2017 @ 2:35am 
NightLight works. Thanks for the guide