VioletaBabel
26일 : 풀링과 싱글톤 & 연습용 슈팅게임 발전 본문
교수님께서 보여주신 풀링과 싱글톤 예시
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | using System.Collections; using System.Collections.Generic; using UnityEngine; public class LazerPool : MonoBehaviour { public GameObject mLazer = null; private static LazerPool mPool = null; private Queue mQueue = new Queue(); private LazerPool() { //프라이빗으로 해줌으로서 다른 클래스에서 이 클래스 객체를 만드려면 GetLazerPool()로만 접근 가능 } public static LazerPool GetLazerPool() { if (mPool == null) { mPool = new LazerPool(); mPool.makeBullet(); } return mPool; }//mPool을 싱글톤 으로 만들어줌. static이라 어디든 접근 가능. private void makeBullet() { for (int i = 0; i < 20; ++i) { GameObject lazer = Instantiate(mLazer, Vector3.zero, Quaternion.identity); mQueue.Enqueue(lazer); } } public GameObject getLazer() { GameObject lazer = mQueue.Dequeue() as GameObject; mQueue.Enqueue(lazer); return lazer; } // Use this for initialization void Start () { } // Update is called once per frame void Update () { } } | cs |
풀링에서 개체가 다 액티브되어 있어서 사용할 오브젝트가 부족할 경우 늘리도록 코드를 짤 수도 있음. 그도 해보자.
====
arraylist
nullable type
stack
hashtable
Dictionary
는 따로 꼭 공부하자
================
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 | //라이프 값 바꿔주기, -----------------------------------------------------------------------------------------완료 //instantiate destroy 줄이기-----------------------------------------------------------------------------------완료 //플레이어랑 돌 충돌시 플레이어 라이프 깎기--------------------------------------------------------------------완료 //이펙트도 instantiate 줄이고, --------------------------------------------------------------------------------완료 /////////////////////레이저도 enable uneanable하기-------------------------------------------------------------완료 //돌을 담을 자료구조. 돌도 처음 1번 만들고 더 이상 만들지 않고 자료구조 이용해서 계속 불러쓰기.----------------완료 //하이스코어 시스템--------------------------------------------------------------------------------------------완료 //하이스코어 갱신 시 보상--------------------------------------------------------------------------------------완료 //상점 이용할 보상 금화----------------------------------------------------------------------------------------완료 //상점 시스템--------------------------------------------------------------------------------------------------완료 //돌의 라이프를 좀 더 다양하게. -------------------------------------------------------------------------------완료 //상점을 이용해 데미지 레벨 업 도입.---------------------------------------------------------------------------완료 //소리 넣기 //GameManagerScript.cs using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System.Net.Json; public class GameManagerScript : MonoBehaviour { // public static GameManagerScript instance = null; public GameObject background = null; public GameObject backGroundManager = null; public GameObject EffectManager = null; public GameObject MenuGameStartButton = null; public GameObject MenuGameExitButton = null; public GameObject MenuGameShopButton = null; public GameObject MenuInGameMenuButton = null; public GameObject MenuInGameResumeButton = null; public GameObject MenuInGameMainMenuButton = null; public GameObject playerShip = null; public GameObject player = null; public GameObject StoneManager = null; public float makeStoneCoolTime = 5.0f; public bool boolInGame = false; public bool firstScore = true; public bool playerFullHp = true; public int score; public float makeStoneTime; public int stageLv; public int playerLife; public float gameOverTime; public int highScore; public int gold; public bool thisRoundHighScore; public int playerDam; public bool gameoverDelay = false; public bool isPause = false; public int[] ShopGoldPrice = new int[10] { 10, 50, 100, 200, 300, 500, 700, 1000, 1500, 3000}; public Image[] NowPlayerLifeSpirte = null; //점수용 public Image[] HighNum = null; public Image[] num = null; public Sprite[] numSprites = null; //스톤 프리팹풀링 용 public int[] stoneKeyValue = new int[8]; private Dictionary<int, Queue<GameObject>> stonePool = new Dictionary<int, Queue<GameObject>>(); public GameObject[] stones = new GameObject[8]; //이펙트 프리팹 풀링용 private Dictionary<int, Queue<GameObject>> effectPool = new Dictionary<int, Queue<GameObject>>(); public GameObject[] effects = new GameObject[2]; //금전수수용 public Image[] goldImage = null; //상점용 public Image[] priceImage = null; public Image[] lvImage = null; //메뉴 텍스트 public GameObject[] MenuText = null; public GameObject[] IngameMenuText = null; void Awake() { //instance = this; CreateStonePool(); CreateEffectPool(); } void Start() { MenuGameStartButton.transform.position = new Vector3(0, 3, 2); MenuGameShopButton.transform.position = new Vector3(0, 0, 2); MenuGameExitButton.transform.position = new Vector3(0, -3, 2); MenuInGameResumeButton.transform.position = new Vector3(0, 2, 0); MenuInGameMainMenuButton.transform.position = new Vector3(0, -2, 0); getGameData(); SetFirstHighScore(); ChangeGold(); //MakeBackground(); MakeMenuLayer(); } // Update is called once per frame void Update() { if (boolInGame) { if (Input.GetKeyDown(KeyCode.Escape)) {//일시정지기능 InGameMenu(); } makeStoneTime += TimeManager.GameTime; if (makeStoneTime >= makeStoneCoolTime) { int stoneType; if (stageLv < 5) stoneType = Random.Range(0, 4); else { int now = Random.Range(0, 5); if (now == 4) stoneType = Random.Range(4, 8); else stoneType = Random.Range(0, 4); } PopStoneByPool(stoneType); makeStoneTime -= makeStoneCoolTime; } } else if(gameoverDelay) { gameOverTime += TimeManager.GameTime; if (gameOverTime >= 5.0f) { MakeMenuLayer(); } } } public void InGameMenu() { TimeManager.bTimeStop = (isPause) ? 0 : 1; isPause = !isPause; if(isPause) { MenuInGameMainMenuButton.SetActive(true); MenuInGameResumeButton.SetActive(true); for (int i = 0; i < 2; ++i) IngameMenuText[i].SetActive(true); } else { MenuInGameMainMenuButton.SetActive(false); MenuInGameResumeButton.SetActive(false); for (int i = 0; i < 2; ++i) IngameMenuText[i].SetActive(false); } } public void MinusScore(int stoneType) { if(stoneType*50 <= score) { score -= stoneType * 50; ChangeScore(); } } void MakeBackground() { for (int i = 0; i < 9; ++i) for (int j = 0; j < 7; ++j) { GameObject nowBG = GameObject.Instantiate(background, new Vector3((i * 2.5f) - 10.0f, 6.0f - (j * 2.5f), 0), Quaternion.identity); nowBG.transform.parent = backGroundManager.transform; } } public void MakeMenuLayer() { //init gameoverDelay = false; thisRoundHighScore = false; gameOverTime = 0; makeStoneCoolTime = 3.0f; score = 0; makeStoneTime = makeStoneCoolTime - 1.0f; firstScore = true; stageLv = 1; playerLife = 3; playerFullHp = true; //---------- boolInGame = false; MenuGameExitButton.SetActive(true); MenuGameShopButton.SetActive(true); MenuGameStartButton.SetActive(true); MenuInGameMenuButton.SetActive(false); MenuInGameMainMenuButton.SetActive(false); MenuInGameResumeButton.SetActive(false); for (int i = 0; i < 3; ++i) MenuText[i].SetActive(true); for (int i = 0; i < 2; ++i) IngameMenuText[i].SetActive(false); MenuText[3].SetActive(false); //scoreText for (int i = 0; i < 6; ++i) { num[i].gameObject.SetActive(false); if (i < 3) NowPlayerLifeSpirte[i].gameObject.SetActive(false); } SetPriceAndLv(); } public void MakeGameLayer() { boolInGame = true; MenuGameExitButton.SetActive(false); MenuGameShopButton.SetActive(false); MenuGameStartButton.SetActive(false); MenuInGameMenuButton.SetActive(true); for (int i = 0; i < 3; ++i) MenuText[i].SetActive(false); MenuText[3].SetActive(true); //scoreText MakePlayer(); ChangeScore(); SeeLife(); player.transform.position = new Vector3(0, -4, 0); } public void SetPriceAndLv() { int nowLv = playerDam - 10; int nowPrice = ShopGoldPrice[nowLv]; int[] nowPrices = new int[4]; int[] nowLvs = new int[2]; for (int i = 1000, j = nowPrice, k = 0, l = nowLv; i > 0; i /= 10, ++k) { nowPrices[k] = j / i; j %= i; if(i<100) { nowLvs[k - 2] = l / i; l %= i; } } for (int i = 0; i < 4; ++i) { priceImage[i].sprite = numSprites[nowPrices[i]]; if (i < 2) lvImage[i].sprite = numSprites[nowLvs[i]]; } } public void AtkLvUp() { int nowLv = playerDam - 10; if(nowLv >= 10) { //다 샀음 } else { int nowPrice = ShopGoldPrice[nowLv]; if(nowPrice <= gold) {//구매가능 gold -= nowPrice; ++playerDam; SetPriceAndLv(); ChangeGold(); setGameData(); } else {//돈 부족 } } } void MakePlayer() { if (player == null) { player = GameObject.Instantiate(playerShip, new Vector3(0, -4, 0), Quaternion.identity); player.transform.parent = this.transform; } else player.SetActive(true); } public void ChangeGold() { int[] nowGold = new int[4]; for(int i = 1000, j = gold, k = 0; i > 0; i/=10, ++k) { nowGold[k] = j / i; j %= i; } for (int i = 0; i < 4; ++i) goldImage[i].sprite = numSprites[nowGold[i]]; } public void ChangeScore() { if (!num[0].gameObject.activeInHierarchy) for (int i = 0; i < 6; ++i) num[i].gameObject.SetActive(true); int[] nowScore = new int[6]; for (int i = 100000, j = score, k = 0; i > 0; i /= 10, ++k) { nowScore[k] = j / i; j %= i; } for (int i = 0; i < 6; ++i) num[i].sprite = numSprites[nowScore[i]]; if (score / 3000 + 1 > stageLv) { if (makeStoneCoolTime > 0.5f) { makeStoneCoolTime -= 0.3f; ++stageLv; } } if (highScore < score) { highScore = score; for (int i = 0; i < 6; ++i) HighNum[i].sprite = numSprites[nowScore[i]]; if (!thisRoundHighScore) thisRoundHighScore = true; } } public void SetFirstHighScore() { int[] nowScore = new int[6]; for (int i = 100000, j = highScore, k = 0; i > 0; i /= 10, ++k) { nowScore[k] = j / i; j %= i; } for (int i = 0; i < 6; ++i) HighNum[i].sprite = numSprites[nowScore[i]]; } public void SeeLife() { if (playerLife == 3) { for (int i = 0; i < 3; ++i) NowPlayerLifeSpirte[i].gameObject.SetActive(true); } else if (playerLife > -1 && playerLife < 3) NowPlayerLifeSpirte[playerLife].gameObject.SetActive(false); } void CreateStonePool() { for (int i = 0; i < 8; ++i) { stoneKeyValue[i] = stones[i].GetInstanceID(); if (!stonePool.ContainsKey(stoneKeyValue[i])) stonePool.Add(stoneKeyValue[i], new Queue<GameObject>()); for (int j = 0; j < 50; ++j) { GameObject thisObj = Instantiate(stones[i], new Vector3(100, 100, 0), Quaternion.identity); if (i < 4) { thisObj.GetComponent<StoneScript>().hp = Random.Range(20, 50); thisObj.GetComponent<StoneScript>().type = 1; } else { thisObj.GetComponent<StoneScript>().hp = Random.Range(50, 90); thisObj.GetComponent<StoneScript>().type = 2; } thisObj.SetActive(false); thisObj.transform.parent = StoneManager.transform; stonePool[stoneKeyValue[i]].Enqueue(thisObj); } } } void CreateEffectPool() { EffectManager = GameObject.FindWithTag("EffectManager"); for (int i = 0; i < 2; ++i) { string a = i.ToString(); if (!effectPool.ContainsKey(20 + i))//20과 21이 각각 데미지, 디스트로이 이펙트 effectPool.Add(20 + i, new Queue<GameObject>()); for(int j = 0; j < 50; ++j) { GameObject thisObj = Instantiate(effects[i], new Vector3(100, 100, -1), Quaternion.identity); thisObj.SetActive(false); thisObj.transform.parent = EffectManager.transform; effectPool[(20 + i)].Enqueue(thisObj); } } } void PopStoneByPool(int stoneType) { float stoneXPos = Random.Range(-10.0f, 10.0f); GameObject thisStone = stonePool[stoneKeyValue[stoneType]].Dequeue(); thisStone.transform.position = new Vector3(stoneXPos, 7, 0); thisStone.SetActive(true); stonePool[stoneKeyValue[stoneType]].Enqueue(thisStone); } public void popEffectByPool(int effectType, Vector3 position) { GameObject thisEffect = effectPool[effectType].Dequeue(); thisEffect.transform.position = position; thisEffect.SetActive(true); thisEffect.GetComponent<EffectScript>().livingEffect = true; effectPool[effectType].Enqueue(thisEffect); } public void hitPlayer() { --playerLife; SeeLife(); popEffectByPool(20, player.transform.position); if (playerLife < 1) { GameOver(); popEffectByPool(21, player.transform.position); } } public void GameOver() { RenewalGameData(); gameoverDelay = true; } public void RenewalGameData() { gold += (score / 1000); if (thisRoundHighScore) gold += 10; ChangeGold(); player.SetActive(false); boolInGame = false; setGameData(); } public void getGameData() { string s; try { s = System.IO.File.ReadAllText("GameData.json"); } catch { playerDam = 10; highScore = 0; gold = 0; setGameData(); s = System.IO.File.ReadAllText("GameData.json"); } JsonTextParser textParser = new JsonTextParser(); JsonObjectCollection root = textParser.Parse(s) as JsonObjectCollection; JsonArrayCollection arr = root["UI"] as JsonArrayCollection; foreach(JsonObjectCollection obj in arr) { highScore = (int)(obj["HighScore"] as JsonNumericValue).Value; gold = (int)(obj["Gold"] as JsonNumericValue).Value; playerDam = (int)(obj["Damage"] as JsonNumericValue).Value; } Debug.Log(highScore); Debug.Log(gold); } public void setGameData() { JsonObjectCollection root = new JsonObjectCollection(); JsonArrayCollection arr = new JsonArrayCollection("UI"); JsonObjectCollection ui = new JsonObjectCollection(); ui.Add(new JsonNumericValue("HighScore", highScore)); ui.Add(new JsonNumericValue("Gold", gold)); ui.Add(new JsonNumericValue("Damage", playerDam)); arr.Add(ui); root.Add(arr); string s = root.ToString(); System.IO.File.WriteAllText("GameData.json", s); } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | //ButtonSizeUpScript.cs using System.Collections; using System.Collections.Generic; using UnityEngine; using DG.Tweening; public class ButtonSizeUpScript : MonoBehaviour { public GameObject GameManager = null; public int buttonType; //1 = start, 2 = exit, 3 = Shop, 4 = InGameMenu, 5 = InGameResumeGame, 6 = InGameMainMenu void OnMouseEnter() { if(buttonType != 4) gameObject.transform.DOScale(new Vector3(6, 6, 6), 0.2f); else gameObject.transform.DOScale(new Vector3(2.5f, 2.5f, 2.5f), 0.2f); } void OnMouseExit() { if (buttonType != 4) gameObject.transform.DOScale(new Vector3(5, 5, 5), 0.2f); else gameObject.transform.DOScale(new Vector3(1.5f, 1.5f, 1.5f), 0.2f); } void OnMouseDown() { if (buttonType != 4) gameObject.transform.DOScale(new Vector3(5.5f, 5.5f, 5.5f), 0.05f); else gameObject.transform.DOScale(new Vector3(1.75f, 1.75f, 1.75f), 0.2f); } void OnMouseUp() { if (buttonType != 4) gameObject.transform.DOScale(new Vector3(6, 6, 6), 0.05f); else gameObject.transform.DOScale(new Vector3(2.5f, 2.5f, 2.5f), 0.2f); if (buttonType == 1) GameManager.GetComponent<GameManagerScript>().MakeGameLayer(); else if (buttonType == 3) GameManager.GetComponent<GameManagerScript>().AtkLvUp(); else if (buttonType == 4 || buttonType == 5) GameManager.GetComponent<GameManagerScript>().InGameMenu(); else if (buttonType == 6) { GameManager.GetComponent<GameManagerScript>().RenewalGameData(); GameManager.GetComponent<GameManagerScript>().InGameMenu(); GameManager.GetComponent<GameManagerScript>().MakeMenuLayer(); } else quit(); } void quit() { #if UNITY_EDITOR UnityEditor.EditorApplication.isPlaying = false; #elif UNITY_WEBPLAYER Application.OpenURL("http://google.com"); #else Application.Quit(); #endif } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | //PlayerScript.cs using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerScript : MonoBehaviour { public GameObject GameManager = null; public GameObject EffectManager = null; public GameObject PlayerLaser = null; public float shootTime = 0; public bool boolShoot = false; public GameObject damEffect = null; public GameObject bombEffect = null; public GameObject nowLaser = null; // Use this for initialization void Start() { GameManager = GameObject.FindWithTag("GameManager"); EffectManager = GameObject.FindWithTag("EffectManager"); Vector3 laserPos = transform.position + new Vector3(0, 6.0f, 0); nowLaser = Instantiate(PlayerLaser, laserPos, Quaternion.identity); nowLaser.transform.parent = transform; nowLaser.gameObject.SetActive(false); } // Update is called once per frame void Update() { if (!GameManager.GetComponent<GameManagerScript>().isPause) { if (Input.GetKey(KeyCode.LeftArrow)) if (transform.position.x > -10) transform.Translate(-TimeManager.GameTime * 10.0f, 0, 0); if (Input.GetKey(KeyCode.RightArrow)) if (transform.position.x < 10) transform.Translate(TimeManager.GameTime * 10.0f, 0, 0); if (Input.GetKey(KeyCode.UpArrow)) if (transform.position.y < 6.5) transform.Translate(0, TimeManager.GameTime * 10.0f, 0); if (Input.GetKey(KeyCode.DownArrow)) if (transform.position.y > -4.5) transform.Translate(0, -TimeManager.GameTime * 10.0f, 0); if (Input.GetKey(KeyCode.Space)) { //hit.collider.gameObject.name.Contains("BigIron") Vector3 laserPos = transform.position + new Vector3(0, 6.0f, 0); if (!boolShoot) { nowLaser.transform.position = laserPos; boolShoot = true; nowLaser.gameObject.SetActive(true); } //RaycastHit[] objectHit = Physics.RaycastAll(transform.position, Vector3.up); RaycastHit2D[] objectHit = Physics2D.RaycastAll(transform.position, Vector2.up); foreach (RaycastHit2D hit in objectHit) { if (hit.collider.gameObject != null) { StoneScript hitStone = hit.collider.gameObject.GetComponent<StoneScript>(); if (hitStone) { if (hitStone.dam < hitStone.hp) { hitStone.damTime += TimeManager.GameTime; if (hitStone.damTime >= 0.5f) { hitStone.damTime -= 0.5f; //++hitStone.dam; hitStone.dam += GameManager.GetComponent<GameManagerScript>().playerDam; GameManager.GetComponent<GameManagerScript>().score += (hitStone.dam / 10) * 100; GameManager.GetComponent<GameManagerScript>().popEffectByPool(20, hit.collider.gameObject.transform.position); } } else if (hitStone.dam >= hitStone.hp) { hit.collider.gameObject.GetComponent<StoneScript>().dam = 0; hit.collider.gameObject.GetComponent<StoneScript>().damTime = 0.5f; hit.collider.gameObject.SetActive(false); GameManager.GetComponent<GameManagerScript>().score += (Random.Range(0, 6) * 100); GameManager.GetComponent<GameManagerScript>().popEffectByPool(21, hit.collider.gameObject.transform.position); } GameManager.GetComponent<GameManagerScript>().ChangeScore(); } } } } else { if (boolShoot) { nowLaser.gameObject.SetActive(false); boolShoot = false; } } } } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | //StoneScript.cs using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class StoneScript : MonoBehaviour { public GameObject GameManager = null; public GameObject EffectManager = null; public Vector3 mDir; public float speed; public float damTime; public int hp; public int dam; public bool damaging = false; public float attackTime; public float damTimeResetTime = 0; public bool INeedResetTime = false; public int type; // 1은 돌, 2는 철 // Use this for initialization void Start() { GameManager = GameObject.FindWithTag("GameManager"); EffectManager = GameObject.FindWithTag("EffectManager"); speed = UnityEngine.Random.Range(0.5f, 4.0f); damTime = 0.5f; dam = 0; if (transform.position.x >= 0) mDir = new Vector3(UnityEngine.Random.Range(-1.0f, 0), -1.0f, 0); else mDir = new Vector3(UnityEngine.Random.Range(1.0f, 0), -1.0f, 0); mDir.Normalize(); attackTime = 1.0f; } // Update is called once per frame void Update() { transform.Translate(mDir * TimeManager.GameTime * speed); if (transform.position.y <= -7) { initStone(); GameManager.GetComponent<GameManagerScript>().MinusScore(type); } if (transform.position.x >= 10 || transform.position.x <= -10) mDir.x *= -1; if (damaging) { attackTime += TimeManager.GameTime; if (attackTime >= 1.0f) { attackTime -= 1.0f; GameManager.GetComponent<GameManagerScript>().hitPlayer(); } } else { attackTime = 1.0f; } if (!GameManager.GetComponent<GameManagerScript>().boolInGame) { GameManager.GetComponent<GameManagerScript>().popEffectByPool(21, this.transform.position); initStone(); } } void initStone() { this.dam = 0; this.damTime = 0.5f; this.damaging = false; this.gameObject.SetActive(false); } private void OnTriggerEnter2D(Collider2D collision) { if (collision.name == "playerShip(Clone)") damaging = true; } private void OnTriggerExit2D(Collider2D collision) { if (collision.name == "playerShip(Clone)") damaging = false; } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | using System.Collections; using System.Collections.Generic; using UnityEngine; public class EffectScript : MonoBehaviour { public GameObject GameManager = null; public bool livingEffect; public float effecttime; public float duration; // Use this for initialization void Start () { GameManager = GameObject.FindWithTag("GameManager"); } // Update is called once per frame void Update () { if(livingEffect) { effecttime += TimeManager.GameTime; if(effecttime >= 5.0f) { effecttime -= 5.0f; initEffect(); } } } void initEffect() { livingEffect = false; effecttime = 0; this.gameObject.SetActive(false); } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | //TimeManager.cs using System.Collections; using System.Collections.Generic; using UnityEngine; public class TimeManager : MonoBehaviour { static public float GameTime = 0; static public int bTimeStop = 0; // Use this for initialization void Start () { } // Update is called once per frame void Update () { if (bTimeStop == 1) GameTime = 0; else GameTime = Time.deltaTime; } } | cs |
'BCA > 4. Unity' 카테고리의 다른 글
28. 슈팅게임 일단은 완성 (0) | 2018.03.30 |
---|---|
27일 : 간단한 슈팅게임 (0) | 2018.03.28 |
25일 : 연습용 슈팅게임 완성 (0) | 2018.03.22 |
24일 : dotween 이용, 2D슈팅게임 (0) | 2018.03.21 |
23일 : 체력바 추가 및 보스 강화 (0) | 2018.03.20 |
Comments