同樣都是顯示方塊的屏幕,與ScreenMain幾乎一樣,所以類的實(shí)現(xiàn)原理就不詳細(xì)講解了,直接按步驟操作即可。
01.添加新類:ScreenAttached,完整代碼請參考附件
using System.Collections.Generic;
using UnityEngine;
public class ScreenAttached : MonoBehaviour
{
public GameObject Pixel; // 像素點(diǎn)對象
GameObject[,] _screen; // 像素點(diǎn)數(shù)組
int _ScreenWidth = 4; // 屏幕寬度
int _ScreenHeight = 4; // 屏幕高度
int _ScreenPoint_x = 196; // 屏幕橫坐標(biāo)
int _ScreenPoint_y = 151; // 屏幕縱坐標(biāo)
int _PixelSize = 14; // 像素點(diǎn)大小
int _PixelInterval = 1; // 像素點(diǎn)間距
List<Layer> _layerList; // 圖層列表
List<MyPoint> _viewData; // 顯示數(shù)據(jù)
// 初始化
public void Init()
{
_viewData = new List<MyPoint>();
InitScreen();
InitLayer();
}
...
02.在場景中添加新對象
添加空物體,命名為ScreenAttached,坐標(biāo)歸零。
03.添加腳本組件
將ScreenAttached腳本添加至空物體ScreenAttached上。
04.綁定像素點(diǎn)與導(dǎo)演
將預(yù)制體Pixel拖放至ScreenAttached的Pixel對象上,再將ScreenAttached拖動(dòng)至導(dǎo)演對象的ScreenAttachedObj上,其實(shí)順序無所謂。
05.通過導(dǎo)演初始化
在導(dǎo)演類添加對應(yīng)的成員變量后,在InitGame()中初始化:
// 初始化附加屏幕
_screenAttachedScript = ScreenAttachedObj.GetComponent<ScreenAttached>();
if (_screenAttachedScript == null) return false;
_screenAttachedScript.Init();
06.下一個(gè)方塊實(shí)現(xiàn)邏輯
首次啟動(dòng)時(shí),需要生成兩個(gè)方塊:當(dāng)前方塊和下一個(gè)方塊。
之后的邏輯為:
當(dāng)前方塊 = 下一個(gè)方塊
下一個(gè)方塊 = 新的下一個(gè)方塊
07.添加下一個(gè)方塊成員變量
其實(shí)就是添加一個(gè)類型即可:
EBlockType _nextBlockType; // 下一個(gè)方塊的類型
08.添加生成下一個(gè)方塊的邏輯
在InitGame()和CreateNewBlock()中添加生成下一個(gè)方塊的代碼:
InitGame():
// 生成下一個(gè)方塊
_nextBlockType = BlockCreator.GetInstance().RandomBlockType();
_screenAttachedScript.GetLayer("DefaultLayer").ViewData = BlockCrea-tor.GetInstance().CreateBlock(_nextBlockType);
_screenAttachedScript.RefreshScreen();
CreateNewBlock():
// 生成方塊
void CreateNewBlock()
{
_blockLayer.Point = new MyPoint(20, 4);
// 使用上一次生成的類型創(chuàng)建方塊
_blockLayer.ViewData = BlockCreator.GetInstance().CreateBlock(_nextBlockType);
// 更新下一個(gè)方塊
_nextBlockType = BlockCreator.GetInstance().RandomBlockType();
_screenAttachedScript.GetLayer("DefaultLayer").ViewData = BlockCrea-tor.GetInstance().CreateBlock(_nextBlockType);
_screenMainScript.RefreshScreen();
_screenAttachedScript.RefreshScreen();
}
運(yùn)行結(jié)果:
代碼鏈接:https://pan.baidu.com/s/1h99cG38vtk9bmJo_A-PEow
提取碼:h91f