Unity3D 读取Excel转List(可用作题库)

知识问答完整项目资源下载链接:

https://download.csdn.net/download/qq_34421469/15078659

以下为关键代码:

读取Excel转成指定数据类型的List集合

using Excel;
using System.Data;
using System.IO;
using System.Collections.Generic;


public class ReadExcel
{
    /// 
    /// 读取Excel转List(List类型可变)
    /// 
    /// 
    /// 
    public static List ReadToList(string excelPath)
    {
        FileStream stream = File.Open(excelPath, FileMode.Open, FileAccess.Read, FileShare.Read);
        IExcelDataReader excelReader;
        if (excelPath.Split('.')[1] == "xls")
        {
            //读取 *.xls 文件
            excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
        }
        else
        {
            //读取 *.xlsx 文件
            excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
        }
       
        DataSet result = excelReader.AsDataSet();
        int rows = result.Tables[0].Rows.Count;
        int cols = result.Tables[0].Columns.Count;
        List excelDatas = new List();
        ExcelData excelData = new ExcelData();
        for (int i = 1; i < rows; i++)
        {
            for (int j = 0; j < cols; j++)
            {

                switch (result.Tables[0].Rows[0][j].ToString())
                {
                    case ExcelDataIndex.ID:
                        excelData.ID = result.Tables[0].Rows[i][j].ToString();
                        break;
                    case ExcelDataIndex.Category:
                        excelData.Category = result.Tables[0].Rows[i][j].ToString();
                        break;
                    case ExcelDataIndex.Question:
                        excelData.Question = result.Tables[0].Rows[i][j].ToString();
                        break;
                    case ExcelDataIndex.OptionA:
                        excelData.OptionA = result.Tables[0].Rows[i][j].ToString();
                        break;
                    case ExcelDataIndex.OptionB:
                        excelData.OptionB = result.Tables[0].Rows[i][j].ToString();
                        break;
                    case ExcelDataIndex.OptionC:
                        excelData.OptionC = result.Tables[0].Rows[i][j].ToString();
                        break;
                    case ExcelDataIndex.OptionD:
                        excelData.OptionD= result.Tables[0].Rows[i][j].ToString();
                        break;
                    case ExcelDataIndex.Answer:
                        excelData.Answer = result.Tables[0].Rows[i][j].ToString();
                        break;
                }

            }
            excelDatas.Add(excelData);
            excelData = new ExcelData();
        }
        return excelDatas;
    }
 
   
}

通过获取到的题库,实现简单知识问答游戏(答题结束有分数,等级等内容)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Button = UnityEngine.UI.Button;

public class PrintExcel : MonoBehaviour {

    public static PrintExcel instance;

    public TextAsset json;
    public List listdata;
   
    public Text txt_Ques;       //题目内容  
    public Text txt_QuesCount;  //当前题目数量
    public GameObject selectItem; //选项
    public Text txt_A;       //选项A内容  
    public Text txt_B;       //选项B内容
    public Text txt_C;       //选项C内容  
    public Text txt_D;       //选项D内容
    public Text txt_CountDown;  //倒计时文本
    public GameObject img_Score;    //得分UI
    public AudioSource audioSource;
    public AudioClip[] audioClips;
    public Texture[] TorF_Icon;  //0:Empty 1:Right 2:Wrong

    string _mAnswer = "";   //正确答案
  
    // 设置出题的数量
    int queCount;
    //出题范围
    int startNum;
    int endNum;
    //用来存储Inspector面板上的的赋值
    int oldStartNum;

    [HideInInspector]
    public string selectName="";
    private int index;
    private int grade=0;    //答题得分
    private int rightCount = 0; //答对题数量
    private int wrongCount = 0; //答错题数量
    private int currentQueCount = 0;    //当前题目数量
    private AudioSource audio_PressBtn;  //点击按钮音效
    private void Awake()
    {
        audio_PressBtn = selectItem.GetComponent();
        if (instance == null)
        {
            instance = this;
        }
    }
    public void Init()
    {
        listdata = DoExcel.Load(json);  //读取题目
        queCount = 20;
        startNum = 1;
        endNum = listdata.Count;
        oldStartNum = endNum;
        grade = 0;
        rightCount = 0;
        wrongCount = 0;
        currentQueCount = 0;
        txt_A.text = "";
        txt_B.text = "";
        txt_C.text = "";
        txt_D.text = "";
        txt_Ques.text = "";
        txt_QuesCount.text = currentQueCount + "/20";
        //显示题目
        SelectQues();
    }
    
