新建文件>导入资源>创建Scenes文件夹保存场景为Scenes
将图片的类型Texture Type属性改为Sprite; Sprite Mode属性改为Single;
选中相机>File>Build Setting设为android
选中相机在Game视图中可以调整分辨率,调整相机的视野为4;
设置精灵的分层>Sprite Renderer属性中的Sorting Layer>Add Sorting Layer
人眼一秒可以接受23帧的动画
子弹添加BoxCollider,勾选is Trigger属性,rigidBody 2D组件,重力Gravity Scale属性设置为0
//两张背景的图片的替换
public class BackgroundTransform : MonoBehaviour {
public static float moveSpeed = 2f;
void Update () {
this.transform.Translate( Vector3.down * moveSpeed * Time.deltaTime );
Vector3 postion = this.transform.position;
if(postion.y<=-8.52f){
this.transform.position = new Vector3(postion.x,postion.y+8.52f*2,postion.z );
}
}
}
public class hero : MonoBehaviour {
public bool animation = true;
public int frameCountPersconds = 10;
public float timer = 0;
public Sprite[] sprites;
private SpriteRenderer spriteRender;
void Update () {
if (animation)
{
timer += Time.deltaTime;
int frameIndex =(int)(timer / (1f / frameCountPersconds));
int frame=frameIndex%2;
spriteRender.sprite = sprites[frame];
}
}
}
using UnityEngine;
using System.Collections;
public enum EnemyType{
smallEnemy,
middleEnemy,
bigEnemy
}
//敌人
public class Enemy : MonoBehaviour {
public int hp = 1;
public float speed = 2;
public int score = 100;
public EnemyType type= EnemyType.smallEnemy;
public bool isDeath = false;
public Sprite[] explosionSprites;
private float timer=0;
public int explosionAnimationFrame=10;
private SpriteRenderer render;
public float hitTimer = 0.2f;
private float resetHitTime ;
public Sprite[] hitSprites;
// Use this for initialization
void Start () {
render = this.GetComponent<SpriteRenderer>();
resetHitTime=hitTimer;
hitTimer=0;
}
// Update is called once per frame
void Update () {
this.transform.Translate( Vector3.down*speed*Time.deltaTime );
if(this.transform.position.y<=-5.6f){
Destroy(this.gameObject);
}
if(isDeath){
timer+=Time.deltaTime;// 0
int frameIndex = (int)(timer/(1f/explosionAnimationFrame));
if(frameIndex>=explosionSprites.Length){
//destroy
Destroy(this.gameObject);
}else{
render.sprite= explosionSprites[frameIndex];
}
}
else{
if(type==EnemyType.middleEnemy||type==EnemyType.bigEnemy){
if(hitTimer>=0){
hitTimer-=Time.deltaTime;
int frameIndex = (int)((resetHitTime-hitTimer)/(1f/explosionAnimationFrame));
frameIndex%=2;
render.sprite= hitSprites[frameIndex];
}
}
}
if (Input.GetKeyDown(KeyCode.Space) && BombManager._instance.count>0 ) {
toDie();
}
}
public void BeHit(){
hp-=1;
// explosion
if(hp<=0){
toDie();
}else{
hitTimer=resetHitTime;
}
}
private void toDie(){
if(!isDeath){
isDeath=true;
GameManager._instance.score+=score;
}
}
}
public class Award : MonoBehaviour {
public int type = 0; //0 gun 1 explode
public float speed = 1.5f;
void Update(){
this.transform.Translate( Vector3.down*Time.deltaTime*speed );
if(this.transform.position.y<=-4.5f){
Destroy(this.gameObject);
}
}
}
public class Spawn : MonoBehaviour {
public GameObject enemy0Prefab;
public GameObject enemy1Prefab;
public GameObject enemy2Prefab;
public GameObject awardType0Prefab;
public GameObject awardType1Prefab;
public float enemy0Rate = 0.5f;
public float enemy1Rate = 5f;
public float enemy2Rate = 8f;
public float awardType0Rate = 7;
public float awardType1Rate = 10;
void Start () {
InvokeRepeating("createEnemy0",1,enemy0Rate);
InvokeRepeating("createEnemy1",3,enemy1Rate);
InvokeRepeating("createEnemy2",6,enemy2Rate);
InvokeRepeating("createAwardType0",10,awardType0Rate);
InvokeRepeating("createAwardType1",10,awardType1Rate);
}
public void createEnemy0(){
float x = Random.Range(-2.15f,2.15f);
GameObject.Instantiate(enemy0Prefab,new Vector3(x,transform.position.y,0),Quaternion.identity);
}
public void createEnemy1(){
float x = Random.Range(-2.04f,2.04f);
GameObject.Instantiate(enemy1Prefab,new Vector3(x,transform.position.y,0),Quaternion.identity);
}
public void createEnemy2(){
float x = Random.Range(-2.04f,2.04f);
GameObject.Instantiate(enemy2Prefab,new Vector3(x,transform.position.y,0),Quaternion.identity);
}
public void createAwardType0(){
audio.Play();
float x = Random.Range(-2.1f,2.1f);
GameObject.Instantiate(awardType0Prefab,new Vector3(x,transform.position.y,0),Quaternion.identity);
}
public void createAwardType1(){
audio.Play();
float x = Random.Range(-2.1f,2.1f);
GameObject.Instantiate(awardType1Prefab,new Vector3(x,transform.position.y,0),Quaternion.identity);
}
}
using UnityEngine;
using System.Collections;
public enum GameState{
Runing,
Pause
}
public class GameManager : MonoBehaviour {
public static GameManager _instance;
public int score=0;
private GUIText guiText;
public GameState gameState = GameState.Runing;
void Awake(){
_instance=this;
guiText=GameObject.FindGameObjectWithTag("ScoreGUI").guiText;
}
// Update is called once per frame
void Update () {
guiText.text="Score:"+score;
}
public void transfromGameState(){
if(gameState==GameState.Runing){
pauseGame();
}else if(gameState==GameState.Pause){
continueGame();
}
}
public void pauseGame(){
Time.timeScale=0;// time.delatTime = 0
gameState=GameState.Pause;
}
public void continueGame(){
Time.timeScale=1;
gameState=GameState.Runing;
}
}
public class ReStart : MonoBehaviour {
void OnMouseUpAsButton() {
Application.LoadLevel(0);
}
}
public class Quit : MonoBehaviour {
void OnMouseUpAsButton() {
Application.Quit();
}
}
public class BombManager : MonoBehaviour {
public GameObject bomb;
public GUIText bombNumber;
public int count=0;
public static BombManager _instance;
void Awake() {
_instance = this;
bomb.SetActive(false);
bombNumber.gameObject.SetActive(false);
}
void Update() {
if (Input.GetKeyDown(KeyCode.Space) && BombManager._instance.count > 0) {
this.UseABomb();
}
}
public void AddABomb() {
bomb.SetActive(true);
bombNumber.gameObject.SetActive(true);
count++;
bombNumber.text = "X " + count;
}
private void UseABomb() {
if (count > 0) {
count--;
bombNumber.text = "X " + count;
if (count <= 0) {
bomb.SetActive(false);
bombNumber.gameObject.SetActive(false);
}
}
}
}
public class Bullet : MonoBehaviour {
public float speed = 2;
void Update () {
transform.Translate( Vector3.up * speed *Time.deltaTime );
if(transform.position.y>4.3f){
Destroy(this.gameObject);
}
}
void OnTriggerEnter2D(Collider2D other) {
if(other.tag=="Enemy"){
if(!other.GetComponent<Enemy>().isDeath){
other.gameObject.SendMessage("BeHit");
GameObject.Destroy(this.gameObject);
}
}
}
}
public class Gun : MonoBehaviour {
public float rate =0.2f;
public GameObject bullet;
public void fire(){
GameObject.Instantiate(bullet,transform.position,Quaternion.identity );
}
public void openFire(){
InvokeRepeating("fire",1,rate);
}
public void stopFire(){
CancelInvoke("fire");
}
}
public class GameOver : MonoBehaviour {
public static GameOver _instance;
public GUIText highScore;
public GUIText nowScore;
void Awake() {
_instance = this;
this.gameObject.SetActive(false);
}
public void Show(float nowScore) {
float historyScore = PlayerPrefs.GetFloat("historyHighScore", 0);
if (nowScore > historyScore) {
PlayerPrefs.SetFloat("historyHighScore", nowScore);
}
highScore.text = historyScore + "";
this.nowScore.text = nowScore + "";
this.gameObject.SetActive(true);
}
}
public class GamePause : MonoBehaviour {
void OnMouseUpAsButton() {
print ("click me!");
GameManager._instance.transfromGameState();
audio.Play();
}
}