我在互动小说游戏中设置了两个按钮,每次按都会调用一行新的文本。问题是,每当我按下按钮时,都会收到两条记录的调试消息,通知我单击,游戏将移动两部分文本。
我尝试了许多不同的方法来解决此问题,包括尝试更改项目设置和许多不同代码形式中的提交输入。任何帮助将不胜感激。
这是我当前的代码:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class AdventureGame : MonoBehaviour
{
[SerializeField]
private Text _textComponent;
[SerializeField]
private State _startingState;
private State state;
[SerializeField]
private Button _input0Button;
[SerializeField]
private Button _input1Button;
[SerializeField]
private Text _choices1;
[SerializeField]
private Text _choices2;
private bool _buttonOnepressed;
private bool _buttonTwopressed;
void Start()
{
state = _startingState;
_textComponent.text = state.GetStateStory();
_input0Button.onClick.AddListener(Input0Button);
_input1Button.onClick.AddListener(Input1Button);
_buttonOnepressed = false;
_buttonTwopressed = false;
}
void Update()
{
ManageState();
}
private void ManageState()
{
if (state._choice == true)
{
_choices1.text = state.getchoiceone();
_choices2.text = state.getchoiceTwo();
_textComponent.text = state.GetStateStory();
_input0Button.gameObject.Setactive(true);
_input1Button.gameObject.Setactive(true);
if(_buttonOnepressed == true)
{
StartCoroutine(WaitForItOne());
}
else if(_buttonTwopressed == true)
{
StartCoroutine(WaitForItTwo());
}
}
else if (state._choice == false)
{
_choices1.text = state.getchoiceone();
_choices2.text = state.getchoiceTwo();
_textComponent.text = state.GetStateStory();
_input0Button.gameObject.Setactive(true);
_input1Button.gameObject.Setactive(false);
if(_buttonOnepressed == true)
{
StartCoroutine(WaitForItOne());
}
}
}
private void ManageChoiceone()
{
_buttonOnepressed = false;
State[] _newState = state.GetNextStatesArray();
state = _newState[0];
}
private void ManageChoiceTwo()
{
_buttonTwopressed = false;
State[] _newState = state.GetNextStatesArray();
state = _newState[1];
}
public void Input0Button()
{
Debug.Log("Input 0 pressed");
_buttonOnepressed = true;
}
public void Input1Button()
{
Debug.Log("Input 1 pressed");
_buttonTwopressed = true;
}
IEnumerator WaitForItOne()
{
yield return new WaitForSeconds(3.0f);
ManageChoiceone();
}
IEnumerator WaitForItTwo()
{
yield return new WaitForSeconds(3.0f);
ManageChoiceTwo();
}
}