Skip to content

Instantly share code, notes, and snippets.

@eduardoromeu
Last active November 22, 2023 01:51
Show Gist options
  • Save eduardoromeu/fa866268e4d64a6f14e99dd2cbba98d0 to your computer and use it in GitHub Desktop.
Save eduardoromeu/fa866268e4d64a6f14e99dd2cbba98d0 to your computer and use it in GitHub Desktop.
Item purchase dialog for unity
using UnityEngine;
using UnityEngine.UIElements;
[RequireComponent(typeof(UIDocument))]
public class PurchaseDialog : MonoBehaviour
{
[SerializeField] private UIDocument UIDocument;
[SerializeField] private string _itemText = "Do you really want to purchase item # for %?";
[SerializeField] private string _yesButtonName = "YesButton";
[SerializeField] private string _noButtonName = "NoButton";
[SerializeField] private string _okButtonName = "OkButton";
[SerializeField] private string _dialogLabelName = "AskLabel";
[SerializeField] private string _itemIndicator = "#";
[SerializeField] private string _costIndicator = "%";
private Label _dialogLabel;
private Button _yesButton;
private Button _noButton;
private Button _okButton;
private VisualElement _root;
public delegate void DialogCallback(bool purchased);
private DialogCallback _dialogCallback;
void Awake()
{
if(UIDocument != null) { UIDocument = GetComponent<UIDocument>(); }
_root = UIDocument.rootVisualElement;
_dialogLabel = _root.Q<Label>(_dialogLabelName);
_yesButton = _root.Q<Button>(_yesButtonName);
_noButton = _root.Q<Button>(_noButtonName);
_okButton = _root.Q<Button>(_okButtonName);
}
void Start()
{
_okButton.style.display = DisplayStyle.None;
SetVisible(false);
}
private void OnEnable()
{
_yesButton.clickable.clicked += () => Purchase(true);
_noButton.clickable.clicked += () => Purchase(false);
_okButton.clickable.clicked += () => SetVisible(false);
}
private void OnDisable()
{
_yesButton.clickable.clicked -= () => Purchase(true);
_noButton.clickable.clicked -= () => Purchase(false);
_okButton.clickable.clicked -= () => SetVisible(false);
}
private void Purchase(bool accepted)
{
if(_dialogCallback != null)
{
_dialogCallback(accepted);
}
SetVisible(false);
}
private void ShowButtons(bool yesButton, bool noButton, bool okButton)
{
_yesButton.style.display = (yesButton) ? DisplayStyle.Flex : DisplayStyle.None;
_noButton.style.display = (noButton) ? DisplayStyle.Flex : DisplayStyle.None;
_okButton.style.display = (okButton) ? DisplayStyle.Flex : DisplayStyle.None;
}
private void SetVisible(bool visible)
{
_root.style.display = visible ? DisplayStyle.Flex : DisplayStyle.None;
}
public void AskPurchase(DialogCallback _callback, string itemName, decimal itemCost)
{
_dialogCallback = _callback;
_dialogLabel.text = _itemText.Replace(_itemIndicator, itemName)
.Replace(_costIndicator, TextExtensions.FormatMoney(itemCost, "R$"));
ShowButtons(true, true, false);
SetVisible(true);
}
public void ShowInfo(string info)
{
_dialogCallback = null;
_dialogLabel.text = info;
ShowButtons(false, false, true);
SetVisible(true);
}
}
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" xsi="http://www.w3.org/2001/XMLSchema-instance" engine="UnityEngine.UIElements" editor="UnityEditor.UIElements" noNamespaceSchemaLocation="../../../../../UIElementsSchema/UIElements.xsd" editor-extension-mode="False">
<ui:VisualElement name="BoxContainer" class="dialog-box">
<ui:Label tabindex="-1" text="Do you really want to purchase item # for %?" parse-escape-sequences="true" display-tooltip-when-elided="true" name="AskLabel" class="text-white dialog-single-label" />
<ui:VisualElement name="ButtonsContainer" style="flex-grow: 1; justify-content: space-around; align-self: auto; align-items: center; flex-direction: row;">
<ui:Button text="No" parse-escape-sequences="true" display-tooltip-when-elided="true" name="NoButton" class="dialog-button UpperMenuOption" />
<ui:Button text="Yes" parse-escape-sequences="true" display-tooltip-when-elided="true" name="YesButton" class="dialog-button UpperMenuOption" style="display: flex;" />
<ui:Button text="OK" parse-escape-sequences="true" display-tooltip-when-elided="true" name="OkButton" class="dialog-button UpperMenuOption" style="display: none;" />
</ui:VisualElement>
</ui:VisualElement>
</ui:UXML>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment