В строке 54 необходимо заменить
CuiButtonComponent
на
CuiElement
для добавления контейнера элементов внутрь контейнера меню.
Исправленный код для строки 54:
C#:
tabContainer.Add(new CuiElement
{
Name = TAB_CONTAINER_NAME,
Parent = MENU_PARENT_NAME,
Components =
{
new CuiRectTransformComponent { AnchorMin = "0.01 0.75", AnchorMax = "0.99 0.95" },
new CuiVerticalLayoutComponent { Padding = "5 5", Spacing = "0.5" }
}
});
В строке 56 необходимо заменить
$"{TAB_BUTTON_NAME}i"
на
$"{TAB_BUTTON_NAME}{i}"
, чтобы создавались уникальные имена для каждой кнопки таба.
Исправленный код для строки 56:
C#:
tabContainer.Add(new CuiButtonComponent
{
Button = { Command = $"custommenu.tab{i}", Color = "0.5 0.5 0.5 0.7" },
RectTransform = { AnchorMin = $"0 0", AnchorMax = $"1 1" },
Text = { Text = $"Tab {i + 1}", FontSize = 18, Align = TextAnchor.MiddleCenter }
}, TAB_CONTAINER_NAME, $"{TAB_BUTTON_NAME}{i}");
using System.Collections.Generic;
using Oxide.Core;
using Oxide.Game.Rust.Cui;
using Rust;
using UnityEngine;
namespace Oxide.Plugins
{
[Info("CustomMenu", "YourName", "1.0.0")]
[Description("Adds a custom menu with tabs and icons")]
class CustomMenu : RustPlugin
{
private const string MENU_PARENT_NAME = "CustomMenu";
private const string MENU_BUTTON_NAME = "CustomMenuButton";
private const string TAB_CONTAINER_NAME = "CustomTabContainer";
private const string TAB_BUTTON_NAME = "CustomTabButton";
private const string TAB_ICON_NAME = "CustomTabIcon";
private const string TAB_TEXT_NAME = "CustomTabText";
private readonly Dictionary<BasePlayer, CuiElementContainer> _menuElements = new Dictionary<BasePlayer, CuiElementContainer>();
private void Init() => BasePlayer.activePlayerList.ForEach(CreateMenuElements);
private void Unload() => BasePlayer.activePlayerList.ForEach(DestroyMenuElements);
private void OnPlayerInit(BasePlayer player) => CreateMenuElements(player);
private void OnPlayerDisconnected(BasePlayer player) => DestroyMenuElements(player);
private void CreateMenuElements(BasePlayer player)
{
if (_menuElements.ContainsKey(player)) return;
var menuContainer = new CuiElementContainer();
// Add the main menu button
menuContainer.Add(new CuiButtonComponent
{
Button = { Command = "custommenu.open", Color = "0.5 0.5 0.5 0.7" },
RectTransform = { AnchorMin = "0.01 0.85", AnchorMax = "0.11 0.95" },
Text = { Text = "Menu", FontSize = 18, Align = TextAnchor.MiddleCenter }
}, MENU_PARENT_NAME, MENU_BUTTON_NAME);
_menuElements[player] = menuContainer;
CuiHelper.AddUi(player, menuContainer);
}
private void DestroyMenuElements(BasePlayer player)
{
if (!_menuElements.TryGetValue(player, out var menuContainer)) return;
tabContainer.Add(new CuiElement
Name = TAB_CONTAINER_NAME,
Parent = MENU_PARENT_NAME,
Components =
{
new CuiRectTransformComponent { AnchorMin = "0.01 0.75", AnchorMax = "0.99 0.95" },
new CuiVerticalLayoutComponent { Padding = "5 5", Spacing = "0.5" }
}
});
_menuElements.Remove(player);
}
[ChatCommand("custommenu.open")]
private void OpenMenu(BasePlayer player)
{
var tabContainer = new CuiElementContainer();
for (var i = 0; i < 15; i++)
{
var yPos = 0.8f - i * 0.05f;
var index = i;
tabContainer.Add(new CuiButtonComponent
{
Button = { Command = $"custommenu.tab{index}", Color = "0.5 0.5 0.5 0.7" },
RectTransform = { AnchorMin = $"0.01 {yPos:F}", AnchorMax = $"0.11 {yPos + 0.04f:F}" },
Text = { Text = (i + 1).ToString(), FontSize = 18, Align = TextAnchor.MiddleCenter }
}, MENU_PARENT_NAME, $"{TAB_BUTTON_NAME}{index}");
tabContainer.Add(new CuiElement
{
Name = $"{TAB_ICON_NAME}{index}",
Parent = MENU_PARENT_NAME,
Components =
{
new CuiRawImageComponent
{
Png = FileStorage.server.GetServerFile($"custommenu/icon{index}.png"),
Color = "1 1 1 1",
Sprite = "assets/content/textures/generic/fulltransparent.tga"
},
new CuiRectTransformComponent
{
AnchorMin = $"0.12 {yPos:F}",
AnchorMax = $"0.22 {yPos + 0.04f:F}"
}
}
});
tabContainer.Add(new CuiLabel
{
Text = { Text = $"Tab {index + 1}", FontSize = 16, Align = TextAnchor.MiddleLeft },
RectTransform = { AnchorMin = $"0.23 {yPos:F}", AnchorMax = $"0.6 {yPos + 0.04f:F}" }
}, MENU_PARENT_NAME, $"{TAB_TEXT_NAME}{index}");
}
_menuElements[player].Add(tabContainer, MENU_PARENT_NAME, TAB_CONTAINER_NAME);
CuiHelper.AddUi(player, _menuElements[player]);
}
[ConsoleCommand("custommenu.tab")]
private void OpenTab(ConsoleSystem.Arg arg)
{
var player = arg.Player();
if (player == null) return;
int tabIndex;
if (!int.TryParse(arg.cmd.Name.Substring(13), out tabIndex)) return;
CuiHelper.DestroyUi(player, MENU_PARENT_NAME, TAB_CONTAINER_NAME);
CuiHelper.AddUi(player, new CuiElementContainer());
SendReply(player, $"You opened tab {tabIndex + 1}.");
}
}
}
Проанализируй код на ошибки