119 lines
3.4 KiB
C#
119 lines
3.4 KiB
C#
//============= Copyright (c) Ludic GmbH, All rights reserved. ==============
|
|
//
|
|
// Purpose: Part of the My Behaviour Tree Controller Code
|
|
//
|
|
//=============================================================================
|
|
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
using MyBT;
|
|
|
|
#if UNITY_EDITOR
|
|
using UnityEditor;
|
|
[CustomEditor(typeof(NamedEventTrigger))]
|
|
public class NamedEventTriggerInspector : ComponentHandlerInspector {
|
|
}
|
|
#endif
|
|
|
|
[RequireComponent(typeof(EventTrigger))]
|
|
public class NamedEventTrigger : ComponentHandler {
|
|
public override string TypeLabel () {
|
|
return "NamedEventTrigger";
|
|
}
|
|
|
|
public override string ContentLabel() {
|
|
UpdateComponent();
|
|
return "";
|
|
}
|
|
|
|
public override void UpdateComponent() {
|
|
base.UpdateComponent();
|
|
|
|
if (GetComponent<Button>()) {
|
|
GetComponent<Button>().onClick.AddListener(PointerClickEventHandler);
|
|
}
|
|
// if it's not a button, check if it has a collider
|
|
else if (GetComponent<Collider>()) {
|
|
eventTrigger = gameObject.AddComponent<EventTrigger>();
|
|
EventTrigger.Entry entry = new EventTrigger.Entry();
|
|
entry.eventID = EventTriggerType.PointerClick;
|
|
entry.callback.AddListener((eventData) => { PointerClickEventHandler(); });
|
|
eventTrigger.triggers.Add(entry);
|
|
}
|
|
else {
|
|
Debug.LogWarning($"NamedEventTrigger '{gameObject.name}' could not attach click event (missing button or collider)");
|
|
}
|
|
}
|
|
|
|
EventTrigger eventTrigger;
|
|
|
|
private bool triggered = false;
|
|
|
|
public void PointerClickEventHandler () {
|
|
triggered = true;
|
|
}
|
|
|
|
public override string titleText {
|
|
get {
|
|
return "Run: Will Succeed on Pointer Click";
|
|
}
|
|
}
|
|
|
|
public override string[][] helpText {
|
|
get {
|
|
return new string[][] {
|
|
new string[] {"Run", "Return Success on Press", $"BTC.Run(\"{roomId}\", \"{gameObject.name}\")"},
|
|
};
|
|
}
|
|
}
|
|
|
|
public override void Run(MyBT.NodeState nodeState) {
|
|
if (nodeState == NodeState.FirstRun) {
|
|
// reset event trigger at start
|
|
triggered = false;
|
|
ToggleActive(true);
|
|
}
|
|
|
|
if (nodeState == NodeState.Aborting) {
|
|
triggered = false;
|
|
ToggleActive(false);
|
|
}
|
|
|
|
if (triggered) {
|
|
ToggleActive(false);
|
|
Task.SetSucceeded();
|
|
return;
|
|
}
|
|
}
|
|
|
|
// public override void Set (MyBT.NodeState nodeState, string key, string value) {
|
|
// if (nodeState == NodeState.FirstRun) {
|
|
// if (key == "label") {
|
|
// UnityEngine.UI.Text textLabel = GetComponentInChildren<UnityEngine.UI.Text>();
|
|
// if (textLabel != null) {
|
|
// textLabel.text = value;
|
|
// Task.SetSucceeded();
|
|
// return;
|
|
// }
|
|
// } else {
|
|
// Task.SetFailed();
|
|
// return;
|
|
// }
|
|
// }
|
|
// if (nodeState == NodeState.Running) {
|
|
// }
|
|
// if (nodeState == NodeState.Aborting) {
|
|
// }
|
|
// Task.SetSucceeded();
|
|
// }
|
|
|
|
public void ToggleActive (bool state) {
|
|
if (eventTrigger != null) {
|
|
eventTrigger.enabled = state;
|
|
}
|
|
}
|
|
}
|