    /// 
    /// 随机获取题目列表
    /// 
    /// 
    public DepenceTableData GetDate()
    {

        index = Random.Range(startNum, endNum);
        --endNum;
        DepenceTableData date = listdata[index];
        Debug.Log(date);
        //移除已经选过的题目 以防出现重复题目
        listdata.RemoveAt(index);
        return date;
    }

    /// 
    /// 获取到题目显示
    /// 
    public void SelectQues()
    {
        queCount--;
        currentQueCount++;
        DepenceTableData data = GetDate();
        txt_Ques.text= data.Ques;
        txt_QuesCount.text = currentQueCount + "/20";
        txt_A.text = data.A;
        txt_B.text = data.B;
        txt_C.text = data.C;
        txt_D.text = data.D;
        _mAnswer = data.answer;
        SetBtnInteractable(true);
    }

    /// 
    /// 判断答案是否正确
    /// 
    /// 用户选择的选项
    public void isAnswer(string name)
    {
        selectName = name;
        SetBtnInteractable(false);
        if (name == _mAnswer)
        {
            SetBtnColor(name, 1);
            PlayEffectAudio(0); //音效
            img_Score.SetActive(true);
            rightCount++;
            grade += 5;
        }
        else
        {
            SetBtnColor(name, 2);
            SetBtnColor(_mAnswer, 1);
            PlayEffectAudio(1); //音效
            wrongCount++;
        }
        
        Debug.Log("当前答对" + rightCount + "题,答错" + wrongCount + "题");
        StartCoroutine(WaitForNextQues());  //下一题
     
    }




    /// 
    /// 下一题
    /// 
    /// 
    IEnumerator WaitForNextQues()
    {
        yield return new WaitForSeconds(1);        
        if (queCount > 0)
        {
            txt_CountDown.text = "3";
            yield return new WaitForSeconds(1);
            txt_CountDown.text = "2";
            yield return new WaitForSeconds(1);
            txt_CountDown.text = "1";
            yield return new WaitForSeconds(1);
            txt_CountDown.text = "";
            img_Score.SetActive(false);
            SetBtnColor(selectName, 0);
            SetBtnColor(_mAnswer, 0);
            SelectQues();
        }
        else
        {
            yield return new WaitForSeconds(2);
            SetBtnColor(selectName, 0);
            SetBtnColor(_mAnswer, 0);
            End();
        }
    }

    /// 
    /// 答题结束
    /// 
    public void End()
    {
        int rank = 0;

        if (grade == 100)
        {
            //答题通过
  
            rank = 1;
        }
        else if (grade >= 70 && grade < 100)
        {
            //答题通过

            rank = 2;
        }
        else if (grade < 70 && grade >= 60)
        {
            //答题通过

            rank = 3;
        }
        else if (grade < 60)
        {
            //答题未通过

            rank = 4;
        }

        StartCoroutine(UIManager.instance.EndOfAnswer(grade, rank));
        
        

        
    }


    /// 
    /// 判断对错
    /// 
    /// 
    /// 
    public void SetBtnColor(string name,int i)
    {
        GameObject.Find(name).GetComponentInChildren().texture = TorF_Icon[i];            
    }

  

    /// 
    /// 设置音效
    /// 
    /// 
    void PlayEffectAudio(int clip)
    {
        audioSource.clip = audioClips[clip];
        audioSource.Play();
    }

    void SetBtnInteractable(bool isInter)
    {
        for(int i = 0; i < selectItem.transform.childCount; i++)
        {
            selectItem.transform.GetChild(i).GetComponent

 

你可能感兴趣的:(Unity3D合集)