C#:
using System;
using System.Collections.Generic;
using UnityEngine;
using Oxide.Core;
using Oxide.Core.Plugins;
using Oxide.Game.Rust.Cui;
namespace Oxide.Plugins
{
[Info("SimpleUiPanel", "D420", "1.2.3")]
[Description("Displays a simple UI panel by command and when a player enters a specific zone for each panel.")]
public class SimpleUiPanel : RustPlugin
{
#region Configuration
private ConfigData configData;
private class ConfigData
{
public List<PanelConfig> Panels { get; set; } = new List<PanelConfig>(); // List of panels
}
private class PanelConfig
{
public string Command { get; set; } = "panel1"; // Command to open this panel
public string Message { get; set; } = "Welcome to the server!"; // Message
public string Color { get; set; } = "#d60390"; // Default color (hex code)
public string TextColor { get; set; } = "#FFFFFF"; // Default text color
public double TextSize { get; set; } = 12.0; // Default text size
public double Duration { get; set; } = 5.0; // Duration in seconds the panel should display
public double AnchorMinX { get; set; } = 0.35; // X position (left anchor)
public double AnchorMinY { get; set; } = 0.5; // Y position (bottom anchor)
public double AnchorMaxX { get; set; } = 0.65; // X position (right anchor)
public double AnchorMaxY { get; set; } = 0.8; // Y position (top anchor)
public string ZoneID { get; set; } = "example_zone"; // Zone ID for this panel
}
protected override void LoadDefaultConfig()
{
configData = new ConfigData
{
Panels = new List<PanelConfig>
{
new PanelConfig
{
Command = "panel1",
Message = "welcome to simple UI panel! \nwhat does plug you can add your own text and \ncolor to individual panels and commands",
Color = "#d60390", // Default pink color
TextColor = "#FFFFFF", // Default text color
TextSize = 12.0, // Default text size
AnchorMinX = 0.35, // Left anchor
AnchorMinY = 0.5, // Bottom anchor
AnchorMaxX = 0.65, // Right anchor
AnchorMaxY = 0.8, // Top anchor
Duration = 10.0,
ZoneID = "zone1" // Example zone ID for panel 1
},
new PanelConfig
{
Command = "panel2",
Message = "Enjoy your stay!",
Color = "#00ff00", // Default green color
TextColor = "#FFFFFF", // Default text color
TextSize = 12.0, // Default text size
AnchorMinX = 0.35, // Left anchor
AnchorMinY = 0.5, // Bottom anchor
AnchorMaxX = 0.65, // Right anchor
AnchorMaxY = 0.8, // Top anchor
Duration = 5.0,
ZoneID = "zone2" // Example zone ID for panel 2
}
}
};
SaveConfig();
}
private void SaveConfig() => Config.WriteObject(configData, true);
private void LoadConfigValues() => configData = Config.ReadObject<ConfigData>();
protected override void LoadConfig()
{
base.LoadConfig();
LoadConfigValues();
}
#endregion
#region Hooks
private void Init()
{
// Register commands for each panel
foreach (var panel in configData.Panels)
{
cmd.AddChatCommand(panel.Command, this, nameof(OpenSpecificPanelCommand));
}
}
private void OnServerInitialized()
{
LoadConfigValues(); // Ensure config is loaded when the server starts
}
void OnEnterZone(string zoneID, BasePlayer player) // Called when a player enters a zone
{
for (int i = 0; i < configData.Panels.Count; i++)
{
if (configData.Panels[i].ZoneID.Equals(zoneID, StringComparison.OrdinalIgnoreCase))
{
ShowPanel(player, i); // Show the corresponding panel for the zone
return;
}
}
}
void OnExitZone(string zoneID, BasePlayer player) // Called when a player leaves a zone
{
for (int i = 0; i < configData.Panels.Count; i++)
{
if (configData.Panels[i].ZoneID.Equals(zoneID, StringComparison.OrdinalIgnoreCase))
{
CuiHelper.DestroyUi(player, "SimpleUiPanel"); // Hide the panel when the player exits the zone
return;
}
}
}
void OnEntityEnterZone(string zoneID, BaseEntity entity) // Called when an entity enters a zone
{
// Optionally handle entity zone entry if needed
}
#endregion
#region UI Creation
private void ShowPanel(BasePlayer player, int panelIndex)
{
if (panelIndex < 0 || panelIndex >= configData.Panels.Count) return;
CuiHelper.DestroyUi(player, "SimpleUiPanel");
var container = new CuiElementContainer();
var panelConfig = configData.Panels[panelIndex];
// Calculate the width based on the text length
float textWidth = CalculateTextWidth(panelConfig.Message, (float)panelConfig.TextSize);
float panelWidth = Math.Max(textWidth + 0.05f, 0.3f); // Minimum width for the panel
float panelHeight = 0.1f; // Fixed height, can be adjusted
// Main panel creation with fixed position
var panel = new CuiPanel
{
RectTransform = {
AnchorMin = $"{(float)panelConfig.AnchorMinX} {(float)panelConfig.AnchorMinY}",
AnchorMax = $"{(float)panelConfig.AnchorMaxX} {(float)panelConfig.AnchorMaxY}"
},
Image = { Color = HexToCuiColor(panelConfig.Color) },
CursorEnabled = false // No need for cursor since there are no buttons
};
var panelName = CuiHelper.GetGuid();
container.Add(panel, "Overlay", panelName);
// Text label
var label = new CuiLabel
{
Text =
{
Text = panelConfig.Message,
FontSize = (int)panelConfig.TextSize, // Explicit cast to int for FontSize
Align = TextAnchor.MiddleCenter,
Color = HexToCuiColor(panelConfig.TextColor)
},
RectTransform = { AnchorMin = "0 0", AnchorMax = "1 1" }
};
container.Add(label, panelName);
CuiHelper.AddUi(player, container);
// Remove the panel after the specified duration
timer.Once((float)panelConfig.Duration, () =>
{
CuiHelper.DestroyUi(player, panelName);
});
}
private float CalculateTextWidth(string text, float fontSize)
{
// Estimate text width based on font size and length of the text
return text.Length * (fontSize / 10f); // This is a rough estimate, you may need to adjust the factor based on actual appearance
}
#endregion
#region Commands
// Open specific panel using its unique command
private void OpenSpecificPanelCommand(BasePlayer player, string command, string[] args)
{
for (int i = 0; i < configData.Panels.Count; i++)
{
if (configData.Panels[i].Command.Equals(command, StringComparison.OrdinalIgnoreCase))
{
ShowPanel(player, i);
return;
}
}
}
#endregion
#region Helpers
private string HexToCuiColor(string hex)
{
Color color;
if (ColorUtility.TryParseHtmlString(hex, out color))
{
return $"{color.r} {color.g} {color.b} {color.a}";
}
return "1 1 1 1"; // Fallback to white color
}
#endregion
}
}