Skip to content
This repository was archived by the owner on Nov 2, 2023. It is now read-only.

Available methods and how to use them.

ZedDevStuff edited this page Oct 4, 2022 · 11 revisions

How to use it?

To start using UIHelper, reference it in you project add using UKUIhelper; and [BepInDependency("zed.uk.uihelper")] to your script(s) (The attribute is necessary for the mod to work). All available methods are accessed with the UIHelper class.

"Create" Methods

The "Create" methods, as their name suggests, creates an object and returns it. Here is a list of currently implemented ones:

UIHelper.CreateButton();

UIHelper.CreateText();

UIHelper.CreateToggle();

UIHelper.CreateImage();

UIHelper.CreatePanel();

UIHelper.CreateInputField();

UIHelper.CreateSlider();

UIHelper.CreateScrollbar();

The CreateOverlay() method

The CreateOverlay() method is the only Create method with a parameter. It is set to false by default and allow you to make the canvas persistent across scenes (Applies DontDestroyOnLoad()). It returns a GameObject like all the other Create methods that has a Canvas, a CanvasScaler and a GraphicRaycaster.

Note: The canvas created by this method will always render on top of other UI elements in the game. To change that, use the sortingOrder property of the Canvas component;

The LoadSprite() method

LoadSprite loads an image from a path and transforms it into a ready-to-use sprite.

UIHelper.LoadSprite(string path,Vector4 border,float pixelsPerUnit)

RectTranform extension

UKUIHelper extends the RectTransform component to make things easier. The currently present features are

// Allows you to use anchor presets with code without touching anchorMin and anchorMax
RectTransform.SetAnchor(AnchorPresets)
// Allows you to control the pivot 
RectTransform.SetPivot(PivotPresets)
// Allows you to control the rect more precisely
RectTransform.SetRect(Rect4)

Rect4 is a struct comprised of 4 floats: top, bottom, left and right. They face inward, so take that into account when using it. If you use RectTransform.SetRect(), please refrain from using RectTransform.sizeDelta as they overwrite each other.

Exemple

using BepInEx;
using System.IO;
using UKUIHelper;
using UnityEngine;
using UnityEngine.UI;

namespace Exemple
{
    [BepInPlugin("Exemple", "Exemple", "1.0.0")]
    public class Plugin : BaseUnityPlugin
    {
        bool isImageActive = true;
        GameObject image,button,canvas;
        Sprite sprite;
        private void Awake()
        {
            // Create a canvas to render our UI elements
            canvas = CreateOverlay();

            // Loads an image from the executable folder
            sprite = UIHelper.LoadSprite(Directory.GetCurrentDirectory() + "image.png");

            // Create an image
            image = UIHelper.CreateImage();

            // Parent it to the canvas
            image.GetComponent<RectTransform>().SetParent(canvas.GetComponent<RectTransform>());

            // Anchor it to the top
            image.GetComponent<RectTransform>().SetAnchor(AnchorPresets.TopCenter);
            image.GetComponent<RectTransform>().SetPivot(PivotPresets.TopCenter);
            image.GetComponent<RectTransform>().anchoredPosition = new Vector2(0,0);
            image.GetComponent<RectTransform>().sizeDelta = new Vector2(200,200);
    
            // Assign the sprite to the image
            image.GetComponent<Image>().sprite = sprite;

            // Create a new button
            button = UIHelper.CreateButton();
            button.GetComponent<RectTransform>().SetAnchor(AnchorPresets.TopCenter);
            button.GetComponent<RectTransform>().anchoredPosition = new Vector2(0,-100);
            // Make it do stuff
            button.GetComponent<Button>().onClick.AddListener(() => ToggleImage());
        }
        void ToggleImage()
        {
            isImageActive = !isImageActive;
            image.SetActive(isImageActive);
        }
    }
}
 

Clone this wiki locally