using UnityEngine;
using System.Collections;
public class GameScenecontroller01 : MonoBehaviour {
private int Score = 0;
public GUIText scoreGUI;
public GameObject[] EnemyCreator;
public bool[] bEnemyCreator;
//public GUIText scoreGUIShadow;
// Use this for initialization
IEnumerator Start () {
for (int i =0; i< bEnemyCreator.Length; i++)
bEnemyCreator [i] = false;
yield return new WaitForSeconds (2.0f);
bEnemyCreator [0] = true;
Instantiate(EnemyCreator[0],new Vector3(0,0,0),Quaternion.identity);
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.Escape)) {
Debug.Log ("Escape Key");
Application.Quit ();
}
if ((Score > 150) && (!bEnemyCreator [2])) {
bEnemyCreator [2] = true;
Instantiate (EnemyCreator [2], new Vector3 (0, 0, 0), Quaternion.identity);
} else if ((Score > 50) && (!bEnemyCreator [1])) {
bEnemyCreator [1] = true;
Instantiate (EnemyCreator [1], new Vector3 (0, 0, 0), Quaternion.identity);
}
}
public void AddScore(int Value) {
Score += Value;
scoreGUI.text = "Score: " + Score;
}
public int GetScore()
{
return Score;
}
}
&&(||) 연산 : 앞의 연산에 대한 처리가 올바르지 않은 경우 뒤의 처리를 하지 않는다.
using UnityEngine;
using System.Collections;
public class Boss01Pattern : MonoBehaviour {
public float xspeed = 1.0f;
public float yspeed = 0.5f;
// Use this for initialization
private bool bLeft = false;
private bool bDown = false;
void Start () {
rigidbody2D.velocity = new Vector2 (xspeed, yspeed);
}
// Update is called once per frame
void Update () {
if ((transform.position.x > 2) && !bLeft) {
xspeed = xspeed * -1;
rigidbody2D.velocity = new Vector2 (xspeed, yspeed);
bLeft = true;
} else if((transform.position.x < -2) && bLeft) {
xspeed = xspeed * -1;
rigidbody2D.velocity = new Vector2 (xspeed, yspeed);
bLeft = false;
}
if ((transform.position.y > 5) && !bDown) {
yspeed = yspeed * -1;
rigidbody2D.velocity = new Vector2 (xspeed, yspeed);
bDown = true;
} else if((transform.position.y < 3) && bDown) {
yspeed = yspeed * -1;
rigidbody2D.velocity = new Vector2 (xspeed, yspeed);
bDown = false;
}
}
}
using UnityEngine;
using System.Collections;
public class BossCollision01 : MonoBehaviour {
public int Score = 20;
private GameObject UICon;
private GameScenecontroller UIscript;
public int BossLives = 3;
void Start () {
UICon = GameObject.Find ("GameController");
UIscript = UICon.GetComponent<GameScenecontroller> ();
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter2D (Collider2D other)
{
if (other.tag == "HeroMissile") {
Debug.Log ("HeroMissile");
Destroy (other.gameObject);
UIscript.AddScore(Score);
BossLives--;
if(BossLives < 0) {
Destroy(gameObject);
}
}
}
}
using UnityEngine;
using System.Collections;
public class Boss01Spawner : MonoBehaviour {
public GameObject[] Enemy;
public Transform[] SpwanPos;
public float minTimeBetweenSpans;
public float maxTimeBetwwnSpans;
public GameObject propInstance;
// Use this for initialization
void Start () {
Random.seed = System.DateTime.Today.Millisecond;
StartCoroutine ("createEnemy");
}
IEnumerator createEnemy() {
float waitTIme = Random.Range (minTimeBetweenSpans, maxTimeBetwwnSpans);
yield return new WaitForSeconds(waitTIme);
for (int i=0; i< 3; i++) {
int j = Random.Range(0,3);
Instantiate (Enemy [j], SpwanPos [i].position, Quaternion.identity);
}
StartCoroutine ("createEnemy");
}
// Update is called once per frame
void Update () {
}
}
using UnityEngine;
using System.Collections;
public class GameScenecontroller02 : MonoBehaviour {
private int Score = 0;
public GUIText scoreGUI;
public GameObject[] EnemyCreator;
public bool[] bEnemyCreator;
//public GUIText scoreGUIShadow;
// Use this for initialization
IEnumerator Start () {
for (int i =0; i< bEnemyCreator.Length; i++)
bEnemyCreator [i] = false;
yield return new WaitForSeconds (2.0f);
bEnemyCreator [0] = true;
Instantiate(EnemyCreator[0],transform.position,Quaternion.identity);
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.Escape)) {
Debug.Log ("Escape Key");
Application.Quit ();
}
if ((Score > 300) && (!bEnemyCreator [3])) {
bEnemyCreator [3] = true;
Instantiate (EnemyCreator [3], EnemyCreator [3].transform.position, Quaternion.identity);
} else if ((Score > 150) && (!bEnemyCreator [2])) {
bEnemyCreator [2] = true;
Instantiate (EnemyCreator [2], transform.position, Quaternion.identity);
} else if ((Score > 50) && (!bEnemyCreator [1])) {
bEnemyCreator [1] = true;
Instantiate (EnemyCreator [1], transform.position, Quaternion.identity);
}
}
public void AddScore(int Value) {
Score += Value;
scoreGUI.text = "Score: " + Score;
}
public int GetScore()
{
return Score;
}
}
using UnityEngine;
using System.Collections;
public class GameScenecontroller03 : MonoBehaviour {
private int Score = 0;
private int HighScore = 0;
public GUIText scoreGUI;
public GameObject[] EnemyCreator;
public bool[] bEnemyCreator;
//public GUIText scoreGUIShadow;
// Use this for initialization
IEnumerator Start () {
for (int i =0; i< bEnemyCreator.Length; i++)
bEnemyCreator [i] = false;
yield return new WaitForSeconds (2.0f);
bEnemyCreator [0] = true;
Instantiate(EnemyCreator[0],transform.position,Quaternion.identity);
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.Escape)) {
Debug.Log ("Escape Key");
Application.Quit ();
}
if ((Score > 300) && (!bEnemyCreator [3])) {
bEnemyCreator [3] = true;
Instantiate (EnemyCreator [3], EnemyCreator [3].transform.position, Quaternion.identity);
} else if ((Score > 150) && (!bEnemyCreator [2])) {
bEnemyCreator [2] = true;
Instantiate (EnemyCreator [2], transform.position, Quaternion.identity);
} else if ((Score > 50) && (!bEnemyCreator [1])) {
bEnemyCreator [1] = true;
Instantiate (EnemyCreator [1], transform.position, Quaternion.identity);
}
}
public void AddScore(int Value) {
Score += Value;
scoreGUI.text = "Score: " + Score;
}
public int GetScore()
{
return Score;
}
public void SetPlayerPrefs() {
HighScore = PlayerPrefs.GetInt("HIGHSCORE");
if (HighScore < Score) {
HighScore = Score;
PlayerPrefs.SetInt ("HIGHSCORE", HighScore);
}
PlayerPrefs.SetInt ("SCORE", Score);
}
}
씬이 끝나는 시점(보스가 죽는 시점)에 SetPlayerPrefs()를 호출하면 저장하고 싶은 데이터가 저장
using UnityEngine;
using System.Collections;
public class BossCollision : MonoBehaviour {
public int Score = 20;
private GameObject UICon;
private GameScenecontroller UIscript;
public int BossLives = 3;
void Start () {
UICon = GameObject.Find ("GameController");
UIscript = UICon.GetComponent<GameScenecontroller> ();
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter2D (Collider2D other)
{
if (other.tag == "HeroMissile") {
Debug.Log ("HeroMissile");
Destroy (other.gameObject);
UIscript.AddScore(Score);
BossLives--;
if(BossLives < 0) {
Destroy(gameObject);
UIscript.SetPlayerPrefs();
Application.LoadLevel("Success");
}
}
}
}
SetPlayerPrefs() 호출 및 다음 씬으로의 이동
using UnityEngine;
using System.Collections;
public class SuccessScore : MonoBehaviour {
public int Score;
// Use this for initialization
void Start () {
LoadData ();
guiText.text = "Score :" + Score;
}
void LoadData() {
Score = PlayerPrefs.GetInt("SCORE");
}
// Update is called once per frame
void Update () {
}
}
using UnityEngine;
using System.Collections;
public class SuccessHighScore : MonoBehaviour {
public int HighScore;
// Use this for initialization
void Start () {
LoadData ();
guiText.text = "HighScore :\n" + HighScore;
}
void LoadData() {
HighScore = PlayerPrefs.GetInt("HIGHSCORE");
}
// Update is called once per frame
void Update () {
}
}