赞
踩
要求如下:
1、创建UI,在UI上显示0~9共10个数字,同时显示“确定”和“重置”两个按键;
2、点击UI上的1个数字,在公交车的前面板和侧面板上同步显示所按的数字,并播放相应的声音;
3、点击3个数字后,公交车的面板上不再显示后面再按数字。此时点出“确定”则依次播放所公交车面板上显示的3个数字的声音;
4、任何时候点击“重置”,则公交车面板上的数字归0,之后可以重新给公交车编号;
5、添加相机控制功能,可以方便地观看公交车各角度,包括拉远拉近。
实现代码:
CameraControl脚本实现相机控制功能:
private Camera Cam; public Transform Bus; private void Start() { Cam = GetComponent<Camera>(); } private void Update() { camControl(); RotateControl(); } /// <summary> /// 滚轮控制相机视野 /// </summary> private void camControl() { float _wheel = Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * 50; // Debug.Log(" "+_wheel); Cam.transform.Translate(Vector3.forward * _wheel); } /// <summary> /// 左键控制相机旋转 /// </summary> private void RotateControl() { float _mouseX = Input.GetAxis("Mouse X"); float _mouseY = Input.GetAxis("Mouse Y"); if (Input.GetMouseButton(0)) { transform.RotateAround(Bus.transform.position, Vector3.up, _mouseX * 5); transform.RotateAround(Bus.transform.position, transform.right, _mouseY * 5); } }
NumWriter脚本实现给公交车编号功能:
public GameObject[] NumberObj; private int Count = 1;//用count控制点击ui面板显示ui对应数值 public Material material; public AudioSource BusSound; private string []str =new string[3]; // Use this for initialization private void Awake() { transform.Find("Panel/确定").GetComponent<Button>().onClick.AddListener(() => { StartCoroutine(SureSound()); }); transform.Find("Panel/重置").GetComponent<Button>().onClick.AddListener(() => { Count = 1; for (int i = 0; i < NumberObj.Length; i++) { NumberObj[i].GetComponent<MeshRenderer>().material = material; } }); } //协程一秒回调,当按下确定时,把面板输入三个数值声音加载出来 IEnumerator SureSound() { for (int i = 0; i < str.Length; i++) { LoadNumberSound(str[i]); yield return new WaitForSeconds(1); } } /// <summary> /// 数字按钮 /// </summary> /// <param name="number"></param> public void ClickKey(string number) { //Debug.Log("输入的数字" + number); if (Count > 0 || Count < 4) { LoadNumber(number); LoadNumberSound(number); } else { Count = 1; } Debug.Log("count的值" + Count); } /// <summary> /// 加载贴图更换数字面板材质纹理 /// </summary> /// <param name="numName"></param> private void LoadNumber(string numName) { // Debug.Log("贴图名称" + numName); Texture texture = new Texture(); texture = Resources.Load<Texture>("Image/" + numName) as Texture; switch (Count) { case 1: NumberObj[0].GetComponent<MeshRenderer>().material.mainTexture = texture; Count = 2; str[0] = numName; Debug.Log("str[0]:" + str[0]); break; case 2: NumberObj[1].GetComponent<MeshRenderer>().material.mainTexture = texture; Count = 3; str[1] = numName; Debug.Log("str[1]:" + str[1]); break; case 3: NumberObj[2].GetComponent<MeshRenderer>().material.mainTexture = texture; Count = 0; str[2] = numName; Debug.Log("str[2]:" + str[2]); break; } } /// <summary> /// 播放数字声音 /// </summary> /// <param name="numSoundName"></param> private void LoadNumberSound(string numSoundName) { AudioClip clip = Resources.Load<AudioClip>(numSoundName); BusSound.clip = clip; BusSound.Play(); } private void OnDestroy() { transform.Find("Panel/确定").GetComponent<Button>().onClick.RemoveAllListeners(); transform.Find("Panel/重置").GetComponent<Button>().onClick.RemoveAllListeners(); }
初步实现了demo需求:
工程里,公交车面板上的三个数值面板共用一个材质球,点击按钮时调用ClickKey方法,将传入的数字名称传到LoadNumber方法里动态加载文件夹下的对应的数字图片,赋值给相应的数字面板,感觉我目前想到的这种方法比较笨拙,希望各位大佬看了之后如果能想到新的点,或者更好更方便的解决方法能给我留言一下,欢迎并感谢大佬的指教。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。