Skip to content

Using ModAPI in your mod

DipCrai edited this page Jun 21, 2024 · 7 revisions

Utils.LoadTexture(string fullPath, FilterMode filterMode = FilterMode.Point)

using UnityEngine;
using WorldBoxModLoader.ModAPI;

namespace SampleMod
{
    public class Mod
    {
        public static void Main()
        {
            GameObject sampleObject = new GameObject("SampleObject");
            SpriteRenderer spriteRenderer = sampleObject.AddComponent<SpriteRenderer>();
            Texture2D texture = Utils.LoadTexture("texture.png");
            spriteRenderer.material.SetTexture("_NORMALMAP", texture);
        }
    }
}

Utils.LoadSprite(string fullPath, FilterMode filterMode = FilterMode.Point, int pixelsPerUnit = 20)

using UnityEngine;
using WorldBoxModLoader.ModAPI;

namespace SampleMod
{
    public class Mod
    {
        public static void Main()
        {
            GameObject sampleObject = new GameObject("SampleObject");
            SpriteRenderer spriteRenderer = sampleObject.AddComponent<SpriteRenderer>();
            spriteRenderer.sprite = Utils.LoadSprite("sprite.png");
        }
    }
}

Utils.LoadSound(string fullPath)

using UnityEngine;
using WorldBoxModLoader.ModAPI;

namespace SampleMod
{
    public class Mod
    {
        public static void Main()
        {
            GameObject sampleObject = new GameObject("SampleObject");
            AudioSource audioSource = sampleObject.AddComponent<AudioSource>();
            audioSource.clip = Utils.LoadSound("sound.mp3");
        }
    }
}

Utils.CreateSimplePowerButton([NotNull] string buttonName, UnityAction buttonAction, Sprite buttonIcon)

Utils.GetTab(string tabName)

Utils.AddButtonToTab(PowerButton button, PowersTab tab, Vector2 position)

using UnityEngine;
using WorldBoxModLoader.ModAPI;

namespace SampleMod
{
    public class Mod
    {
        public static void Main()
        {
            GameObject sampleObject = new GameObject("SampleObject");
            SampleComponent sampleComponent = sampleObject.AddComponent<SampleComponent>();

            PowerButton powerButton = Utils.CreateSimplePowerButton("Multiplayer menu", sampleComponent.Toggle, Utils.LoadSprite("icon.png"));
            PowersTab powersTab = Utils.GetTab("Tab_Main");
            Utils.AddButtonToTab(powerButton, powersTab, Vector2.zero);
        }
    }
    public class SampleComponent : MonoBehaviour
    {
        public void Toggle()
        {
            gameObject.active = !gameObject.active;
        }
    }
}

Utils.CreateTab(string tabName, string titleKey, Sprite tabIcon)

using WorldBoxModLoader.ModAPI;

namespace SampleMod
{
    public class Mod
    {
        public static void Main()
        {
            PowersTab powersTab = Utils.CreateTab("TabName", "Sample Title", Utils.LoadSprite("TabIcon.png"));
        }
    }
}