創(chuàng)建2D游戲工程惦界,和場景
-
創(chuàng)建2D工程
-
創(chuàng)建2D場景(場景會保存在Asset目錄下面)
創(chuàng)建工作層
為了使游戲?qū)ο笤趫鼍爸氐膶哟尾粫e亂,可以創(chuàng)建不同的工作層來管理游戲?qū)ο罅摺亩褂螒蛴螒驁鼍暗膶哟胃臃置髡赐幔皖愃芇S里面的圖層的概念。
-
調(diào)出工作層面板
-
這里我們創(chuàng)建兩個工作層雾消,一個是背景工作層灾搏,一個是前景工作層
添加靜態(tài)的物體
-
導(dǎo)入靜態(tài)資源
- 稍后會把資源傳到網(wǎng)盤上面去
資源地址 先占位
- 這里啰嗦一下Unity支持的資源導(dǎo)入的方式主要有,直接將資源復(fù)制到文件的目錄下面去立润,將資源拖動到Project視圖中的Asset文件夾中狂窑,通過Assets -> Import New Asset進行導(dǎo)入
-
創(chuàng)建游戲的天空背景(就是創(chuàng)建一個精靈)
-
精靈各項屬性的意思
- Sprite 圖片信息
- Color 顏色信息
- Material 材質(zhì)信息
- Sorting Layer 分類層,層級越靠前優(yōu)先級越高范删,相同情況后被渲染
- Order in Layer 層中的順序蕾域,數(shù)值越大優(yōu)先級越高相同情況后被渲染
-
修改精靈的屬性
-
創(chuàng)建游戲的草地精靈(小技巧,拖動資源到Hierachy面板可以直接創(chuàng)建精靈)
- 草地的層應(yīng)該和天空的層一樣到旦,渲染順序要比天空要大
-
多復(fù)制(選中 Ctrl + D)幾個草地旨巷,調(diào)整位置放到層的下面
-
調(diào)整相機的Size和位置
添加角色和控制
創(chuàng)建Sprite動畫(將一張多紋理圖切成多個Sprite)
-
選擇一個多紋理圖,切割~~
-
切割完成
-
制作動畫(創(chuàng)建一個Sprite添忘,并把動畫的第一幀給這個Sprite)
-
創(chuàng)建一個文件夾用來保存動畫
-
給剛才創(chuàng)建的那個Sprite創(chuàng)建一個動畫
用Sprite的變化作為動畫幀
不斷的添加關(guān)鍵幀 -
創(chuàng)建一個文件夾(Scripts)用來存放腳本(C#)
-
創(chuàng)建一個腳本(SwanMove)
雙擊腳本并寫代碼(VS 果然是宇宙最強ide)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SwanMove : MonoBehaviour {
private float moveSpeed = 4;//移動的速度
// Use this for initialization
void Start () {
//設(shè)置初始位置
transform.position = new Vector3(22,3,0);
}
// Update is called once per frame
void Update () {
if (transform.position.x > -22)
{
//移動
transform.Translate(Vector3.right * -moveSpeed * Time.deltaTime);
}
else {
transform.position = new Vector3(22, 3, 0);
}
}
}
-
將腳本設(shè)置給Sprite(直接將腳本拖動到Sprite上面就行)
創(chuàng)建主要的游戲?qū)ο?/h3>
-
創(chuàng)建Sprite 都為前景層采呐,只是Order in Layout不同
-
調(diào)整位置
創(chuàng)建保齡球Sprite
-
為保齡球添加剛體
-
添加一個圓形的碰撞體(2D碰撞體)
將保齡球做成預(yù)設(shè)體(就是方便多次使用)
-
創(chuàng)建一個Asset文件夾取名 Prefabs
直接拖過來
-
創(chuàng)建一個空對象來控制保齡球的水平高度
-
創(chuàng)建一個腳本來控制保齡球的實例化 使保齡球從天上一個一個的落下
public class GameController : MonoBehaviour {
public GameObject ball;//保齡球
private float maxWidth;
private float time = 2;
private GameObject newball;
// Use this for initialization
void Start () {
//將屏幕的寬度轉(zhuǎn)換成世界坐標
Vector3 screenPos = new Vector3(Screen.width, 0, 0);
Vector3 moveWidth = Camera.main.ScreenToWorldPoint(screenPos);
//獲取保齡球自身的寬度
float ballWidth = ball.GetComponent<Renderer>().bounds.extents.x;
//計算保齡球?qū)嵗恢玫膶挾? maxWidth = moveWidth.x - ballWidth;
}
// Update is called once per frame
void Update () {
}
private void FixedUpdate()
{
time -= Time.deltaTime;
if (time < 0) {
time = Random.Range(1.5f, 2.0f);
float posX = Random.Range(-maxWidth, maxWidth);
Vector3 spawnPosition = new Vector3(posX,transform.position.y,0);
newball = Instantiate(ball, spawnPosition, Quaternion.identity);
Destroy(newball, 10);
}
}
}
-
將這個腳本放置到創(chuàng)建的空對象上面去并設(shè)置屬性的初始值
創(chuàng)建一個腳本控制帽子的移動
public class HatController : MonoBehaviour {
private Vector3 rawPosition;
private Vector3 hatPosition;
private float maxWidth;
// Use this for initialization
void Start () {
Vector3 screenPos = new Vector3(Screen.width,0,0);
Vector3 moveWidth = Camera.main.ScreenToWorldPoint(screenPos);
float hatW = GetComponent<Renderer>().bounds.extents.x;
hatPosition = transform.position;
maxWidth = moveWidth.x - hatW;
}
// Update is called once per frame
void Update () {
}
private void FixedUpdate()
{
rawPosition = Camera.main.ScreenToViewportPoint(Input.mousePosition);
hatPosition = new Vector3(rawPosition.x, hatPosition.y, 0);
hatPosition.x = Mathf.Clamp(hatPosition.x, -maxWidth, maxWidth);
GetComponent<Rigidbody2D>().MovePosition(hatPosition);
}
}
創(chuàng)建2D阻擋物
-
主要是給地面添加一個box的剛體
添加2D效果
- 導(dǎo)入資源包
- 添加粒子效果
Nothing is certain in this life. The only thing i know for sure is that. I love you and my life. That is the only thing i know. have a good day
創(chuàng)建Sprite 都為前景層采呐,只是Order in Layout不同
調(diào)整位置
創(chuàng)建保齡球Sprite
為保齡球添加剛體
添加一個圓形的碰撞體(2D碰撞體)
將保齡球做成預(yù)設(shè)體(就是方便多次使用)
創(chuàng)建一個Asset文件夾取名 Prefabs
創(chuàng)建一個空對象來控制保齡球的水平高度
創(chuàng)建一個腳本來控制保齡球的實例化 使保齡球從天上一個一個的落下
public class GameController : MonoBehaviour {
public GameObject ball;//保齡球
private float maxWidth;
private float time = 2;
private GameObject newball;
// Use this for initialization
void Start () {
//將屏幕的寬度轉(zhuǎn)換成世界坐標
Vector3 screenPos = new Vector3(Screen.width, 0, 0);
Vector3 moveWidth = Camera.main.ScreenToWorldPoint(screenPos);
//獲取保齡球自身的寬度
float ballWidth = ball.GetComponent<Renderer>().bounds.extents.x;
//計算保齡球?qū)嵗恢玫膶挾? maxWidth = moveWidth.x - ballWidth;
}
// Update is called once per frame
void Update () {
}
private void FixedUpdate()
{
time -= Time.deltaTime;
if (time < 0) {
time = Random.Range(1.5f, 2.0f);
float posX = Random.Range(-maxWidth, maxWidth);
Vector3 spawnPosition = new Vector3(posX,transform.position.y,0);
newball = Instantiate(ball, spawnPosition, Quaternion.identity);
Destroy(newball, 10);
}
}
}
將這個腳本放置到創(chuàng)建的空對象上面去并設(shè)置屬性的初始值
創(chuàng)建一個腳本控制帽子的移動
public class HatController : MonoBehaviour {
private Vector3 rawPosition;
private Vector3 hatPosition;
private float maxWidth;
// Use this for initialization
void Start () {
Vector3 screenPos = new Vector3(Screen.width,0,0);
Vector3 moveWidth = Camera.main.ScreenToWorldPoint(screenPos);
float hatW = GetComponent<Renderer>().bounds.extents.x;
hatPosition = transform.position;
maxWidth = moveWidth.x - hatW;
}
// Update is called once per frame
void Update () {
}
private void FixedUpdate()
{
rawPosition = Camera.main.ScreenToViewportPoint(Input.mousePosition);
hatPosition = new Vector3(rawPosition.x, hatPosition.y, 0);
hatPosition.x = Mathf.Clamp(hatPosition.x, -maxWidth, maxWidth);
GetComponent<Rigidbody2D>().MovePosition(hatPosition);
}
}
主要是給地面添加一個box的剛體
Nothing is certain in this life. The only thing i know for sure is that. I love you and my life. That is the only thing i know. have a good day
:)