После анализа кода, были обнаружены следующие ошибки:using System.Collections.Generic;
using Oxide.Core;
using Oxide.Core.Plugins;
using Oxide.Game.Rust.Cui;
using UnityEngine;
namespace Oxide.Plugins
{
[Info("CustomMenu", "YourName", "1.0.0")]
[Description("Добавляет пользовательское меню с вкладками и иконками.")]
class CustomMenu : RustPlugin
{
private const string MenuParentName = "CustomMenu";
private const string MenuButtonName = "CustomMenuButton";
private const string TabContainerName = "CustomTabContainer";
private const string TabButtonName = "CustomTabButton";
private const string TabIconName = "CustomTabIcon";
private const string TabTextName = "CustomTabText";
private readonly Dictionary<BasePlayer, CuiElementContainer> _menuElements = new Dictionary<BasePlayer, CuiElementContainer>();
private void Init()
{
foreach (var player in BasePlayer.activePlayerList)
{
CreateMenuElements(player);
}
}
private void Unload()
{
foreach (var player in BasePlayer.activePlayerList)
{
DestroyMenuElements(player);
}
}
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();
// Добавляем кнопку главного меню
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 = "Меню", FontSize = 18, Align = TextAnchor.MiddleCenter }
}, MenuParentName, MenuButtonName);
_menuElements[player] = menuContainer;
CuiHelper.AddUi(player, menuContainer);
}
private void DestroyMenuElements(BasePlayer player)
{
if (!_menuElements.TryGetValue(player, out var menuContainer)) return;
// Замена конкатенации строк на StringBuilder
StringBuilder buttonName = new StringBuilder(TabButtonName);
buttonName.Append(i);
StringBuilder iconName = new StringBuilder(TabIconName);
iconName.Append(i);
StringBuilder textName = new StringBuilder(TabTextName);
textName.Append(i);
tabContainer.Add(new CuiButtonComponent
{
Button = { Command = "custommenu.tab" + i, Color = "0.5 0.5 0.5 0.7" },
RectTransform = { AnchorMin = string.Format("0.01 {0:F}", yPos), AnchorMax = string.Format("0.11 {0:F}", yPos + 0.04f) },
Text = { Text = (i + 1).ToString(), FontSize = 18, Align = TextAnchor.MiddleCenter }
}, MenuParentName, "TabButton" + i + @"\n");
tabContainer.Add(new CuiElement
{
Name = iconName.ToString(),
Parent = MenuParentName,
Components =
{
new CuiRawImageComponent
{
Png = FileStorage.server.GetServerFile(string.Format("custommenu/icon{0}.png", i)),
Color = "1 1 1 1",
Sprite = "assets/content/textures/generic/fulltransparent.tga"
},
new CuiRectTransformComponent
{
AnchorMin = string.Format("0.12 {0:F}", yPos),
AnchorMax = string.Format("0.22 {0:F}", yPos + 0.04f)
}
}
});
[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;
tabContainer.Add(new CuiButtonComponent
{
Button = { Command = "custommenu.tab" + i.ToString(), Color = "0.5 0.5 0.5 0.7" },
RectTransform = { AnchorMin = string.Format("0.01 {0:F}", yPos), AnchorMax = string.Format("0.11 {0:F}", yPos + 0.04f) },
Text = { Text = (i + 1).ToString(), FontSize = 18, Align = TextAnchor.MiddleCenter }
}, MenuParentName, string.Format("{0}{1}", TabButtonName, i));
tabContainer.Add(new CuiElement
{
Name = string.Format("{0}{1}", TabIconName, i),
Parent = MenuParentName,
Components =
{
new CuiRawImageComponent
{
Png = FileStorage.server.GetServerFile(string.Format("custommenu/icon{0}.png", i)),
Color = "1 1 1 1",
Sprite = "assets/content/textures/generic/fulltransparent.tga"
},
new CuiRectTransformComponent
{
AnchorMin = string.Format("0.12 {0:F}", yPos),
AnchorMax = string.Format("0.22 {0:F}", yPos + 0.04f)
}
}
});
tabContainer.Add(new CuiLabel
{
Text = { Text = string.Format("Вкладка {0}", i + 1), FontSize = 16, Align = TextAnchor.MiddleLeft },
RectTransform = { AnchorMin = string.Format("0.23 {0:F}", yPos), AnchorMax = string.Format("0.6 {0:F}", yPos + 0.04f) }
}, MenuParentName, string.Format("{0}{1}", TabTextName, i));
}
_menuElements[player].Add(tabContainer, MenuParentName, TabContainerName);
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(10), out tabIndex)) return;
CuiHelper.DestroyUi(player, MenuParentName, TabContainerName);
CuiHelper.AddUi(player, new CuiElementContainer());
SendReply(player, string.Format("Вы открыли вкладку {0}., tabIndex + 1));
}
}
}
Пофикси ошибки в моём коде
1. В блоке кода метода DestroyMenuElements не была закрыта фигурная скобка после добавления компонентов для TabIcon и TabText.
2. В блоке кода метода OpenMenu была незакрытая фигурная скобка после создания объекта tabContainer.
3. В методе OpenTab, в строке SendReply была незакрытая кавычка.
Исправленный код:
Код:
using System.Collections.Generic;
using System.Text;
using Oxide.Core;
using Oxide.Core.Plugins;
using Oxide.Game.Rust.Cui;
using UnityEngine;
namespace Oxide.Plugins
{
[Info("CustomMenu", "YourName", "1.0.0")]
[Description("Добавляет пользовательское меню с вкладками и иконками.")]
class CustomMenu : RustPlugin
{
private const string MenuParentName = "CustomMenu";
private const string MenuButtonName = "CustomMenuButton";
private const string TabContainerName = "CustomTabContainer";
private const string TabButtonName = "CustomTabButton";
private const string TabIconName = "CustomTabIcon";
private const string TabTextName = "CustomTabText";
private readonly Dictionary<BasePlayer, CuiElementContainer> _menuElements = new Dictionary<BasePlayer, CuiElementContainer>();
private void Init()
{
foreach (var player in BasePlayer.activePlayerList)
{
CreateMenuElements(player);
}
}
private void Unload()
{
foreach (var player in BasePlayer.activePlayerList)
{
DestroyMenuElements(player);
}
}
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();
// Добавляем кнопку главного меню
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 = "Меню",
FontSize = 18,
Align = TextAnchor.MiddleCenter
}
}, MenuParentName, MenuButtonName);
_menuElements[player] = menuContainer;
CuiHelper.AddUi(player, menuContainer);
}
private void DestroyMenuElements(BasePlayer player)
{
if (!_menuElements.TryGetValue(player, out var menuContainer)) return;
for (var i = 0; i < 15; i++)
{
var yPos = 0.8f - i * 0.05f;
var tabButtonName = TabButtonName + i.ToString();
var tabIconName = TabIconName + i.ToString();
var tabTextName = TabTextName + i.ToString();
menuContainer.Remove(tabButtonName);
menuContainer.Remove(tabIconName);
menuContainer.Remove(tabTextName);
}
menuContainer.Remove(TabContainerName);
_menuElements.Remove(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(10), out tabIndex)) return;
CuiHelper.DestroyUi(player, MenuParentName, TabContainerName);
CuiHelper.AddUi(player, new CuiElementContainer());
SendReply(player, string.Format("Вы открыли вкладку {0}.", tabIndex + 1));
}
[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;
tabContainer.Add(new CuiButtonComponent
{
Button =
{
Command = "custommenu.tab" + i,
Color = "0.5 0.5 0.5 0.7"
},
RectTransform =
{
AnchorMin = string.Format("0.01 {0:F}", yPos),
AnchorMax = string.Format("0.11 {0:F}", yPos + 0.04f)
},
Text =
{
Text = (i + 1).ToString(),
FontSize = 18,
Align = TextAnchor.MiddleCenter
}
}, MenuParentName, TabButtonName + i.ToString());
tabContainer.Add(new CuiElement
{
Name = TabIconName + i.ToString(),
Parent = MenuParentName,
Components =
{
new CuiRawImageComponent
{
Png = FileStorage.server.GetServerFile(string.Format("custommenu/icon{0}.png", i)),
Color = "1 1 1 1",
Sprite = "assets/content/textures/generic/fulltransparent.tga"
},
new CuiRectTransformComponent
{
AnchorMin = string.Format("0.12 {0:F}", yPos),
AnchorMax = string.Format("0.22 {0:F}", yPos + 0.04f)
}
}
});
tabContainer.Add(new CuiLabel
{
Text =
{
Text = string.Format("Вкладка {0}", i + 1),
FontSize = 16,
Align = TextAnchor.MiddleLeft
},
RectTransform =
{
AnchorMin = string.Format("0.23 {0:F}", yPos),
AnchorMax = string.Format("0.6 {0:F}", yPos + 0.04f)
}
}, MenuParentName, TabTextName + i.ToString());
}
_menuElements[player].Add(tabContainer, MenuParentName, TabContainerName);
CuiHelper.AddUi(player, _menuElements[player]);
}
}
}