使用λ表达式为按钮添加监听事件,添加时未报错,但是点击后报错显示数组越界,甚至按钮中的debug都没显示。检查后发现并无数组越界,其他代码也并无问题(取消按钮事件并无报错)
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Answer_Nine : MonoBehaviour
{
//读取文档
string[][] ArrayX;
string[] lineArray;
public int topicMax = 3;//最大题数
private List<bool> isAnserList = new List<bool>();//存放是否答过题的状态
//加载题目
public Button[] Buttonlist;//答题Button
public Text indexText;//当前第几题
public Text TM_Text;//当前回答
public List<Text> DA_TextList;//选项
private int topicIndex = 0;//第几题
public Text tipsText;//提示信息
public List<int> Qarray;//存放现有题目数组
int PosFlag = 0; //位置
public Transform Pos; //起始位置
string[] ArrayA = new string[9];//答题数组
void Start()
{
TextCsv_Nine();
LoadAnswer_Nine();
}
/*****************读取txt数据******************/
void TextCsv_Nine()
{
//读取csv二进制文件
TextAsset binAsset = Resources.Load("TianKong", typeof(TextAsset)) as TextAsset;
//读取每一行的内容
lineArray = binAsset.text.Split(':');
//创建二维数组
ArrayX = new string[lineArray.Length][];
//把csv中的数据储存在二维数组中
for (int i = 0; i < lineArray.Length; i++)
{
ArrayX[i] = lineArray[i].Split(' ');
}
// //查看保存的题目数据
// for (int i = 0; i < ArrayX.Length; i++)
// {
// for (int j = 0; j < ArrayX[i].Length; j++)
// {
// Debug.Log(ArrayX[i][j]);
// }
// }
}
/*****************加载题目******************/
public void LoadAnswer_Nine()
{
indexText.text = "第" + (topicIndex + 1) + "题:";//第几题
//随机抽一句诗
int n = RandomQ();
//选项数组
string[] ArrayO = new string[9];
//加载选项
//设置正确选项
for(int i=0;i<ArrayX[n].Length;i++)
{
ArrayO[i]=ArrayX[n][i];
}
//设置错误选项
int y = ArrayX[n].Length;
while (y<=8)
{
int l = 1;
int j = Random.Range(0, lineArray.Length);
int k = Random.Range(0, ArrayX[j].Length);
//如果重复则break
for (int i = 0; i < y; i++)
{
if (ArrayX[j][k] == " " || ArrayX[j][k] == "\r"||ArrayX[j][k] == ArrayO[i])
{
l=0;
}
}
if(l==0)
continue;
//
ArrayO[y]=ArrayX[j][k];
y++;
}
//打乱顺序并赋值给各按钮
RandomString(ArrayO);
for(int i=0;i<ArrayO.Length;i++)
{
Buttonlist[i].GetComponentInChildren<Text>().text = ArrayO[i];
//点击按钮后这里报错了——————————————————————————————————————————
Buttonlist[i].onClick.AddListener(() => Judge(ArrayO[i],lineArray[n]));
Debug.Log(ArrayO[i]);
}
}
//打乱顺序
public void RandomString(string[] S)
{
int index=0;
for(int i=0;i<S.Length;i++)
{
int curindex = Random.Range(i,S.Length);
string temp = S[index];
S[index]=S[curindex];
S[curindex] = temp ;
}
}
public void Correct()
{
Debug.Log("what");
tipsText.text = "恭喜您,回答正确!";
}
public void Wrong(string s)
{
tipsText.text = "很遗憾,正确选项为"+"“"+s+"”";
}
public bool AlreadyQ(int n)
{
for(int i = 0;i<Qarray.ToArray().Length;i++)
{
if(n==Qarray[i])
{
return true;
}
}
return false;
}
//下一题
public void NextQ()
{
if(topicIndex < topicMax)
{
tipsText.text = "";
TM_Text.text = "";
LoadAnswer_Nine();
Debug.Log(topicIndex);
topicIndex++;
}
else
{
UnityEngine.SceneManagement.SceneManager.LoadScene(1);
}
}
//随机抽一句诗
public int RandomQ()
{
while(true)
{
int n = Random.Range(0,lineArray.Length);
if(topicIndex == 0)
{
Qarray.Add(n);
return n;
}
else if(!AlreadyQ(n))
{
Qarray.Add(n);
return n;
}
}
}
//判断排序是否正确
public void Judge(string St,string S)
{
//这里压根没debug出来
Debug.Log("Hello");
//答题数组
TM_Text.text += St;
Debug.Log(St);
ArrayA[PosFlag] = St;
if(PosFlag == S.Length)
{
if(TM_Text.text == S)
{
Correct();
}
else
{
Wrong(S);
}
}
}
}