Files
trail-into-darkness/Packages/com.jovian.popup-system/Samples~/Scripts/CodeOnlyPopupExample.cs
2026-04-06 17:30:09 +02:00

64 lines
2.1 KiB
C#

using Jovian.PopupSystem;
using Jovian.PopupSystem.UI;
using UnityEngine;
/// <summary>
/// Example: Showing popups from code without PopupTrigger components.
///
/// Use this approach for confirmation dialogs, tutorial tips, or any popup
/// that is triggered by game logic rather than hover events.
/// </summary>
public class CodeOnlyPopupExample : MonoBehaviour {
[SerializeField] PopupSettings popupSettings;
[SerializeField] PopupReference popupReferencePrefab;
[SerializeField] Transform canvasRoot;
[SerializeField] RectTransform targetElement;
IPopupSystem popupSystem;
void Start() {
popupSystem = new PopupSystem(popupSettings, popupReferencePrefab, canvasRoot);
popupSystem.RegisterCategory(PopupCategory.General, priority: 1);
}
void Update() {
popupSystem?.Tick(Time.deltaTime);
// Show anchored to an element on key press
if(Input.GetKeyDown(KeyCode.Alpha1)) {
popupSystem.Show(PopupCategory.General, builder => {
builder
.AddHeader("Anchored Popup")
.AddText("This popup is anchored to a UI element.");
}, targetElement, AnchorSide.Right);
}
// Show at a fixed screen position
if(Input.GetKeyDown(KeyCode.Alpha2)) {
popupSystem.ShowAtPosition(PopupCategory.General, builder => {
builder
.AddHeader("Fixed Position")
.AddText("This popup appears at the center of the screen.");
}, new Vector2(Screen.width * 0.5f, Screen.height * 0.5f));
}
// Show following the mouse
if(Input.GetKeyDown(KeyCode.Alpha3)) {
popupSystem.Show(PopupCategory.General, builder => {
builder
.AddHeader("Follow Mouse")
.AddText("This popup follows the cursor.");
});
}
// Hide on key press
if(Input.GetKeyDown(KeyCode.Escape)) {
popupSystem.HideAll();
}
}
void OnDestroy() {
popupSystem?.Dispose();
}
}