Certainly! Below is a basic example of a MelonLoader mod that creates a simple UI menu using UnityEngine.UI. This menu should work in most Unity games that use the standard Unity UI system. The menu can be toggled with a key (e.g., F1), and you can expand it with your own buttons and logic.
Requirements:
- MelonLoader installed in your game.
- Reference
UnityEngine, UnityEngine.UI, and MelonLoader in your mod project.
Example: Universal UI Menu MelonLoader Mod
File: UniversalUIMenu.cs
using MelonLoader;
using UnityEngine;
using UnityEngine.UI;
[assembly: MelonInfo(typeof(UniversalUIMenu.Main), "Universal UI Menu", "1.0.0", "YourName")]
[assembly: MelonGame] // No game specified, works for any
namespace UniversalUIMenu
{
public class Main : MelonMod
{
private GameObject canvasObj;
private GameObject panelObj;
private bool menuVisible = false;
public override void OnUpdate()
{
// Toggle menu with F1
if (Input.GetKeyDown(KeyCode.F1))
{
menuVisible = !menuVisible;
if (panelObj != null)
panelObj.SetActive(menuVisible);
}
}
public override void OnSceneWasInitialized(int buildIndex, string sceneName)
{
if (canvasObj == null)
CreateMenu();
}
private void CreateMenu()
{
// Create Canvas
canvasObj = new GameObject("UniversalUIMenu_Canvas");
var canvas = canvasObj.AddComponent<Canvas>();
canvas.renderMode = RenderMode.ScreenSpaceOverlay;
canvasObj.AddComponent<CanvasScaler>().uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
canvasObj.AddComponent<GraphicRaycaster>();
GameObject.DontDestroyOnLoad(canvasObj);
// Create Panel
panelObj = new GameObject("MenuPanel");
panelObj.transform.SetParent(canvasObj.transform, false);
var image = panelObj.AddComponent<Image>();
image.color = new Color(0, 0, 0, 0.8f);
var rect = panelObj.GetComponent<RectTransform>();
rect.sizeDelta = new Vector2(400, 300);
rect.anchoredPosition = Vector2.zero;
// Add a sample button
var buttonObj = new GameObject("SampleButton");
buttonObj.transform.SetParent(panelObj.transform, false);
var button = buttonObj.AddComponent<Button>();
var btnImage = buttonObj.AddComponent<Image>();
btnImage.color = new Color(1, 1, 1, 1);
var btnRect = buttonObj.GetComponent<RectTransform>();
btnRect.sizeDelta = new Vector2(160, 40);
btnRect.anchoredPosition = new Vector2(0, -50);
// Add button text
var textObj = new GameObject("ButtonText");
textObj.transform.SetParent(buttonObj.transform, false);
var text = textObj.AddComponent<Text>();
text.text = "Click Me!";
text.font = Resources.GetBuiltinResource<Font>("Arial.ttf");
text.alignment = TextAnchor.MiddleCenter;
text.color = Color.black;
var textRect = textObj.GetComponent<RectTransform>();
textRect.sizeDelta = btnRect.sizeDelta;
textRect.anchoredPosition = Vector2.zero;
// Button click event
button.onClick.AddListener(() =>
{
MelonLogger.Msg("Button clicked!");
});
// Add a title
var titleObj = new GameObject("MenuTitle");
titleObj.transform.SetParent(panelObj.transform, false);
var titleText = titleObj.AddComponent<Text>();
titleText.text = "Universal UI Menu";
titleText.font = Resources.GetBuiltinResource<Font>("Arial.ttf");
titleText.alignment = TextAnchor.UpperCenter;
titleText.color = Color.white;
var titleRect = titleObj.GetComponent<RectTransform>();
titleRect.sizeDelta = new Vector2(380, 60);
titleRect.anchoredPosition = new Vector2(0, 110);
// Hide menu by default
panelObj.SetActive(menuVisible);
}
}
}
How to Use
- Build the DLL with references to
MelonLoader.dll, UnityEngine.dll, and UnityEngine.UI.dll.
- Place the DLL in your game's
Mods folder.
- Launch the game. Press F1 to toggle the menu.
Notes
- This is a basic template. You can add more UI elements (sliders, toggles, etc.) as needed.
- Some games may not include
UnityEngine.UI by default. If you get errors, you may need to bundle the UI DLL or use IMGUI instead.
- For more advanced UI, consider using Unity UI Toolkit or a third-party UI library.
Let me know if you want an IMGUI version (which works in all Unity games, even those without Unity UI)!