1、unity 3d 三款游戏完整入门进阶必备代码一太空陨石大战/*控制太空背景向下移动的游戏脚本*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
2、.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 explosi
3、onEnemy;/定义爆炸动画对象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 Upda
4、te () 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);/否则继续保存本地最高分Appl
5、ication.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,
6、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=Instru
7、ction:nn Press left and right arrow to move.n PressSpace to fire.;/定义简单的游戏操作说明public Texture startTexture;/定义关联start场景的变量void OnGUI()GUI.DrawTexture (newRect(0,0,Screen.width,Screen.height),startTexture);/绘制开始start场景区域,并添加关联的场景GUI.Label(new Rect(10,10,250,200),instructionText);/绘制游戏简介内容if(Input.anyK
8、eyDown)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
9、this for initializationvoid Start () / Update is called once per framevoid Update () myTime+=Time.deltaTime;/游戏花费时间的累计,每当累计到1秒时,倒计时leftTime减1秒if(myTime1)leftTime-;myTime=0;if(leftTime=0)/若游戏倒计时结束,则转入玩家胜利的Win场景if(RockController.scorePlayerPrefs.GetInt(HighScore)RockController.highScore=RockController
10、.score;PlayerPrefs.SetInt(HighScore,RockController.score);elseRockController.highScore=PlayerPrefs.GetInt(HighScore);Application.LoadLevel(Win);void OnGUI()for(int i=0;ileftTime.ToString().Length;i+)GUI.DrawTexture(new Rect(300+i*32,20,32,45),timeNumbersSystem.Int32.Parse(leftTime.ToString()i.ToStri
11、ng();using UnityEngine;using System.Collections;public class WinController : MonoBehaviour public Texture winTexture;/ Use this for initializationvoid Start () RockController.score=0;RockController.lives=3;TimeRemainDisplay.leftTime=100;/ Update is called once per framevoid OnGUI () GUI.DrawTexture(
12、new Rect(0,0,Screen.width,Screen.height),winTexture);if(Input.anyKeyDown)Application.LoadLevel(Level);二坦克大战游戏:using UnityEngine;using System.Collections;public class BackgroundController : MonoBehaviour public float speed=2.0f;/声明背景向左移动的速度private bool change=false;/为背景的循环移动声明一个change变量,用于标识背景是否需要移动/
13、 Use this for initializationvoid Start () / Update is called once per framevoid Update () transform.Translate(Vector3.left*speed*Time.deltaTime);if(transform.position.x-14.4)transform.position=new Vector3(15.5f,transform.position.y,transform.position.z);change=true;if(transform.position.x0)/判断获得的horizontal的值是否大于0transform.Translate(Vector3.right*speed*Time.deltaTime); /是,控制飞机向右移动if(Input.GetAxis(Horizontal)0)/判断获得的horizontal的值是否小于0transform.Translate(2*Vector3.left*speed*Time.deltaTime);/是,飞机向左移动transf
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1