第三十七節(jié) 顯示下一個(gè)方塊

同樣都是顯示方塊的屏幕,與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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Swift1> Swift和OC的區(qū)別1.1> Swift沒有地址/指針的概念1.2> 泛型1.3> 類型嚴(yán)謹(jǐn) 對...
    cosWriter閱讀 11,136評論 1 32
  • 轉(zhuǎn)載自VR設(shè)計(jì)云課堂[http://www.lxweimin.com/u/c7ffdc4b379e]Unity S...
    水月凡閱讀 1,051評論 0 0
  • 國家電網(wǎng)公司企業(yè)標(biāo)準(zhǔn)(Q/GDW)- 面向?qū)ο蟮挠秒娦畔?shù)據(jù)交換協(xié)議 - 報(bào)批稿:20170802 前言: 排版 ...
    庭說閱讀 11,123評論 6 13
  • 現(xiàn)在的我倒是挺享受一個(gè)人的感覺,那種隨心的感覺真的很棒。內(nèi)心的豐盈撐起來外在的獨(dú)立,一人相處也不覺孤獨(dú),用心感受世...
    夢夕夢閱讀 109評論 0 2
  • 世界杯決賽當(dāng)天的中午兩點(diǎn)半,翻完了本書的最后一頁。書的作者,是法國人,儒勒.凡爾納。 這本書是《海...
    幾比閱讀 889評論 0 0