Unity制作一套自定义选择题试卷
如果想要实现一套自定义的试卷题目,可以参照我的这个demo。
下图结构很简单,first是第一题显示页面,second是第二题页面,UIcanvas添加UImanager脚本。
运行如图,选择答案进行下一题。
这是UImanager代码 。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UImanager : MonoBehaviour
{
private static UImanager instance;
public static UImanager Instance
{
get
{
if (instance == null)
{
instance = new UImanager();
}
return instance;
}
}
GameObject Fisrt;
GameObject Second;
List<GameObject> AllOptions = new List<GameObject>();
int Option = 0;
// Use this for initialization
private void Awake()
{
instance = this;
}
void Start()
{
Init();
}
void Init()
{
Fisrt = transform.Find("First").gameObject;
Fisrt.AddComponent<RandomOptions>();
AllOptions.Add(Fisrt);
Second = transform.Find("Second").gameObject;
Second.AddComponent<RandomOptions>();
AllOptions.Add(Second);
}
public void SetOptions(string name )
{
if (name == "Text_A")Debug.Log("您答对了");
else Debug.Log("您答错了");
AllOptions[Option].SetActive(false);
Option += 1;
if (Option < AllOptions.Count)
{
AllOptions[Option].SetActive(true);
}
}
}
这个是RandomOptions 代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class RandomOptions : MonoBehaviour
{
Transform Options;
List<int> list = new List<int>();
List<Transform> OptionsTrans = new List<Transform>();
List<Vector3> OptionsStartTrans = new List<Vector3>();
int number;
private void