本文只是一个简单的射击游戏,包含四个文件helloworld.cs,bullet.cs,score.cs,restart.cs
helloworld.cs:主场景的控制,包括子弹射击,子弹计数,
bullet.cs:子弹控制类,主要功能是计数,碰撞检测
score.cs:计算分数
restart.cs:结束面板,主要包括,背景音乐的设置,退出游戏,重新开始,打开博客,截屏
主要代码如下:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class helloworld : MonoBehaviour {
Transform bullet;
Text bulletNumText; //子弹数
Text scoreText; //得分text
Text PosText;
string text;
string text1;
int speed = 100;
float x = 0.0f;
// float y = 0.0f;
float z = 0.0f;
int bulletNum = 0;
float width = 80; //按钮宽度
float height = 80; //按钮高度
float screenW = Screen.width; //屏幕宽度
float screenH = Screen.height; //屏幕高度
float startX = 20; //按钮水平初始位置(贴边)
public int score = 0;
AudioSource audioMgr;
Camera mainCamera;
// Camera came = (Camera)gameObject;
// Use this for initialization
void Start () {
mainCamera = (Camera)transform.GetComponent ("Camera");
GameObject obj = GameObject.Find ("bullet");
GameObject textObj = GameObject.Find ("bulletNumText2");
// bulletNumText = (TextMesh)textObj.transform.GetComponent("TextMesh"); //获得3dText
// scoreText = (TextMesh)GameObject.Find("scoreText").transform.GetComponent("TextMesh");
bulletNumText = (Text)textObj.transform.GetComponent("Text"); //获得UI系列的Text
scoreText = (Text)GameObject.Find ("scoreText2").transform.GetComponent ("Text");
// this.TranslateObject (new Vector3 (20, textObj.transform.position.y, textObj.transform.position.z), textObj.transform);
// textObj.transform.position = new Vector3 (-20, textObj.transform.position.y, textObj.transform.position.z);
// GameObject.Find ("PosText").transform.position = new Vector3 (-20, GameObject.Find ("PosText").transform.position.y, GameObject.Find ("PosText").transform.position.z);
PosText = (Text)GameObject.Find ("PosText").transform.GetComponent ("Text");
audioMgr = (AudioSource)transform.GetComponent ("AudioSource");
obj.SetActive (true);
bullet = obj.transform;
text = bulletNumText.text;
text1 = scoreText.text;
this.UpdateBulletNumText ();
this.UpdateScoreText ();
mainCamera.aspect = screenW / screenH;
Text deviceName = (Text)GameObject.Find ("deviceName").transform.GetComponent ("Text");
Text opreatingName = (Text)GameObject.Find ("opreatingName").transform.GetComponent ("Text");
Text graphicsShaderLevel = (Text)GameObject.Find ("graphicsShaderLevel").transform.GetComponent ("Text");
Text processorCount = (Text)GameObject.Find ("processorCount").transform.GetComponent ("Text");
Text processorType = (Text)GameObject.Find ("processorType").transform.GetComponent ("Text");
deviceName.text = deviceName.text + SystemInfo.deviceName;
opreatingName.text = opreatingName.text + SystemInfo.operatingSystem;
graphicsShaderLevel.text = graphicsShaderLevel.text + SystemInfo.graphicsShaderLevel;
processorCount.text = processorCount.text + SystemInfo.processorCount;
processorType.text = processorType.text + SystemInfo.processorType;
}
void LaunchBullet(){
// Vector3 bulltPos = new Vector3 (pos.x, pos.y, transform.position.z);
Transform obje = Instantiate (this.bullet,transform.position,transform.rotation) as Transform;
// Transform obje = Instantiate (this.bullet,bulltPos,transform.rotation) as Transform;
Vector3 fwd = transform.TransformDirection(Vector3.forward);
Rigidbody rigidbody = (Rigidbody) obje.GetComponent("Rigidbody");
rigidbody.AddForce(fwd *10000);
this.bulletNum ++;
this.UpdateBulletNumText ();
audioMgr.Play ();
// unityText.text = string.Format("{0}{1}",text,this.bulletNum);
}
// rotaion object
void RotationObject(Vector3 rotation,Transform trans){
trans.Rotate (rotation, Space.Self);
}
void TranslateObject(Vector3 vect,Transform trans){
trans.Translate (vect);
}
void UpdateBulletNumText (){
bulletNumText.text = text + this.bulletNum;
}
public void UpdateScoreText(){
scoreText.text = text1 + this.score;
}
// Update is called once per frame
void Update () {
//移动摄像机
// this.x = Input.GetAxis ("Horizontal") * Time.deltaTime * this.speed;
// this.z = Input.GetAxis ("Vertical") * Time.deltaTime * this.speed;
// Vector3 vect = new Vector3 (this.x, this.y, this.z);
// this.TranslateObject (vect,transform);
// Debug.Log (vect);
// pressed all the time
if (Input.GetKey (KeyCode.Space)) {
Debug.Log("you pressed :Space ");
}
if (Input.GetKey (KeyCode.Q)) {
Debug.Log("you pressed Q");
Vector3 rotation = new Vector3(0,-this.speed*Time.deltaTime/2,0);
this.RotationObject(rotation,transform);
}
if (Input.GetKey (KeyCode.E)) {
Debug.Log("you pressed E");
Vector3 rotation = new Vector3(0,this.speed*Time.deltaTime/2,0);
this.RotationObject(rotation,transform);
}
if (Input.GetKey (KeyCode.Z)) {
Debug.Log("you pressed Z");
Vector3 rotation = new Vector3(-this.speed*Time.deltaTime/2,0,0);
this.RotationObject(rotation,transform);
}
if (Input.GetKey (KeyCode.C)) {
Debug.Log("you pressed C");
Vector3 rotation = new Vector3(this.speed*Time.deltaTime/2,0,0);
this.RotationObject(rotation,transform);
}
if (Input.GetKeyDown (KeyCode.Return)) {
Debug.Log("you pressed return");
}
if (Input.GetButtonDown ("Fire1")) {
}
if (Input.GetKey (KeyCode.G)) {
Vector3 tempVect = new Vector3(0,-this.speed*Time.deltaTime,0);
this.TranslateObject(tempVect,transform);
}
if (Input.GetKey (KeyCode.H)) {
Vector3 tempVect = new Vector3(0,this.speed*Time.deltaTime,0);
this.TranslateObject(tempVect,transform);
}
if(Input.touchCount>0) {
Touch touch = Input.touches[0];
Vector2 touchPosition = touch.position;
//将屏幕坐标转换到游戏场景中
// Vector3 bulletPos = mainCamera.ScreenToWorldPoint(new Vector3(touchPosition.x,touchPosition.y,transform.position.z));
if(touch.phase == TouchPhase.Began) {
}
if(touch.phase == TouchPhase.Moved) {
}
if(touch.phase == TouchPhase.Stationary) {
// this.LaunchBullet(bulletPos);
}
if(touch.phase == TouchPhase.Canceled) {
}
if(touch.phase == TouchPhase.Ended) {
PosText.text = touchPosition.x + "," + touchPosition.y;
}
}
}
void OnGUI(){
//向左旋转
if (GUI.RepeatButton(new Rect(startX,screenH-height*3.3f,width,height),"LeftR")){
Vector3 rotation = new Vector3(0,this.speed*Time.deltaTime/2,0);
this.RotationObject(rotation,transform);
}
//平行向左
if (GUI.RepeatButton(new Rect(startX,screenH-height*2.2f,width,height),"Left")){
this.x = Time.deltaTime * this.speed;
Vector3 vect = new Vector3 (this.x, 0, 0);
this.TranslateObject (vect,transform);
}
//向下旋转
if (GUI.RepeatButton(new Rect(startX,screenH-height*1.1f,width,height),"DownR")){
Vector3 rotation = new Vector3(-this.speed*Time.deltaTime/2,0,0);
this.RotationObject(rotation,transform);
}
//竖直向上
if (GUI.RepeatButton(new Rect(startX+width*1.1f,screenH-height*3.3f,width,height),"Up")){
Vector3 tempVect = new Vector3(0,-this.speed*Time.deltaTime,0);
this.TranslateObject(tempVect,transform);
}
//射击
if (GUI.RepeatButton(new Rect(startX+width*1.1f,screenH-height*2.2f,width,height),"Shoot")){
this.LaunchBullet();
}
//竖直向下
if (GUI.RepeatButton(new Rect(startX+width*1.1f,screenH-height*1.1f,width,height),"Down")){
Vector3 tempVect = new Vector3(0,this.speed*Time.deltaTime,0);
this.TranslateObject(tempVect,transform);
}
//向右旋转
if (GUI.RepeatButton(new Rect(startX+width*2.2f,screenH-height*3.3f,width,height),"RightR")){
Vector3 rotation = new Vector3(0,-this.speed*Time.deltaTime/2,0);
this.RotationObject(rotation,transform);
}
//水平向右
if (GUI.RepeatButton(new Rect(startX+width*2.2f,screenH-height*2.2f,width,height),"Right")){
this.x = -Time.deltaTime * this.speed;
Vector3 vect = new Vector3 (this.x, 0, 0);
this.TranslateObject (vect,transform);
}
//向上旋转
if (GUI.RepeatButton(new Rect(startX+width*2.2f,screenH-height*1.1f,width,height),"UpR")){
Vector3 rotation = new Vector3(this.speed*Time.deltaTime/2,0,0);
this.RotationObject(rotation,transform);
}
//向屏幕内
if (GUI.RepeatButton(new Rect(screenW-startX-width,screenH-height*3.3f,width,height),"In")){
this.z = -Time.deltaTime * this.speed;
Vector3 vect = new Vector3 (0,0, this.z);
this.TranslateObject (vect,transform);
}
if (GUI.RepeatButton(new Rect(screenW-startX-width,screenH-height*2.2f,width,height),"Shoot")){
this.LaunchBullet();
}
//向屏幕外
if (GUI.RepeatButton(new Rect(screenW-startX-width,screenH-height*1.1f,width,height),"Out")){
this.z = Time.deltaTime * this.speed;
Vector3 vect = new Vector3 (0,0, this.z);
this.TranslateObject (vect,transform);
}
}
}
using System.Collections;
using UnityEngine.UI;
public class helloworld : MonoBehaviour {
Transform bullet;
Text bulletNumText; //子弹数
Text scoreText; //得分text
Text PosText;
string text;
string text1;
int speed = 100;
float x = 0.0f;
// float y = 0.0f;
float z = 0.0f;
int bulletNum = 0;
float width = 80; //按钮宽度
float height = 80; //按钮高度
float screenW = Screen.width; //屏幕宽度
float screenH = Screen.height; //屏幕高度
float startX = 20; //按钮水平初始位置(贴边)
public int score = 0;
AudioSource audioMgr;
Camera mainCamera;
// Camera came = (Camera)gameObject;
// Use this for initialization
void Start () {
mainCamera = (Camera)transform.GetComponent ("Camera");
GameObject obj = GameObject.Find ("bullet");
GameObject textObj = GameObject.Find ("bulletNumText2");
// bulletNumText = (TextMesh)textObj.transform.GetComponent("TextMesh"); //获得3dText
// scoreText = (TextMesh)GameObject.Find("scoreText").transform.GetComponent("TextMesh");
bulletNumText = (Text)textObj.transform.GetComponent("Text"); //获得UI系列的Text
scoreText = (Text)GameObject.Find ("scoreText2").transform.GetComponent ("Text");
// this.TranslateObject (new Vector3 (20, textObj.transform.position.y, textObj.transform.position.z), textObj.transform);
// textObj.transform.position = new Vector3 (-20, textObj.transform.position.y, textObj.transform.position.z);
// GameObject.Find ("PosText").transform.position = new Vector3 (-20, GameObject.Find ("PosText").transform.position.y, GameObject.Find ("PosText").transform.position.z);
PosText = (Text)GameObject.Find ("PosText").transform.GetComponent ("Text");
audioMgr = (AudioSource)transform.GetComponent ("AudioSource");
obj.SetActive (true);
bullet = obj.transform;
text = bulletNumText.text;
text1 = scoreText.text;
this.UpdateBulletNumText ();
this.UpdateScoreText ();
mainCamera.aspect = screenW / screenH;
Text deviceName = (Text)GameObject.Find ("deviceName").transform.GetComponent ("Text");
Text opreatingName = (Text)GameObject.Find ("opreatingName").transform.GetComponent ("Text");
Text graphicsShaderLevel = (Text)GameObject.Find ("graphicsShaderLevel").transform.GetComponent ("Text");
Text processorCount = (Text)GameObject.Find ("processorCount").transform.GetComponent ("Text");
Text processorType = (Text)GameObject.Find ("processorType").transform.GetComponent ("Text");
deviceName.text = deviceName.text + SystemInfo.deviceName;
opreatingName.text = opreatingName.text + SystemInfo.operatingSystem;
graphicsShaderLevel.text = graphicsShaderLevel.text + SystemInfo.graphicsShaderLevel;
processorCount.text = processorCount.text + SystemInfo.processorCount;
processorType.text = processorType.text + SystemInfo.processorType;
}
void LaunchBullet(){
// Vector3 bulltPos = new Vector3 (pos.x, pos.y, transform.position.z);
Transform obje = Instantiate (this.bullet,transform.position,transform.rotation) as Transform;
// Transform obje = Instantiate (this.bullet,bulltPos,transform.rotation) as Transform;
Vector3 fwd = transform.TransformDirection(Vector3.forward);
Rigidbody rigidbody = (Rigidbody) obje.GetComponent("Rigidbody");
rigidbody.AddForce(fwd *10000);
this.bulletNum ++;
this.UpdateBulletNumText ();
audioMgr.Play ();
// unityText.text = string.Format("{0}{1}",text,this.bulletNum);
}
// rotaion object
void RotationObject(Vector3 rotation,Transform trans){
trans.Rotate (rotation, Space.Self);
}
void TranslateObject(Vector3 vect,Transform trans){
trans.Translate (vect);
}
void UpdateBulletNumText (){
bulletNumText.text = text + this.bulletNum;
}
public void UpdateScoreText(){
scoreText.text = text1 + this.score;
}
// Update is called once per frame
void Update () {
//移动摄像机
// this.x = Input.GetAxis ("Horizontal") * Time.deltaTime * this.speed;
// this.z = Input.GetAxis ("Vertical") * Time.deltaTime * this.speed;
// Vector3 vect = new Vector3 (this.x, this.y, this.z);
// this.TranslateObject (vect,transform);
// Debug.Log (vect);
// pressed all the time
if (Input.GetKey (KeyCode.Space)) {
Debug.Log("you pressed :Space ");
}
if (Input.GetKey (KeyCode.Q)) {
Debug.Log("you pressed Q");
Vector3 rotation = new Vector3(0,-this.speed*Time.deltaTime/2,0);
this.RotationObject(rotation,transform);
}
if (Input.GetKey (KeyCode.E)) {
Debug.Log("you pressed E");
Vector3 rotation = new Vector3(0,this.speed*Time.deltaTime/2,0);
this.RotationObject(rotation,transform);
}
if (Input.GetKey (KeyCode.Z)) {
Debug.Log("you pressed Z");
Vector3 rotation = new Vector3(-this.speed*Time.deltaTime/2,0,0);
this.RotationObject(rotation,transform);
}
if (Input.GetKey (KeyCode.C)) {
Debug.Log("you pressed C");
Vector3 rotation = new Vector3(this.speed*Time.deltaTime/2,0,0);
this.RotationObject(rotation,transform);
}
if (Input.GetKeyDown (KeyCode.Return)) {
Debug.Log("you pressed return");
}
if (Input.GetButtonDown ("Fire1")) {
}
if (Input.GetKey (KeyCode.G)) {
Vector3 tempVect = new Vector3(0,-this.speed*Time.deltaTime,0);
this.TranslateObject(tempVect,transform);
}
if (Input.GetKey (KeyCode.H)) {
Vector3 tempVect = new Vector3(0,this.speed*Time.deltaTime,0);
this.TranslateObject(tempVect,transform);
}
if(Input.touchCount>0) {
Touch touch = Input.touches[0];
Vector2 touchPosition = touch.position;
//将屏幕坐标转换到游戏场景中
// Vector3 bulletPos = mainCamera.ScreenToWorldPoint(new Vector3(touchPosition.x,touchPosition.y,transform.position.z));
if(touch.phase == TouchPhase.Began) {
}
if(touch.phase == TouchPhase.Moved) {
}
if(touch.phase == TouchPhase.Stationary) {
// this.LaunchBullet(bulletPos);
}
if(touch.phase == TouchPhase.Canceled) {
}
if(touch.phase == TouchPhase.Ended) {
PosText.text = touchPosition.x + "," + touchPosition.y;
}
}
}
void OnGUI(){
//向左旋转
if (GUI.RepeatButton(new Rect(startX,screenH-height*3.3f,width,height),"LeftR")){
Vector3 rotation = new Vector3(0,this.speed*Time.deltaTime/2,0);
this.RotationObject(rotation,transform);
}
//平行向左
if (GUI.RepeatButton(new Rect(startX,screenH-height*2.2f,width,height),"Left")){
this.x = Time.deltaTime * this.speed;
Vector3 vect = new Vector3 (this.x, 0, 0);
this.TranslateObject (vect,transform);
}
//向下旋转
if (GUI.RepeatButton(new Rect(startX,screenH-height*1.1f,width,height),"DownR")){
Vector3 rotation = new Vector3(-this.speed*Time.deltaTime/2,0,0);
this.RotationObject(rotation,transform);
}
//竖直向上
if (GUI.RepeatButton(new Rect(startX+width*1.1f,screenH-height*3.3f,width,height),"Up")){
Vector3 tempVect = new Vector3(0,-this.speed*Time.deltaTime,0);
this.TranslateObject(tempVect,transform);
}
//射击
if (GUI.RepeatButton(new Rect(startX+width*1.1f,screenH-height*2.2f,width,height),"Shoot")){
this.LaunchBullet();
}
//竖直向下
if (GUI.RepeatButton(new Rect(startX+width*1.1f,screenH-height*1.1f,width,height),"Down")){
Vector3 tempVect = new Vector3(0,this.speed*Time.deltaTime,0);
this.TranslateObject(tempVect,transform);
}
//向右旋转
if (GUI.RepeatButton(new Rect(startX+width*2.2f,screenH-height*3.3f,width,height),"RightR")){
Vector3 rotation = new Vector3(0,-this.speed*Time.deltaTime/2,0);
this.RotationObject(rotation,transform);
}
//水平向右
if (GUI.RepeatButton(new Rect(startX+width*2.2f,screenH-height*2.2f,width,height),"Right")){
this.x = -Time.deltaTime * this.speed;
Vector3 vect = new Vector3 (this.x, 0, 0);
this.TranslateObject (vect,transform);
}
//向上旋转
if (GUI.RepeatButton(new Rect(startX+width*2.2f,screenH-height*1.1f,width,height),"UpR")){
Vector3 rotation = new Vector3(this.speed*Time.deltaTime/2,0,0);
this.RotationObject(rotation,transform);
}
//向屏幕内
if (GUI.RepeatButton(new Rect(screenW-startX-width,screenH-height*3.3f,width,height),"In")){
this.z = -Time.deltaTime * this.speed;
Vector3 vect = new Vector3 (0,0, this.z);
this.TranslateObject (vect,transform);
}
if (GUI.RepeatButton(new Rect(screenW-startX-width,screenH-height*2.2f,width,height),"Shoot")){
this.LaunchBullet();
}
//向屏幕外
if (GUI.RepeatButton(new Rect(screenW-startX-width,screenH-height*1.1f,width,height),"Out")){
this.z = Time.deltaTime * this.speed;
Vector3 vect = new Vector3 (0,0, this.z);
this.TranslateObject (vect,transform);
}
}
}