1、音乐、音效
音乐、音效是游戏不可分割的一部分,接下来就要给游戏添加这两者,充实游戏效果。由于教程文件中,音效和音乐文件都已经准备好了,所以添加进游戏里也很简单,一句话就是各找各妈,直接拖入到各自组件之中去就可以了。唯一需要用脚本调一下的是玩家射击的音效。
脚本方面也很简单,在PlayerController脚本中,玩家射击语句后面加一条即可,如下:
void Update()
{
if (Input.GetButton("Fire1") && (Time.time > nextFire))
{
Instantiate(fire, fireSpawn.position,fireSpawn.rotation);
nextFire = Time.time + fireRate;
GetComponent<AudioSource>().Play(); //播放音效
}
}
2、接下来开始做UI。
说是UI,实际就是简单的计算得分以及自机爆炸后的后事处理。首先在Hierarchy界面建立UI文本,最简单的TEXT文件即可,之后调整文本颜色、位置、大小等数据,调整后效果如下:
UI建立就这么多,接下来就是大量脚本逻辑编辑。主要用到的代码:
Text.text
public string text ;
The string value this Text displays.
This is the string value of a Text component. Use this to read or edit the message displayed in Text.
//Attach this script to a GameObject.
//Create a Text GameObject (Create>UI>Text) and attach it to the My Text field in the Inspector of your GameObject
//Press the space bar in Play Mode to see the Text change.
using UnityEngine;
using UnityEngine.UI;//别忘了加这行
public class Example : MonoBehaviour
{
public Text m_MyText;
void Start()
{
//Text sets your text to say this message
m_MyText.text = "This is my text";
}
void Update()
{
//Press the space key to change the Text message
if (Input.GetKey(KeyCode.Space))
{
m_MyText.text = "Text has changed.";
}
}
}
首先,要建立Public对象,把Unity中的UI文件引入进来,并进行初始化,游戏开始后第一帧应该显示什么我们是知道的。
using System.Collections