Browse Source

Using AddListener to get button callbacks

master
chrisgregan 10 years ago
parent
commit
47c439135b
  1. 37
      Assets/Fungus/Dialog/Scripts/ChooseDialog.cs
  2. 1
      Assets/Fungus/Dialog/Scripts/SayDialog.cs
  3. BIN
      Assets/Shuttle/ShuttleGame.unity

37
Assets/Fungus/Dialog/Scripts/ChooseDialog.cs

@ -1,5 +1,6 @@
using UnityEngine; using UnityEngine;
using UnityEngine.UI; using UnityEngine.UI;
using UnityEngine.Events;
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
@ -14,13 +15,11 @@ namespace Fungus.Script
public class Option public class Option
{ {
public string text; public string text;
public Action onSelect; public UnityAction onSelect;
} }
public List<UnityEngine.UI.Button> optionButtons = new List<UnityEngine.UI.Button>(); public List<UnityEngine.UI.Button> optionButtons = new List<UnityEngine.UI.Button>();
List<Action> optionActions = new List<Action>();
public void Choose(string text, List<Option> options, float timeoutDuration, Action onTimeout) public void Choose(string text, List<Option> options, float timeoutDuration, Action onTimeout)
{ {
Clear(); Clear();
@ -76,7 +75,10 @@ namespace Fungus.Script
return; return;
} }
optionActions.Clear(); foreach (UnityEngine.UI.Button button in optionButtons)
{
button.onClick.RemoveAllListeners();
}
foreach (UnityEngine.UI.Button button in optionButtons) foreach (UnityEngine.UI.Button button in optionButtons)
{ {
@ -87,7 +89,7 @@ namespace Fungus.Script
} }
} }
bool AddOption(string text, Action action) bool AddOption(string text, UnityAction action)
{ {
if (optionButtons == null) if (optionButtons == null)
{ {
@ -107,7 +109,16 @@ namespace Fungus.Script
textComponent.text = text; textComponent.text = text;
} }
optionActions.Add(action); UnityAction buttonAction = action;
button.onClick.AddListener(delegate {
StopAllCoroutines(); // Stop timeout
Clear();
if (buttonAction != null)
{
buttonAction();
}
});
addedOption = true; addedOption = true;
break; break;
@ -116,20 +127,6 @@ namespace Fungus.Script
return addedOption; return addedOption;
} }
public void SelectOption(int index)
{
if (index < optionActions.Count)
{
Action optionAction = optionActions[index];
if (optionAction != null)
{
StopCoroutine("WaitForTimeout");
Clear();
optionAction();
}
}
}
} }
} }

1
Assets/Fungus/Dialog/Scripts/SayDialog.cs

@ -50,7 +50,6 @@ namespace Fungus.Script
IEnumerator WaitForInput(Action onInput) IEnumerator WaitForInput(Action onInput)
{ {
// TODO: Handle touch input
while (!Input.GetMouseButtonDown(0)) while (!Input.GetMouseButtonDown(0))
{ {
yield return null; yield return null;

BIN
Assets/Shuttle/ShuttleGame.unity

Binary file not shown.
Loading…
Cancel
Save