
Привет! Если вы попали на эту страницу, то скорее всего вы начали изучать практикум по
созданию игры на Unity. Ниже будет опубликована полезная информация и ссылки на внешние ресурсы, которые использовались при написании практикума «Разработка игры на Unity. С нуля и до реализации». А также здесь будет размещен программный код, который приводится в практикуме.
- Поиграть в готовую реализацию игры из книги «Разработка игры на Unity. С нуля и до реализации» можно на портале simmer.io по ссылке
- Или на Яндекс Играх по ссылке
- Готовый проект [old ver 1] в Unity можно скачать с Google Drive
C# скрипт файлы в готовой игре
EnemyDragon.cs
в этом скрипте описывается поведение дракона, которые драпает яйцо через заданные временные интервалы:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyDragon : MonoBehaviour
{
[Header("Set in Inspector")]
public GameObject dragonEggPrefab;
public float speed = 1f;
public float timeBetweenEggDrops = 1f;
public float leftRightDistance = 10f;
public float chanceDirections = 0.1f;
void Start()
{
Invoke("DropEgg", 2f);
}
void DropEgg()
{
Vector3 myVector = new Vector3(0.0f, 5.0f, 0.0f);
GameObject egg = Instantiate<GameObject>(dragonEggPrefab);
egg.transform.position = transform.position + myVector;
Invoke("DropEgg", timeBetweenEggDrops);
}
void Update()
{
Vector3 pos = transform.position;
pos.x += speed * Time.deltaTime;
transform.position = pos;
if (pos.x < -leftRightDistance)
{
speed = Mathf.Abs(speed);
}
else if (pos.x > leftRightDistance)
{
speed = -Mathf.Abs(speed);
}
}
private void FixedUpdate()
{
if (Random.value < chanceDirections)
{
speed *= -1;
}
}
}
DragonEgg.cs
в этом скрипте описывается поведение драконьего яйца:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DragonEgg : MonoBehaviour
{
public static float bottomY = -30f;
public AudioSource audioSource;
private void OnTriggerEnter(Collider other)
{
ParticleSystem ps = GetComponent<ParticleSystem>();
var em = ps.emission;
em.enabled = true;
Renderer rend;
rend = GetComponent<Renderer>();
rend.enabled = false;
audioSource = GetComponent<AudioSource>();
audioSource.Play();
}
void Update()
{
if (transform.position.y < bottomY)
{
Destroy(this.gameObject);
DragonPicker apScript = Camera.main.GetComponent<DragonPicker>();
apScript.DragonEggDestroyed();
}
}
}
EnergyShield.cs
этот скрипт отвечает за перемещение энергетических щитов, которые ловят яйцо, а также производит учет заработанного количества очком и количества доступных жизней:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class EnergyShield : MonoBehaviour
{
public Text scoreGT;
public AudioSource audioSource;
void Start()
{
GameObject scoreGO = GameObject.Find("Score");
scoreGT = scoreGO.GetComponent<Text>();
scoreGT.text = "0";
}
void Update()
{
Vector3 mousePos2D = Input.mousePosition;
mousePos2D.z = -Camera.main.transform.position.z;
Vector3 mousePos3D = Camera.main.ScreenToWorldPoint(mousePos2D);
Vector3 pos = this.transform.position;
pos.x = mousePos3D.x;
this.transform.position = pos;
}
private void OnCollisionEnter(Collision coll)
{
GameObject Collided = coll.gameObject;
if (Collided.tag == "DragonEgg")
{
Destroy(Collided);
}
int score = int.Parse(scoreGT.text);
score += 1;
scoreGT.text = score.ToString();
audioSource = GetComponent<AudioSource>();
audioSource.Play();
}
}
DragonPicker.cs
В этом скрипте описывается общая игровая логика, условия загрузки и переключения между сценами:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class DragonPicker : MonoBehaviour
{
public GameObject energyShieldPrefab;
public int numEnergyShield = 3;
public float energyShieldButtomY = -6f;
public float energyShieldRadius = 1.5f;
public List<GameObject> basketList;
void Start()
{
basketList = new List<GameObject>();
for (int i = 1; i <= numEnergyShield; i++)
{
GameObject tBasketGo = Instantiate<GameObject>(energyShieldPrefab);
tBasketGo.transform.position = new Vector3(0, energyShieldButtomY, 0);
tBasketGo.transform.localScale = new Vector3(1 * i, 1 * i, 1 * i);
basketList.Add(tBasketGo);
}
}
public void DragonEggDestroyed()
{
GameObject[] tDragonEggArray = GameObject.FindGameObjectsWithTag("DragonEgg");
foreach (GameObject tGO in tDragonEggArray)
{
Destroy(tGO);
}
int basketIndex = basketList.Count - 1;
GameObject tBasketGo = basketList[basketIndex];
basketList.RemoveAt(basketIndex);
Destroy(tBasketGo);
if (basketList.Count == 0)
{
SceneManager.LoadScene("_0Scene");
}
}
}
MainMenu.cs
здесь описывается работа главного меню в игре
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class MainMenu : MonoBehaviour
{
public void PlayGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
public void QuitGame()
{
Application.Quit();
}
}
Pause.cs
а в этом скрипте реализована возможность поставить игру на паузу:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Pause : MonoBehaviour
{
private bool paused = false;
public GameObject panel;
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
if (!paused)
{
Time.timeScale = 0;
paused = true;
panel.SetActive(true);
}
else
{
Time.timeScale = 1;
paused = false;
panel.SetActive(false);
}
}
if (Input.GetKeyDown(KeyCode.Escape))
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex - 1);
}
}
}