forked from Shardstone/trail-into-darkness
added documentation, fixed some bugs
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
using Jovian.PopupSystem;
|
||||
using Jovian.PopupSystem.UI;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// Example: Using PopupSystem with dynamically instantiated UI elements.
|
||||
///
|
||||
/// When UI elements are created at runtime (e.g. inventory slots, party member portraits),
|
||||
/// use InitializeTriggersInChildren to scan and bind triggers after instantiation.
|
||||
/// </summary>
|
||||
public class DynamicTriggersExample : MonoBehaviour {
|
||||
[SerializeField] PopupSettings popupSettings;
|
||||
[SerializeField] PopupReference popupReferencePrefab;
|
||||
[SerializeField] Transform canvasRoot;
|
||||
[SerializeField] Transform slotsContainer;
|
||||
[SerializeField] GameObject slotPrefab;
|
||||
|
||||
IPopupSystem popupSystem;
|
||||
|
||||
void Start() {
|
||||
popupSystem = new PopupSystem(popupSettings, popupReferencePrefab, canvasRoot);
|
||||
popupSystem.RegisterCategory(PopupCategory.Item, priority: 5);
|
||||
|
||||
// Simulate creating dynamic UI slots
|
||||
for(int i = 0; i < 5; i++) {
|
||||
Instantiate(slotPrefab, slotsContainer);
|
||||
}
|
||||
|
||||
// Scan the container for any PopupTrigger components on the new slots.
|
||||
// Each trigger is automatically bound to the popup system.
|
||||
// The configure callback lets you set content per trigger.
|
||||
popupSystem.InitializeTriggersInChildren(slotsContainer, trigger => {
|
||||
var slotName = trigger.gameObject.name;
|
||||
trigger.SetContent(builder => {
|
||||
builder
|
||||
.AddHeader(slotName)
|
||||
.AddSeparator()
|
||||
.AddText("This is a dynamically created slot.");
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
void Update() {
|
||||
popupSystem?.Tick(Time.deltaTime);
|
||||
}
|
||||
|
||||
void OnDestroy() {
|
||||
popupSystem?.Dispose();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
using Jovian.PopupSystem;
|
||||
using Jovian.PopupSystem.UI;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// Example: Setting up PopupSystem with auto-scanned triggers.
|
||||
///
|
||||
/// 1. Attach this to a GameObject in your scene.
|
||||
/// 2. Assign the PopupSettings asset and PopupReference prefab.
|
||||
/// 3. Set canvasRoot to the root Canvas that contains your UI and PopupTrigger components.
|
||||
/// 4. Place PopupTrigger components on any UI elements that should show popups on hover.
|
||||
/// 5. The system auto-scans triggers on creation. Use SetContent() to provide data.
|
||||
/// </summary>
|
||||
public class PopupSystemExample : MonoBehaviour {
|
||||
[SerializeField] PopupSettings popupSettings;
|
||||
[SerializeField] PopupReference popupReferencePrefab;
|
||||
[SerializeField] Transform canvasRoot;
|
||||
|
||||
IPopupSystem popupSystem;
|
||||
|
||||
void Start() {
|
||||
// Create the system. Passing canvasRoot auto-scans all PopupTrigger components.
|
||||
popupSystem = new PopupSystem(popupSettings, popupReferencePrefab, canvasRoot);
|
||||
|
||||
// Register categories before showing popups.
|
||||
popupSystem.RegisterCategory(PopupCategory.Character, priority: 10);
|
||||
popupSystem.RegisterCategory(PopupCategory.Item, priority: 5);
|
||||
popupSystem.RegisterCategory(PopupCategory.General, priority: 1);
|
||||
|
||||
// Option A: Set content on an auto-scanned trigger by GameObject name.
|
||||
var characterTrigger = popupSystem.GetTrigger("CharacterPortrait");
|
||||
if(characterTrigger != null) {
|
||||
characterTrigger.SetContent(builder => {
|
||||
builder
|
||||
.AddHeader("Kael")
|
||||
.AddText("Human Warrior", "CCCCCC")
|
||||
.AddSeparator()
|
||||
.AddStat("Health", 55)
|
||||
.AddStat("Mana", 42)
|
||||
.AddStat("Level", 1)
|
||||
.AddSeparator()
|
||||
.AddStat("Might", 8)
|
||||
.AddStat("Reflex", 2)
|
||||
.AddStat("Knowledge", 5)
|
||||
.AddStat("Perception", 1);
|
||||
});
|
||||
}
|
||||
|
||||
// Option B: Set content on all triggers of a category.
|
||||
foreach(var trigger in popupSystem.GetTriggers(PopupCategory.Item)) {
|
||||
trigger.SetContent(builder => {
|
||||
builder
|
||||
.AddHeader("Health Potion")
|
||||
.AddSeparator()
|
||||
.AddText("Restores a moderate amount of health.")
|
||||
.AddStat("Heal Amount", 50);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void Update() {
|
||||
// Tick drives delay timers and animations.
|
||||
popupSystem?.Tick(Time.deltaTime);
|
||||
}
|
||||
|
||||
void OnDestroy() {
|
||||
// Clean up all popup views.
|
||||
popupSystem?.Dispose();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user