forked from Shardstone/trail-into-darkness
Made the popup system a lot more generic
This commit is contained in:
@@ -7,6 +7,7 @@ using UnityEngine;
|
||||
///
|
||||
/// Use this approach for confirmation dialogs, tutorial tips, or any popup
|
||||
/// that is triggered by game logic rather than hover events.
|
||||
/// Also demonstrates PopupElementType variants and the generic Add() method.
|
||||
/// </summary>
|
||||
public class CodeOnlyPopupExample : MonoBehaviour {
|
||||
[SerializeField] PopupSettings popupSettings;
|
||||
@@ -14,6 +15,9 @@ public class CodeOnlyPopupExample : MonoBehaviour {
|
||||
[SerializeField] Transform canvasRoot;
|
||||
[SerializeField] RectTransform targetElement;
|
||||
|
||||
// Custom element type defined in game code
|
||||
static readonly PopupElementType BadgeElement = new("badge");
|
||||
|
||||
IPopupSystem popupSystem;
|
||||
|
||||
void Start() {
|
||||
@@ -24,12 +28,12 @@ public class CodeOnlyPopupExample : MonoBehaviour {
|
||||
void Update() {
|
||||
popupSystem?.Tick(Time.deltaTime);
|
||||
|
||||
// Show anchored to an element on key press
|
||||
// Show anchored to an element
|
||||
if(Input.GetKeyDown(KeyCode.Alpha1)) {
|
||||
popupSystem.Show(PopupCategory.General, builder => {
|
||||
builder
|
||||
.AddHeader("Anchored Popup")
|
||||
.AddText("This popup is anchored to a UI element.");
|
||||
.AddText("Anchored Popup", PopupElementType.Header)
|
||||
.AddText("This popup is anchored to a UI element.", PopupElementType.Text);
|
||||
}, targetElement, AnchorSide.Right);
|
||||
}
|
||||
|
||||
@@ -37,8 +41,8 @@ public class CodeOnlyPopupExample : MonoBehaviour {
|
||||
if(Input.GetKeyDown(KeyCode.Alpha2)) {
|
||||
popupSystem.ShowAtPosition(PopupCategory.General, builder => {
|
||||
builder
|
||||
.AddHeader("Fixed Position")
|
||||
.AddText("This popup appears at the center of the screen.");
|
||||
.AddText("Fixed Position", PopupElementType.Header)
|
||||
.AddText("This popup appears at the center of the screen.", PopupElementType.Text);
|
||||
}, new Vector2(Screen.width * 0.5f, Screen.height * 0.5f));
|
||||
}
|
||||
|
||||
@@ -46,12 +50,36 @@ public class CodeOnlyPopupExample : MonoBehaviour {
|
||||
if(Input.GetKeyDown(KeyCode.Alpha3)) {
|
||||
popupSystem.Show(PopupCategory.General, builder => {
|
||||
builder
|
||||
.AddHeader("Follow Mouse")
|
||||
.AddText("This popup follows the cursor.");
|
||||
.AddText("Follow Mouse", PopupElementType.Header)
|
||||
.AddText("This popup follows the cursor.", PopupElementType.Text);
|
||||
});
|
||||
}
|
||||
|
||||
// Hide on key press
|
||||
// Demonstrate variant elements
|
||||
// Requires "header_gold" and "separator_thick" entries in PopupSettings.elementPrefabs
|
||||
if(Input.GetKeyDown(KeyCode.Alpha4)) {
|
||||
popupSystem.Show(PopupCategory.General, builder => {
|
||||
builder
|
||||
.AddText("Legendary Item", PopupElementType.Header.Variant("gold"))
|
||||
.AddSeparator(PopupElementType.Separator.Variant("thick"))
|
||||
.AddNameValue("Damage", "150", PopupElementType.LabelValueText)
|
||||
.AddText("A weapon forged in dragon fire.", "FF6600", PopupElementType.Text);
|
||||
}, targetElement, AnchorSide.Below);
|
||||
}
|
||||
|
||||
// Demonstrate generic Add() for fully custom elements
|
||||
// Requires a "badge" entry in PopupSettings.elementPrefabs
|
||||
if(Input.GetKeyDown(KeyCode.Alpha5)) {
|
||||
popupSystem.Show(PopupCategory.General, builder => {
|
||||
builder.AddText("Custom Element", PopupElementType.Header);
|
||||
var badge = builder.Add(BadgeElement);
|
||||
if(badge != null) {
|
||||
// Access any components on the custom prefab
|
||||
// badge.GetComponent<MyBadgeComponent>().SetData(...);
|
||||
}
|
||||
}, targetElement);
|
||||
}
|
||||
|
||||
if(Input.GetKeyDown(KeyCode.Escape)) {
|
||||
popupSystem.HideAll();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user