forked from Shardstone/trail-into-darkness
Made the popup system a lot more generic
This commit is contained in:
@@ -9,7 +9,10 @@ using UnityEngine;
|
||||
/// 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.
|
||||
/// 5. The system auto-scans triggers on creation. Use GetTriggerHandler() to set content.
|
||||
///
|
||||
/// Element prefabs are configured in PopupSettings via PopupElementType.
|
||||
/// Add entries mapping types (header, text, label_value_text, separator, image) to prefabs.
|
||||
/// </summary>
|
||||
public class PopupSystemExample : MonoBehaviour {
|
||||
[SerializeField] PopupSettings popupSettings;
|
||||
@@ -19,52 +22,46 @@ public class PopupSystemExample : MonoBehaviour {
|
||||
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);
|
||||
});
|
||||
}
|
||||
// Set content on an auto-scanned trigger by GameObject name.
|
||||
var characterHandler = popupSystem.GetTriggerHandler("CharacterPortrait");
|
||||
characterHandler?.SetContent(builder => {
|
||||
builder
|
||||
.AddText("Kael", PopupElementType.Header)
|
||||
.AddText("Human Warrior", "CCCCCC", PopupElementType.Text)
|
||||
.AddSeparator(PopupElementType.Separator)
|
||||
.AddNameValue("Health", 55, PopupElementType.LabelValueText)
|
||||
.AddNameValue("Mana", 42, PopupElementType.LabelValueText)
|
||||
.AddNameValue("Level", 1, PopupElementType.LabelValueText)
|
||||
.AddSeparator(PopupElementType.Separator)
|
||||
.AddNameValue("Might", 8, PopupElementType.LabelValueText)
|
||||
.AddNameValue("Reflex", 2, PopupElementType.LabelValueText)
|
||||
.AddNameValue("Knowledge", 5, PopupElementType.LabelValueText)
|
||||
.AddNameValue("Perception", 1, PopupElementType.LabelValueText);
|
||||
});
|
||||
|
||||
// Option B: Set content on all triggers of a category.
|
||||
foreach(var trigger in popupSystem.GetTriggers(PopupCategory.Item)) {
|
||||
trigger.SetContent(builder => {
|
||||
// Set content on all triggers of a category.
|
||||
foreach(var handler in popupSystem.GetTriggerHandlers(PopupCategory.Item)) {
|
||||
handler.SetContent(builder => {
|
||||
builder
|
||||
.AddHeader("Health Potion")
|
||||
.AddSeparator()
|
||||
.AddText("Restores a moderate amount of health.")
|
||||
.AddStat("Heal Amount", 50);
|
||||
.AddText("Health Potion", PopupElementType.Header)
|
||||
.AddSeparator(PopupElementType.Separator)
|
||||
.AddText("Restores a moderate amount of health.", PopupElementType.Text)
|
||||
.AddNameValue("Heal Amount", 50, PopupElementType.LabelValueText);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
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