Unity单选题开发

第一步

在Canvas下创建一个空物体(Dati_Single),用来挂脚本
在这里插入图片描述

第二步

在Dati_Single创建一个答题板背景图片(Img_DatiBackground)和一个下一题按钮(btn_Next)在这里插入图片描述

第三步

在答题板背景图片(Img_DatiBackgroud)下创建好你需要的所有题目和对应选项——这里也可以用预制体实例化题目,但笔者就直接创建多个物体了。在这里插入图片描述

第四步

这里注意选项名字末尾需要用数字递增答题,这样十分后面代码获取选项更方便!下面就是脚本了!首先创建一个答题控制脚本(DatiController)并挂到第一步中的空物体(Dati_Single)上,假设答对得五分

    ///答题总分
    int score = 0;
    ///下一题按钮
    public Button btn_Next;
    ///需要显示题目的索引
    public int currentTimuIndex = 0;
    ///题目集合
    public List<Transform> timus;
    ///答案集合
    public List<string> ans;
    ///配对答案字典
    ///答案配对——选中的选项的名称==ansDic[timus[currentTimuIndex]]
    ///就是选出来的toggle通过获取其名字与当前题号对应的字典中的值是否匹对(但在这之前先要判定主键是否存在)
    public Dictionary<Transform, string> ansDic;

	//初始化操作
 	private void Init()
    {
    
    
        btn_Next = transform.Find("btn_Next").GetComponent<Button>();
        btn_Next.onClick.AddListener(OnNextBtnClick);

        timus = new List<Transform>();
        for (int i = 0; i < transform.Find("Img_DatiBackground").childCount; i++)//列表元素个数等于这个物体的子物体数
        {
    
    
            timus.Add(transform.Find("Img_DatiBackground/Timu" + (i + 1)));//添加其下所有子物体到列表中
        }

        {
    
    
            //答案列表赋值
            ans = new List<string>();
            ans.Add("Sele_1");
            ans.Add("Sele_1");
            ans.Add("Sele_3");
        }

        //题目、答案进行字典配对
        ansDic = new Dictionary<Transform, string>();
        for (int i = 0; i < transform.Find("Img_DatiBackground").childCount; i++)
        {
    
    
            ansDic.Add(timus[i], ans[i]);
        }

        //默认激活第一道题
        timus[0].gameObject.SetActive(true);
    }	   

第五步

我们需要对下一题按钮进行事件注册,并且还需要两个方法——一个获取当前显示的题目的所有选项,另一个判断答题的正确性并进行分数统计!

	private void OnNextBtnClick()
	{
    
    
		//获取当前显示题目的选项
		//计算分数
		//切换题目
	}
	/// <summary>
    /// 哪个被勾选,然后通过题目列表的序号对应勾选的toggle名字进行匹对,匹配即正确
    /// </summary>
    /// <returns></returns>
	private Toggle[] GetCurrentTimuToggle()
	{
    
    
		//返回获取到的当前题目的所有选项————这里也可以改用列表
	}
	/// <summary>
    /// 分数处理及得分计算
    /// </summary>
    /// <returns></returns>
	private int CalculateScore()
	{
    
    
		//计算分数并返回
	}

第六步

上面三个代码具体实现

    private void OnNextBtnClick()
    {
    
    
        //初始化当前题目的所有选项
        GetCurrentTimuToggle();
        //在加载下一题之前进行分数计算
        Debug.Log(CalculateScore());
        //题号索引自增
        currentTimuIndex++;
        //当前题目编号对题目总数取余
        int showIndex = currentTimuIndex % timus.Count;

        if (currentTimuIndex>=timus.Count)
        {
    
    
            btn_Next.enabled = false;
            Debug.Log("答题结束");
        }

        if (currentTimuIndex > transform.Find("Img_DatiBackground").childCount - 1) return;
        try
        {
    
    
            timus[currentTimuIndex - 1].gameObject.SetActive(false);
            timus[currentTimuIndex].gameObject.SetActive(true);
        }
        catch (System.IndexOutOfRangeException)
        {
    
    
            Debug.Log("数组越界");
        }
    }
	/// <summary>
    /// 哪个被勾选,然后通过题目列表的序号对应勾选的toggle名字进行匹对,匹配即正确
    /// </summary>
    /// <returns></returns>
	public Toggle[] GetCurrentTimuToggle()
    {
    
    
        Toggle[] selections = new Toggle[4];
        if (currentTimuIndex > timus.Count-1) return null;
        Transform toggleGroup = timus[currentTimuIndex].Find("ToggleGroup").GetComponent<Transform>();
        for (int i = 0; i < toggleGroup.childCount; i++)
        {
    
    
            selections[i] = toggleGroup.Find("Sele_" + (i+1)).GetComponent<Toggle>();
        }
        return selections;
    }
    /// <summary>
    /// 分数处理及得分计算
    /// </summary>
    /// <returns></returns>
	private int CalculateScore()
    {
    
    
        if (GetCurrentTimuToggle() == null) return score;
        foreach (var item in GetCurrentTimuToggle())
        {
    
    
            if (item!=null)
            {
    
    
                if (item.isOn)
                {
    
    
                    string seleName = item.name;
                    if (ansDic.ContainsKey(timus[currentTimuIndex]))
                    {
    
    
                        if (ansDic.TryGetValue(timus[currentTimuIndex], out string ans))
                        {
    
    
                            score=(seleName==ans ? score += 5 : score += 0);
                        }
                    }
                }
            }
        }
        return score;
    }

第七步

上效果图
下面答对分数就+5,答错不加分
在这里插入图片描述
转载注明出处:https://blog.csdn.net/weixin_44870508/article/details/110225276

!!!!另还待进一步完善!!!!!

猜你喜欢

转载自blog.csdn.net/weixin_44870508/article/details/110225276