Topic category: User side tutorials
How to make a GUI with button images for MCreator 2022.2 (Should work in older mcreator versions) in forge 1.16.5
Before:
After:
1 - Create a GUI with buttons and assign a functional procedure to the buttons.
2 - Edit the GUI code and select the file that ends with Window.java
3 - In the imports add at the end the line:
import net.minecraft.client.gui.widget.button.ImageButton;
4 - In your class add this line:
private static final ResourceLocation MYIMAGE = new ResourceLocation("MODID:textures/(PATH WHERE IS YOUR TEXTURE)/.png");
Replace MODID with the mod id of your mod, then write your path where is your texture
5 - find the init() class
6 - comment each button with /* at the beginning and */ at the end
(optional, otherwise delete them, but note the numbers and needed text, text is shown in the 8th part)
7 - Add this code by button:
this.addButton(new ImageButton(this.guiLeft + 51, this.guiTop + 25, 10, 20, 0, 0, 20, MYIMAGE, e -> {
if (true) {
MODNAMEMod.PACKET_HANDLER.sendToServer(new CLASSNAMEGui.ButtonPressedMessage(0, x, y, z));
CLASSNAMEGui.handleButtonAction(entity, 0, x, y, z);
}
((ImageButton) e).setPosition(this.guiLeft + 51, this.guiTop + 25);
}));
8 - change ImageButton attributes for all buttons
9 - 8th part before and after code
Before:
After:
10 - Save and Lock (Press CTRL + W and CTRL + S, IMPORTANT you have to press CTRL + W for import to work or else you will get error)
11 - Texture it has to be 256x256, If you have different button textures you can put them anywhere but you will have to count where it is placed and write numbers in button code example: 12th part.
12 - Your button texture is in different place from tutorial
So check where your button texture begins (Aim at first top pixel of button) then look at mouse location in top left where you can see (px, px) currently (0, 0) and rewrite it in code. Example: 13th part.
13 - Rewrite numbers for button texture place.
14 - Example of changing button texture place
1. Check button texture placement:
2. Rewrite button texture code placement:
3. Save and Lock code and test it out:
Credits:
Main tutorial (1.17.1) - https://mcreator.net/forum/83507/added-image-buttons-guis
Help from Kleiders - https://mcreator.net/user/291988/urielito3010
Sick