给公交车编号
大概要求:
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);
}
}
}