
#6 - Создание самой игры
Начиная с этого урока вы приступаете к созданию механики для игры. Вы создадите основные функции и продумаете логику для полностью всей игры.
Видеоурок
Материалы для курса
Чтобы скачивать материалы к видеокурсам необходимо оформить подписку на сайт
Исходный код
Скрипт RandCol
using UnityEngine;
using System.Collections;
public class RandCol : MonoBehaviour {
public bool main = false, right = false;
private static Color aColor;
void Awake () {
if (main)
aColor = new Vector4 (Random.Range (0.1f, 1f), Random.Range (0.1f, 1f), Random.Range (0.1f, 1f), 1);
}
void Start () {
if (main || right)
GetComponent <Renderer> ().material.color = aColor;
else
GetComponent <Renderer> ().material.color = new Vector4 (aColor.r + Random.Range (0.1f, 0.3f), aColor.g + Random.Range (0.1f, 0.3f), aColor.b + Random.Range (0.1f, 0.3f), aColor.a);
}
}
Скрипт RightOne
using UnityEngine;
using System.Collections;
public class RightOne : MonoBehaviour {
private GameObject mainCube;
void Start () {
mainCube = GameObject.Find ("Main Cube");
}
void OnMouseDown () {
if (GetComponent <Renderer> ().material.color == mainCube.GetComponent <Renderer> ().material.color)
mainCube.GetComponent <GameCntrl> ().next = true;
else
mainCube.GetComponent <GameCntrl> ().lose = true;
}
}
Скрипт GameCntrl
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class GameCntrl : MonoBehaviour {
public GameObject colBlock;
public Vector3 [] positions;
private GameObject block;
private GameObject [] blocks = new GameObject[4];
private int rand, count;
private float rCol, gCol, bCol;
public Text score;
private static Color aColor;
[HideInInspector]
public bool next, lose;
void Start () {
count = 0;
next = false;
lose = false;
rand = Random.Range (0, positions.Length);
for (int i = 0; i < positions.Length; i++) {
blocks [i] = Instantiate (colBlock, positions[i], Quaternion.identity) as GameObject;
if (rand == i)
block = blocks [i];
}
block.GetComponent <RandCol> ().right = true;
}
void Update () {
if (lose)
playerLose ();
if (next && !lose)
nextColors ();
}
void nextColors () {
count++;
score.text = count.ToString ();
aColor = new Vector4 (Random.Range (0.1f, 1f), Random.Range (0.1f, 1f), Random.Range (0.1f, 1f), 1);
GetComponent <Renderer> ().material.color = aColor;
next = false;
if (count < 3) {
rCol = 0.2f;
gCol = 0.2f;
bCol = 0.2f;
} else if (count >= 3 && count < 5) {
rCol = 0.1f;
gCol = 0.1f;
bCol = 0f;
} else if (count >= 5) {
rCol = 0f;
gCol = 0f;
bCol = 0.05f;
}
// New colors for blocks
rand = Random.Range (0, positions.Length);
for (int i = 0; i < positions.Length; i++) {
if (i == rand)
blocks [i].GetComponent <Renderer> ().material.color = aColor;
else {
float r = aColor.r + Random.Range (0.1f, rCol) > 1f ? 1f : aColor.r + Random.Range (0.1f, rCol);
float g = aColor.g + Random.Range (0.1f, gCol) > 1f ? 1f : aColor.g + Random.Range (0.1f, gCol);
float b = aColor.b + Random.Range (0.1f, bCol) > 1f ? 1f : aColor.b + Random.Range (0.1f, bCol);
blocks [i].GetComponent <Renderer> ().material.color = new Vector4 (r, g, b, aColor.a);
}
}
}
void playerLose () {
print ("Player lose");
}
}
Также стоит посмотреть
Комментарии