选择题UI

news2024/10/6 14:27:26

选择题UI
选择题

QuestionInfoSetting.cs

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class QuestionInfoSetting : MonoBehaviour
{
    [Header("选项")]
    public GameObject OptionItem;
    [Header("选项父级")]
    public GameObject optionsFather;
    [Header("选择题类型")]
    public GameObject typeSingle, typeMultip;

    [Header("答案提示")]
    public GameObject answerTip;
    //public GameObject rightImg, errorImg;
    public TextMeshProUGUI titleTxt, judgeTxt, answerTxt;
    [Header("解析")]
    public TextMeshProUGUI parseRight, parseError;
    [Header("确定按钮")]
    public GameObject confimBtn;
    [Header("本地题库")]
    public QuestionConfig questionConfig;
    [Header("回答错误/正确语音")]
    public AudioClip[] clipArr;
    public Action<bool> actAnswerFinished;
    
    private QuesItem quesItem;
    private int singleAnswer = -1;
    private List<int> mulptiAnswer = new List<int>();
    private bool isRight = true;
    private List<Transform> itemList = new List<Transform>();

    private void OnDestroy()
    {
        itemList.Clear();
        mulptiAnswer.Clear();
        if (actAnswerFinished != null)
        {
            actAnswerFinished = null;
        }
        
    }

    /// <summary>
    /// 解锁问题
    /// </summary>
    /// <param name="id">问题id。取值范围:大于0的正整数</param>
    /// <returns></returns>
    public bool UnlockQuestion(int id)
    {
        if (questionConfig == null) return false;
        if(questionConfig.quesItems.Length < id || id<=0) return false;

        //删除之前的克隆体
        var transformArr = optionsFather.GetComponentsInChildren<Transform>();
        foreach (var child in transformArr)
        {
            if (child.gameObject.name.Contains("Clone"))
            {
                child.gameObject.SetActive(false);
                child.gameObject.GetComponent<Button>().onClick.RemoveAllListeners();
                GameObject.Destroy(child.gameObject);
            }
        }

        confimBtn.GetComponent<Button>().onClick.RemoveListener(OnBtnConfimClicked);
        confimBtn.GetComponent<Button>().onClick.AddListener(OnBtnConfimClicked);

        // rightImg.SetActive(false);
        // errorImg.SetActive(false);
        this.parseRight.gameObject.transform.parent.gameObject.SetActive(false);
        this.parseError.gameObject.transform.parent.gameObject.SetActive(false);
        judgeTxt.gameObject.SetActive(false);
        answerTxt.gameObject.SetActive(false);
        itemList.Clear();
        //answerTip.SetActive(false);
        quesItem = questionConfig.quesItems[id - 1];
        if("单选题" != quesItem.type && "多选题" == quesItem.type)
        {
            quesItem.type = quesItem.standardAnswers.Count > 1 ? "多选题" : "单选题";
        }
        typeSingle.SetActive("单选题" == quesItem.type);
        typeMultip.SetActive("多选题" == quesItem.type);
        titleTxt.text = quesItem.title;

        //创建选项
        CreateOptions();
        
        return true;
    }
    
    
    /// <summary>
    /// 选项被点击
    /// </summary>
    /// <param name="obj"></param>
    public void OnOptionClicked(GameObject obj)
    {
        int index = -1;
        if (itemList.Contains(obj.transform))
        {
            index = itemList.IndexOf(obj.transform);
        }

        if ("单选题" == quesItem.type)
        {
            if(singleAnswer != index 
                && singleAnswer != -1 
                && singleAnswer <= itemList.Count)
            {
                itemList[singleAnswer].gameObject.transform.GetChild(0).gameObject.SetActive(false);
            }

            singleAnswer = index;
            obj.transform.GetChild(0).gameObject.SetActive(true);
        }
        else if ("多选题" == quesItem.type)
        {
            if (mulptiAnswer.Contains(index))
            {
                mulptiAnswer.Remove(index);
                obj.transform.GetChild(0).gameObject.SetActive(false);
            }
            else
            {
                mulptiAnswer.Add(index);
                obj.transform.GetChild(0).gameObject.SetActive(true);
            }
        }
    }

    #region --------私有方法--------

    /// <summary>
    /// 创建选项
    /// </summary>
    private void CreateOptions()
    {
        if (OptionItem == null || optionsFather == null) return;

        mulptiAnswer.Clear();
        OptionItem.SetActive(true);
        for (int i=0; i< quesItem.options.Length; i++)
        {
            var item = GameObject.Instantiate(OptionItem, optionsFather.transform);
            item.GetComponentInChildren<TextMeshProUGUI>().text = quesItem.options[i].content.Substring(2);
            //TODO:
        }
        OptionItem.SetActive(false);

        //缓存选项transform信息
        var transformArr = optionsFather.GetComponentsInChildren<Transform>();
        foreach (var child in transformArr)
        {
            if (child.gameObject.name.Contains("Clone"))
            {
                itemList.Add(child);
            }
        }
        gameObject.SetActive(true);
    }
    
    /// <summary>
    /// 响应确定按钮点击事件
    /// </summary>
    private void OnBtnConfimClicked()
    {
        string strBtnName = confimBtn.GetComponentInChildren<TextMeshProUGUI>().text;
        if ("确 定" == strBtnName)
        {
            if(singleAnswer == -1 && mulptiAnswer.Count == 0)
            {//还未选择任何选项
                return;
            }

            //不允许再点击选项了
            var transformArr = optionsFather.GetComponentsInChildren<Transform>();
            foreach (var child in transformArr)
            {
                if (child.gameObject.name.Contains("Clone"))
                {
                    child.gameObject.GetComponent<Button>().onClick.RemoveAllListeners();
                    child.gameObject.GetComponent<Button>().interactable = false;
                }
            }

            if ("单选题" == quesItem.type)
            {
                isRight = singleAnswer == quesItem.standardAnswers[0];
            }
            else if("多选题" == quesItem.type)
            {
                isRight = true;
                if(mulptiAnswer.Count != quesItem.standardAnswers.Count)
                {
                    isRight = false;
                }
                else
                {
                    foreach (var i in mulptiAnswer)
                    {
                        if (quesItem.standardAnswers.Contains(i))
                        {
                            continue;
                        }
                        else
                        {
                            isRight = false;
                            break;
                        }
                    }
                }
            }

            //answerTip.SetActive(true);
            // rightImg.SetActive(isRight);
            // errorImg.SetActive(!isRight);
            judgeTxt.text = isRight ? "选择正确!" : "选择错误!";
            judgeTxt.color = isRight? Color.green : Color.red;
            judgeTxt.gameObject.SetActive(true);
            answerTip.GetComponent<AudioSource>().clip = isRight ? clipArr[1] : clipArr[0];
            answerTip.GetComponent<AudioSource>().Play();
            string answerTemp = "";
            foreach (var answer in quesItem.standardAnswers)
            {
                if (!isRight && itemList.Count > answer)
                {//选择错误后,将正确选项背景变绿
                    itemList[answer].gameObject.GetComponent<ReferenceCollector>().Get<GameObject>("ImageRight").SetActive(true);
                }
                
                answerTemp += NumberToChar(answer);
            }
            answerTxt.text = "正确答案:" + answerTemp;
            answerTxt.color = isRight? Color.green : Color.red;
            answerTxt.gameObject.SetActive(true);

            if ("" != this.quesItem.parse)
            {//如果解析不为空,显示解析
                this.parseRight.text = this.quesItem.parse;
                this.parseRight.gameObject.transform.parent.gameObject.SetActive(isRight);
                this.parseError.text = this.quesItem.parse;
                this.parseError.gameObject.transform.parent.gameObject.SetActive(!isRight);
            }
            
            confimBtn.GetComponentInChildren<TextMeshProUGUI>().text = "关 闭";
            WaitAMoment(2, confimBtn.GetComponent<Button>());

        }
        else
        {
            actAnswerFinished?.Invoke(isRight);
            gameObject.SetActive(false);
            isRight = true;
            singleAnswer = -1;
            mulptiAnswer.Clear();
            confimBtn.GetComponentInChildren<TextMeshProUGUI>().text = "确 定";

            //删除之前的克隆体
            var transformArr = optionsFather.GetComponentsInChildren<Transform>();
            foreach (var child in transformArr)
            {
                if (child.gameObject.name.Contains("Clone"))
                {
                    child.gameObject.SetActive(false);
                    child.gameObject.GetComponent<Button>().onClick.RemoveAllListeners();
                    GameObject.Destroy(child.gameObject);
                }
            }
        }
    }
   

    private async void WaitAMoment(float time, Button bch)
    {
        bch.interactable = false;
        await Task.Delay((int)time * 1000);
        bch.interactable = true;
    }

    /// <summary>
    /// 根据Ascii码转换,将0~25转换为A~Z
    /// </summary>
    /// <param name="_num"></param>
    /// <returns></returns>
    private static string NumberToChar(int _num)
    {
        string strTemp = "";
        if (_num >= 0 && _num < 26)
        {
            int num = _num + 65;
            System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
            byte[] btNumber = new byte[] { (byte)num };
            strTemp = asciiEncoding.GetString(btNumber);
        }
        return strTemp;
    }
    #endregion --------------------
}

