Advertisement
Redee

применение Делегата....

Jan 30th, 2017
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.99 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class GameController : MonoBehaviour {
  6.  
  7.     delegate void DoSpawn();
  8.     DoSpawn doMySpawn;
  9.  
  10.     public GameObject hazard;
  11.     public Vector3 spawnValues = new Vector3(6, 0, 16);
  12.     public float spawnWait = .5f;
  13.     public float wait = 1;
  14.  
  15.     float nextTime;
  16.  
  17.     // Use this for initialization
  18.     void Start ()
  19.     {
  20.         nextTime = Time.time + wait;
  21.         doMySpawn = Do1;
  22.     }
  23.  
  24.     void SpawnWaves()
  25.     {
  26.         Vector3 spawnPosition = new Vector3(
  27.             Random.Range(-spawnValues.x, spawnValues.x),
  28.             spawnValues.y, spawnValues.z);
  29.            
  30.         Quaternion spawnRotation = Quaternion.identity;
  31.         Instantiate(hazard, spawnPosition, spawnRotation);
  32.     }
  33.  
  34.     void Do1()
  35.     {
  36.         if (Time.time > nextTime)
  37.         {
  38.             Do2();
  39.             doMySpawn = Do2;
  40.         }
  41.     }
  42.  
  43.     void Do2()
  44.     {
  45.         if (Time.time > nextTime)
  46.         {
  47.             nextTime = Time.time + spawnWait;
  48.             SpawnWaves();
  49.         }
  50.     }
  51.  
  52.     // Update is called once per frame
  53.     void Update () {
  54.         doMySpawn();
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement