consegue juntar AddBackground e AddPanel em uma só?
using System.Collections.Generic;
using Oxide.Core.Plugins;
using Oxide.Game.Rust.Cui;
using UnityEngine;
To combine the AddBackground and AddPanel methods into a single method, you can create a more generalized method that takes additional parameters to control the specifics of each panel, including whether or not it should function as a background with blur, the container to which it should be added, its anchors, panel name, color, and any additional properties that may differ between a standard panel and a background panel.
This new method, AddPanelOrBackground, can add both background panels and regular panels depending on the parameters passed to it. By using this method, you can simplify the UI generation logic and avoid code duplication.
Sure, you can modify the AddPanelOrBackground method to take the material and sprite as parameters. This will allow the method to be more flexible and reusable for any specific cases where these properties might need different values.
Here's how you can do this:
csharp
private void AddPanelOrBackground(
CuiElementContainer container,
string anchorMin,
string anchorMax,
string panelName,
string parent = "Overlay",
string color = "0 0 0 1",
bool isBackground = false,
bool cursorEnabled = false,
string material = null,
string sprite = null)
{
var panel = new CuiPanel
{
Image = { Color = color },
RectTransform = { AnchorMin = anchorMin, AnchorMax = anchorMax },
CursorEnabled = cursorEnabled
};
if (isBackground)
{
panel.Image.Material = material ?? "assets/content/ui/uibackgroundblur.mat";
panel.Image.Sprite = sprite ?? "assets/content/ui/ui.background.transparent.radial.psd";
}
else
{
panel.Image.Material = material;
panel.Image.Sprite = sprite;
}
container.Add(panel, parent, panelName);
}
// Usage
private void HandleOpenUiCommand(BasePlayer player)
{
RemoveUiFromPlayer(player);
var container = new CuiElementContainer();
// Add a background with default material and sprite
AddPanelOrBackground(
container,
"0 0",
"1 1",
"QuarryUi_Background",
color: "0.5 0.5 0.5 0.9",
isBackground: true,
cursorEnabled: true
);
// Add a panel within the background, without material and sprite
AddPanelOrBackground(
container,
"0.235 0.26",
"0.4985 0.724",
"QuarryUi_Full_Left",
parent: "QuarryUi_Background"
);
AddPanelOrBackground(
container,
"0.4995 0.26",
"0.765 0.724",
"QuarryUi_Full_Right",
parent: "QuarryUi_Background"
);
// Add further elements as needed
AddInnerSquare(container, "QuarryUi_Full_Left", "0.05 0.05", "0.45 0.45", "0.8 0.2 0.2 1");
AddInnerSquare(container, "QuarryUi_Full_Left", "0.55 0.55", "0.95 0.95", "0.2 0.2 0.8 1");
AddInnerSquare(container, "QuarryUi_Full_Right", "0.05 0.05", "0.45 0.45", "0.8 0.8 0.2 1");
AddInnerSquare(container, "QuarryUi_Full_Right", "0.55 0.55", "0.95 0.95", "0.2 0.8 0.2 1");
AddCloseButton(container, "QuarryUi_Full_Right");
CuiHelper.AddUi(player, container);
player.SetPlayerFlag(BasePlayer.PlayerFlags.Spectating, true);
}
In this implementation, both material and sprite are optional parameters for the new method. If they are not provided for the background panel, default values are used. For non-background panels, you can specify them as needed, or simply leave them null if not required.