
Unity3D三款游戏完整(入门进阶必备)代码.doc
25页一.太空陨石大战//******控制太空背景向下移动的游戏脚本******using UnityEngine;using System.Collections;public class BackgroundContoller : MonoBehaviour {public float speed=1.0f; //设置背景向下移动的速度// Use this for initializationvoid Start () {}// Update is called once per framevoid Update () {transform.Translate(0,-speed*Time.deltaTime,0); //实现背景向下移动if(transform.position.y2.14f) //判断子弹位置是否移出游戏屏幕Destroy (gameObject); //若移出,销毁子弹对象}}//******控制陨石的游戏脚本******using UnityEngine;using System.Collections;public class RockController : MonoBehaviour {public float speed=2.0f; //定义陨石的下落速度public GameObject explosionPlayer; //定义爆炸动画对象public GameObject explosionEnemy; //定义爆炸动画对象public static int score=0; //定义击落陨石获得的积分public static int lives=3; //定义玩家生命public static int highScore=0; //设置最高分的存储变量// Use this for initializationvoid Start () {highScore=PlayerPrefs.GetInt("HighScore"); //调用 PlayerPrefs 类中 GetInt()方法读取保存在本地的变量 highScore}// Update is called once per framevoid Update () {transform.Translate(0,-speed*Time.deltaTime,0); //实现陨石在 Y 方向上的下落运动if(transform.position.yPlayerPrefs.GetInt ("HighScore")){ //若当前获得分数大于之前本地保存的最高分highScore=RockController.score ; //保存最高分变量PlayerPrefs.SetInt ("HighScore",RockController.score); //更新最高分}elsehighScore=PlayerPrefs.GetInt("HighScore"); //否则继续保存本地最高分Application.LoadLevel("Lose");}Instantiate(explosionPlayer,transform.position,transform.rotation); //调用飞机与陨石碰撞的爆炸效果transform.position=new Vector3(Random.Range(-2.2f,2.2f),2.5f,0); //重置陨石的的代码,在指定位置重新生成陨石//Destroy(other.gameObject);//销毁飞机,所以将其注释掉,不销毁飞机;Destroy(gameObject)是销毁陨石的}}void OnGUI(){GUI.Label(new Rect(10,10,120,120),"score:"+score.ToString());GUI.Label(new Rect(10,30,60,20),"Lives:"+lives.ToString());GUI.Label (new Rect(10,50,120,20),"highscore:"+highScore.ToString ()); //显示最高分}}//*********游戏开始场景控制代码**********using UnityEngine;using System.Collections;public class StartController : MonoBehaviour {private string instructionText="Instruction:\n\n Press left and right arrow to move.\n Press Space to fire."; //定义简单的游戏操作说明public Texture startTexture; //定义关联 start 场景的变量void OnGUI(){GUI.DrawTexture (new Rect(0,0,Screen.width,Screen.height),startTexture); //绘制开始 start 场景区域,并添加关联的场景GUI.Label(new Rect(10,10,250,200),instructionText); //绘制游戏简介内容if(Input.anyKeyDown){Application.LoadLevel("Level"); //按下任意键,加载场景 1Debug.Log ("Level");}}}//*********个性化游戏倒计时控制代码******using UnityEngine;using System.Collections;public class TimeRemainDisplay : MonoBehaviour {public Texture [] timeNumbers; //定义一个贴图数组,用于存放数字图片 0,1,2...9public static int leftTime=100; //定义游戏倒计时时间为 100 秒,并用于存放游戏剩余时间float myTime; // Use this for initializationvoid Start () {}// Update is called once per framevoid Update () {myTime+=Time.deltaTime; //游戏花费时间的累计,每当累计到 1 秒时,倒计时 leftTime 减 1 秒if(myTime>1){leftTime--;myTime=0;}if(leftTime==0) //若游戏倒计时结束,则转入玩家胜利的 Win 场景{if(RockController.score>PlayerPrefs.GetInt("HighScore")){RockController.highScore=RockController.score;PlayerPrefs.SetInt("HighScore",RockController.score);}elseRockController.highScore=PlayerPrefs.GetInt("HighScore");Application.LoadLevel("Win");}}void OnGUI(){for(int i=0;i0) //判断获得的 horizontal 的值是否大于 0transform.Translate(Vector3.right*speed*Time.deltaTime); //是,控制飞机向右移动if(Input.GetAxis("Horizontal")0) //判断飞机是否右移动 myBomb.velocity=new Vector3(-3*speed,0,0); //是,使炸弹获得惯性速度else if(Input.GetAxis("Horizontal")==0) //判断飞机速度为 0myBomb.velocity=Vector3.zero; //炸弹右移速度为 0elsemyBomb.velocity=new Vector3(3*speed,0,0); //否则飞机左移,使炸弹获得向左的惯性}}}using UnityEngine;using System.Collections;public class ProjectileController : MonoBehaviour {public AudioClip explosionSound;public GameObject explosion;// Use this for initializationvoid Start () {AudioSource.PlayClipAtPoint(explosionSound,new Vector3(0,0,-5));}// Update is called once per framevoid Update () {if(transform.position.x3.45f)Destroy (gameObject);}void OnCollisionEnter(Collision collision){if(collision.gameObject.tag=="plane"){Instantiate(explosion,new Vector3(transform.position.x,transform.position.y,-1),Quaternion.identity);Destroy (gameObject);}}}using UnityEngine;using System.Collections;public class SoundController : MonoBehaviour {public AudioClip explosionSound;// Use this for initializationvoid Start () {AudioSource.PlayClipAtPoint(explosionSound,new Vector3(0,0,-5));}// Update is called once per framevoid Update () {}}using UnityEngine;using System.Collections;public class StartController : MonoBehaviour {public Texture2D startTexture;public Texture2D buttonA;public Texture2D buttonB;// Use this for initializationvoid Start () {}// Update is called once per framevoid Update(){if(Input.GetKeyDown("a"))Application.LoadLevel(1);if(Input.GetKeyDown("b"))Application.Quit();}void OnGUI(){GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height),startTexture);GUI.Label(new Re。