using UnityEngine;
public class ChangeOptionBG : MonoBehaviour
{
    public GameObject Selected, Dedault;
    public void Init(bool _isSelected)
    {
        Selected.SetActive(_isSelected);
        //Dedault?.SetActive(!_isSelected);
    }
}

本地选择题库配置

using System;
using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu(menuName ="CreateQuesCfg")]
public class QuestionConfig : ScriptableObject
{
    public QuesItem[] quesItems;
}

[Serializable]
public class QuesItem
{
    public int id;
    public string title;
    public string type;
    public string parse;
    public List<int> standardAnswers;
    public List<int> myAnswers;
    public OptionItem[] options;
}

[Serializable]
public class OptionItem
{
    public string content;
    public bool   isRight;
}


本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/661745.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

LabVIEW错误-1073807360发生于 VISA Configure Serial Port (Instr).vi

调试上位机发生错误&#xff0c;错误信息为&#xff1a;“错误-1073807360发生于 VISA Configure Serial Port (Instr).vi->300W.vi中的属性节点&#xff08;arg 1&#xff09;” 查阅资料后得出的出错原因大致分为两种&#xff1a; 1.在运行程序时&#xff0c;没有选择端…

华为OD机试之MELON的难题(Java源码)

MELON的难题 题目描述 MELON有一堆精美的雨花石(数量为n&#xff0c;重量各异)&#xff0c;准备送给S和W。MELON希望送给俩人的雨花石重量一致&#xff0c;请你设计一个程序&#xff0c;帮MELON确认是否能将雨花石平均分配。 输入描述 第1行输入为雨花石个数: n&#xff0c;0&…

SN74LVC2T45使用说明

DIR高电平&#xff0c;允许从A传输到B&#xff0c;而低电平则允许从B传输到A。 针脚A和DIR以VCCA为基准&#xff0c;针脚B以VCCB为基准

PACS影像系统源码:三维后处理解决方案

一、三维后处理功能是临床数字技术中的重要组成部分&#xff0c;在与PACS系统整合后,能帮助医院构建完整的影像管理及三维影像后处理平台&#xff0c;实现在任意PACS终端进行三维影像后处理工作。 二、三维后处理解决方案是指对三维医学影像数据进行加工处理&#xff0c;以提取…

【C/C++套接字编程】UDP通信实验

目录 一、实验目的&#xff1a; 二、实验内容简要描述 三、实验步骤与结果分析 四、结论 系列博客 【C/C套接字编程】套接字的基本概念与基础语法_Mr_Fmnwon的博客-CSDN博客 【C/C套接字编程】TCP协议通信的简单实现_Mr_Fmnwon的博客-CSDN博客 【C/C套接字编程】UDP协议通信…

Springboot实现接口传输加解密

前言 先给大家看下效果&#xff0c;原本我们的请求是这样子的 加密后的数据传输是这样子的 加解密步骤&#xff1a; 1.前端请求前进行加密&#xff0c;然后发送到后端 2.后端收到请求后解密 3.后端返回数据前进行加密 4.前端拿到加密串后&#xff0c;解密数据 加解密算法&…

新增一个全局处理异常

要在Java应用程序中理新增一个全局处理异常&#xff0c;通常需要执行以下三个步骤&#xff1a; 1.定义全局异常处理器&#xff08;Global Exception Handler&#xff09;&#xff1a; 创建一个类&#xff0c;实现ExceptionHandler接口或使用ControllerAdvice注解&#xff0c;用…

同步模式之保护性暂停

目录 定义 基本实现 带超时版 GuardedObject 多任务版 GuardedObject 总结 定义 即 Guarded Suspension&#xff0c;用在一个线程等待另一个线程的执行结果 要点 有一个结果需要从一个线程传递到另一个线程&#xff0c;让他们关联同一个 GuardedObject如果有结果不断从一…

SpringBoot 实现定时任务动态管理,太爽了

SpringBoot的定时任务的加强工具&#xff0c;实现对SpringBoot原生的定时任务进行动态管理,完全兼容原生Scheduled注解,无需对原本的定时任务进行修改。 快速使用 具体的功能已经封装成SpringBoot-starter即插即用&#xff1a; <dependency><groupId>com.github…

(CVPR-2017)用于目标检测的特征金字塔网络

用于目标检测的特征金字塔网络 论文题目&#xff1a;Feature Pyramid Networks for Object Detection 论文是FAIR发表在CVPR 2017的工作 paper地址 Abstract 特征金字塔是识别系统中用于检测不同尺度对象的基本组件。但最近的深度学习对象检测器避免了金字塔表示&#xff0c;部…

GPT提示词系统学习-第一课-你竟然想不到一个3位数乘法GPT会算错的原因

开篇 在我这个系统的开篇“GPT使我变成超人”中说过,什么样的人使用AI才是起到决定作用的。AI只是工具,它不是万能。使用的人决定了AI可以带什么样的效果。一个很强的人当使用GPT时会形成1+1>2的效果。 因此,提示词的系统化学习是非常重要。这一门课是任何目前国内市面…

使用omp并行技术实现矩阵乘法

矩阵乘法&#xff1a; OpenMP基本概念 OpenMP是一种用于共享内存并行系统的多线程程序设计方案&#xff0c;支持的编程语言包括C、C和Fortran。OpenMP提供了对并行算法的高层抽象描述&#xff0c;特别适合在多核CPU机器上的并行程序设计。编译器根据程序中添加的pragma指令&…

Unity基础 视频组件VideoPlayer,视频的播放与控制

在Unity中&#xff0c;视频播放功能具有广泛的应用&#xff0c;以下是一些视频播放在Unity中的常见用途&#xff1a; 游戏引入和过场动画&#xff1a;使用视频播放可以在游戏开始或过场动画中添加引人注目的视频&#xff0c;为游戏制造氛围和引起玩家的兴趣。这种方式可以通过播…

【运维知识进阶篇】zabbix5.0稳定版详解2(自定义监控+报警+图形+模板)

zabbix内容很多&#xff0c;这篇文章继续给大家介绍&#xff0c;zabbix功能很强大&#xff0c;只要是能获取到的数据都可以监控&#xff0c;俗称万物可监控&#xff0c;这也就决定了zabbix有很大的自由度&#xff0c;本篇文章包括自定义监控&#xff0c;自定义报警&#xff0c;…

分类预测 | MATLAB实现PSO-DBN粒子群优化深度置信网络多输入分类预测

分类预测 | MATLAB实现PSO-DBN粒子群优化深度置信网络多输入分类预测 目录 分类预测 | MATLAB实现PSO-DBN粒子群优化深度置信网络多输入分类预测效果一览基本介绍模型描述程序设计参考资料效果一览

LC-1262. 可被三整除的最大和(状态机DP)

1262. 可被三整除的最大和 难度中等229 给你一个整数数组 nums&#xff0c;请你找出并返回能被三整除的元素最大和。 示例 1&#xff1a; 输入&#xff1a;nums [3,6,5,1,8] 输出&#xff1a;18 解释&#xff1a;选出数字 3, 6, 1 和 8&#xff0c;它们的和是 18&#xff…

图的操作算法详解

一.图 基础概念&#xff1a; 有向图 - 图中每个边都有一个方向&#xff0c;例如社交媒体网站上的关注关系图就是有向图。无向图 - 图中每个边都没有方向&#xff0c;例如朋友之间的相互认识关系图可以是无向图。简单图 - 没有自环和重复边的无向图或有向图&#xff0c;例如一…

025.【树形结构算法】

1. 树的定义 树形结构是由n个元素组成的有限集合&#xff0c;如果n0&#xff0c;那么就称为空树&#xff1b;如果n>0&#xff0c;树形结构应该满足以下条件&#xff1a; 有一个特定的结点&#xff0c;称为根结点或根。 除根结点外&#xff0c;其余结点被分成m(m≥0)个互不…

面试官:一个 TCP 连接可以发多少个 HTTP 请求?

目录 &#x1f914; 第一个问题 &#x1f914; 第二个问题 &#x1f914; 第三个问题 &#x1f914; 第四个问题 &#x1f914; 第五个问题 曾经有这么一道经典面试题&#xff1a;从 URL 在浏览器被被输入到页面展现的过程中发生了什么&#xff1f; 相信大多数准备过的同…

产品经理面试常见的25个必问题(一)

1、你认为产品经理的工作职责是什么&#xff1f; ●需求阶段&#xff08;需求收集、需求管理、需求分析、需求评估&#xff09; ●设计阶段&#xff08;业务流程、功能模块、原型交互、需求文档&#xff09; ●开发阶段&#xff08;需求评审、项目管理、测试验收&#xff09…