Creating a custom item
This guide will teach you how to add a custom item in your mod. Preferably, you should follow Creating a mod first.
Your mod folder should now only contain a mod.json
file and a script.cs
, which should look like this:
using UnityEngine; //You'll probably need this...
namespace Mod
{
public class Mod
{
public static void Main()
{
//This method is the entry point.
}
}
}
Adding a new item is really quite simple. All you need to do is call the ModAPI.Register
function and pass the appropriate arguments.
This snippet of code is (kind of) from the snippets repository, but it is a very clear example of what the Register
function expects.
// register item to the mod api
ModAPI.Register(
new Modification()
{
OriginalItem = ModAPI.FindSpawnable("Brick"), //item to derive from
NameOverride = "Blue Brick lmfao -BlueBrick", //new item name with a suffix to assure it is globally unique
DescriptionOverride = "Like a brick but differently coloured.", //new item description
CategoryOverride = ModAPI.FindCategory("Misc."), //new item category
ThumbnailOverride = ModAPI.LoadSprite("blueBrickView.png"), //new item thumbnail (relative path)
AfterSpawn = (Instance) => //all code in the AfterSpawn delegate will be executed when the item is spawned
{
Instance.GetComponent<SpriteRenderer>().sprite = ModAPI.LoadSprite("blueBrick.png"); //get the SpriteRenderer and replace its sprite with a custom one
}
}
);
Now to put this in your Main
function is super straightforward:
using UnityEngine;
namespace Mod
{
public class Mod
{
public static void Main()
{
ModAPI.Register(
new Modification()
{
OriginalItem = ModAPI.FindSpawnable("Brick"),
NameOverride = "Blue Brick lmfao -BlueBrick",
DescriptionOverride = "Like a brick but differently coloured.",
CategoryOverride = ModAPI.FindCategory("Misc."),
ThumbnailOverride = ModAPI.LoadSprite("blueBrickView.png"), //Doesn't exist yet!
AfterSpawn = (Instance) =>
{
Instance.GetComponent<SpriteRenderer>().sprite = ModAPI.LoadSprite("blueBrick.png"); //Doesn't exist yet!
}
}
);
}
}
}
When this is done, all you need to do now is actually create the blueBrickView.png
and blueBrick.png
images that are referenced in the code. An item thumbnail should be a 256x256 PNG image, and game sprites can be whatever size you want. It is recommended to have a regular in-game item sprite by your side to make sure you are scaling your item correctly. These files have to exist for the mod to work, so don't continue without creating these. They should be located in your mod folder.