虚拟面试题Demo

该博客介绍了一个公交车编号的交互系统实现,包括UI设计、数字点击与声音播放、重置功能以及相机控制。用户可以通过点击UI上的数字按钮为公交车编号,点击3个数字后播放对应的语音,点击'确定'播放全部数字,'重置'按钮可清除当前编号。此外,系统还具备相机控制功能,允许用户自由观察公交车的各个角度并进行缩放操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

给公交车编号

大概要求:
1、创建UI,在UI上显示0~9共10个数字,同时显示“确定”和“重置”两个按键;
2、点击UI上的1个数字,在公交车的前面板和侧面板上同步显示所按的数字,并播放相应的声音;
3、点击3个数字后,公交车的面板上不再显示后面再按数字。此时点出“确定”则依次播放所公交车面板上显示的3个数字的声音;
4、任何时候点击“重置”,则公交车面板上的数字归0,之后可以重新给公交车编号;
5、添加相机控制功能,可以方便地观看公交车各角度,包括拉远拉近。

同样的需求实现的方法有很多,代码很简单,就不注释了。

/*----------------------------------------------------------------

 Created by 王银
 文件名: CameraCtrl
 创建时间: 
 文件功能描述: 
 Copyright © 2022年 王银 All rights reserved.
----------------------------------------------------------------*/

using UnityEngine;

namespace TestDemo
{
    public class CameraCtrl : MonoBehaviour
    {
        public float roate_Speed = 10.0f;
        void LateUpdate()
        {
            Transform target_transform = null;

            if (Input.GetMouseButton(0))
            {
                //在屏幕上转换坐标:将鼠标点转换成射线
                Ray rayObj = Camera.main.ScreenPointToRay(Input.mousePosition);
                RaycastHit hitObj;
                if (Physics.Raycast(rayObj, out hitObj))
                {
                    target_transform = hitObj.transform;
                }
                if (target_transform != null)
                {
                    float mousX = Input.GetAxis("Mouse X") * roate_Speed;
                    float mousY = Input.GetAxis("Mouse Y") * roate_Speed;
                    target_transform.Rotate(new Vector3(0, -mousX, mousY));
                    Debug.Log($"MouseAxis X...Y :  {mousX} .... {mousY}");
                }
            }
            //鼠标缩放
            if (Camera.main.orthographic == true)
            {
                Camera.main.orthographicSize += Input.GetAxis("Mouse ScrollWheel") * 10;
            }
            else
            {
                Camera.main.fieldOfView += Input.GetAxis("Mouse ScrollWheel") * 10;
            }
        }

    }
}
/*----------------------------------------------------------------

 Created by 王银
 文件名: 
 创建时间: 
 文件功能描述: 
 Copyright © 2022年 王银 All rights reserved.
----------------------------------------------------------------*/

using System;
using System.Collections;
using System.Collections.Generic;
using TestDemo;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;

namespace TestDemo
{
    public class UIToPanel : MonoBehaviour
    {
        public List<Button> BtnList = new List<Button>(12);
        private int num;
        private List<int> numList = new List<int>(3);

        void Awake()
        {
            int count = BtnList.Count;
            for (int i = 0; i < count; i++)
            {
                GameObject go = BtnList[i].gameObject;
                BtnList[i].onClick.AddListener(delegate () { BtnCallBack(go); });
            }
            numList.Clear();
        }
        void BtnCallBack(GameObject go)
        {
            if (go.name == "Button_OK")
            {
                numList = GameData.instance.GetList();
                if (numList.Count > 2)
                {
                    for (int i = 0; i < numList.Count; i++)
                    {
                        StartCoroutine(Play(i, numList[i]));
                    }
                }
            }
            else if (go.name == "Button_Rest")
            {
                GameData.instance.Clear();
            }
            else
            {
                num = int.Parse(go.name);
                GameData.instance.SetList(num);
                SoundManager.instance.Play(num);
            }
        }
        private IEnumerator Play(int index, int id)
        {
            yield return new WaitForSeconds(index * 0.5f);
            SoundManager.instance.Play(id);
        }
    }
}
/*----------------------------------------------------------------

 Created by 王银
 文件名: 
 创建时间: 
 文件功能描述: 
 Copyright © 2022年 王银 All rights reserved.
----------------------------------------------------------------*/

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

namespace TestDemo
{
    public class GameData
    {
        public static GameData instance = new GameData();
        private List<int> numList;
        private int index;
        public GameData()
        {
            numList = new List<int>(3);
            index = 0;
        }

        public void SetList(int id)
        {
            if (numList.Count < 3)
            {
                SetMaterial.instance.SetMaterialById(index, id);
                index++;
                numList.Add(id);
            }

        }
        public List<int> GetList()
        {
            return numList;
        }

        public void Clear()
        {
            SetMaterial.instance.SetMaterialById(0, 0);
            SetMaterial.instance.SetMaterialById(1, 0);
            SetMaterial.instance.SetMaterialById(2, 0);
            numList.Clear();
            index = 0;
            SoundManager.instance.Clear();
        }
    }
}
/*----------------------------------------------------------------

 Created by 王银
 文件名: 
 创建时间: 
 文件功能描述: 
 Copyright © 2022年 王银 All rights reserved.
----------------------------------------------------------------*/

using UnityEngine;
using System.Collections.Generic;

namespace TestDemo
{
    public class SoundEntity
    {
        public string id;
        public GameObject soundObj;
        public AudioSource source;
        public float time;
        public bool isPause;
    }

    public class SoundManager : MonoBehaviour
    {
        public static SoundManager instance;
       
        //音量
        public float volume = 1.0f;
        //音效缓存
        private Stack<SoundEntity> pools;
        //播放中的音效
        private Dictionary<string, List<SoundEntity>> soundDic;
        private List<SoundEntity> sounds;
        //声音容器
        private Transform transform;

        void Awake()
        {
            instance = this;
            pools = new Stack<SoundEntity>();
            soundDic = new Dictionary<string, List<SoundEntity>>();
            sounds = new List<SoundEntity>();
        }

        /// <summary>
        /// 每帧更新
        /// </summary>
        public void Update()
        {
            List<SoundEntity> list;
            SoundEntity soundData;
            int len = sounds.Count;
            for (int i = 0; i < len;)
            {
                soundData = sounds[i];
                if ((soundData.time > 0 && Time.realtimeSinceStartup > soundData.time))
                {
                    sounds.RemoveAt(i);
                    if (soundDic.TryGetValue(soundData.id, out list))
                    {
                        list.Remove(soundData);
                    }
                    len--;
                    AddToPool(soundData);
                    continue;
                }
                i++;
            }
        }
        /// <summary>
        /// 播放声音
        /// </summary>
        public SoundEntity Play(int soundid)
        {
            string id = soundid.ToString();
            float systime = Time.realtimeSinceStartup;
            AudioClip clip = Resources.Load<AudioClip>("Sound/" + id);
            if (clip != null)
            {
                SoundEntity soundData = GetAtPool();
                soundData.id = id;
                soundData.isPause = false;
                soundData.time = Time.realtimeSinceStartup + clip.length;
                GameObject soundObj = soundData.soundObj;
                if (soundObj == null)
                {
                    soundObj = new GameObject();
                    soundObj.transform.parent = transform;
                    soundData.soundObj = soundObj;
                }
                soundObj.name = "Audio_" + id;
                soundObj.SetActive(true);
                AudioSource source = soundData.source;
                if (source == null)
                {
                    source = soundObj.AddComponent<AudioSource>();
                    soundData.source = source;
                }
                source.clip = clip;
                source.volume = volume;
                source.Play();
                sounds.Add(soundData);
                List<SoundEntity> list;
                if (!soundDic.TryGetValue(id, out list))
                {
                    list = new List<SoundEntity>();
                    soundDic.Add(id, list);
                }
                list.Add(soundData);
                systime = Time.realtimeSinceStartup - systime;
                return soundData;
            }
            return null;
        }

        /// <summary>
        /// 清理数据
        /// </summary>
        public void Clear()
        {
            soundDic.Clear();
            int i, len;
            len = sounds.Count;
            for (i = 0; i < len; i++)
            {
                DestroySound(sounds[i]);
            }
            sounds.Clear();
            len = pools.Count;
            SoundEntity[] list = pools.ToArray();
            for (i = 0; i < len; i++)
            {
                DestroySound(list[i]);
            }
        }

        /// <summary>
        /// 销毁声音
        /// </summary>
        private void DestroySound(SoundEntity soundData)
        {
            if (soundData != null)
            {
                if (soundData.source != null)
                {
                    soundData.source.Stop();
                    soundData.source.clip = null;
                    soundData.source = null;
                }
                if (soundData.soundObj != null)
                {
                    GameObject.DestroyImmediate(soundData.soundObj);
                    soundData.soundObj = null;
                }
            }
        }

        /// <summary>
        /// 添加到缓存
        /// </summary>
        private void AddToPool(SoundEntity data)
        {
            if (data.source != null)
            {
                data.source.Stop();
                data.source.clip = null;
            }
            if (data.soundObj != null)
            {
                data.soundObj.SetActive(false);
            }
            pools.Push(data);
        }

        /// <summary>
        /// 从缓存获得对象
        /// </summary>
        private SoundEntity GetAtPool()
        {
            if (pools.Count > 0)
            {
                return pools.Pop();
            }
            return new SoundEntity();
        }
    }
}
/*----------------------------------------------------------------

 Created by 王银
 文件名: 
 创建时间: 
 文件功能描述: 
 Copyright © 2022年 王银 All rights reserved.
----------------------------------------------------------------*/

using System.Collections.Generic;
using UnityEngine;

namespace TestDemo
{
    public class SetMaterial : MonoBehaviour
    {
        public static SetMaterial instance;

        private List<MeshRenderer> numList = new List<MeshRenderer>(3);

        void Awake()
        {
            instance = this;
            numList.Clear();
            numList.Add(transform.Find("Number1").GetComponent<MeshRenderer>());
            numList.Add(transform.Find("Number2").GetComponent<MeshRenderer>());
            numList.Add(transform.Find("Number3").GetComponent<MeshRenderer>());
        }

        public void SetMaterialById(int index, int matarialId)
        {
            if (index < numList.Count)
                numList[index].material = Resources.Load<Material>("Materials/" + matarialId);
        }
    }
}

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王 银

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值