亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

C# 世界地圖生成器問題

C# 世界地圖生成器問題

C#
小怪獸愛吃肉 2022-12-24 12:50:05
我正在開發一款包含 1000 x 1000 正方形平鋪地圖的游戲,但我遇到了問題。我嘗試制作兩個不同的腳本來解決這個問題。第一個是對我需要的草率方法。我的第二個腳本是一種更有效的方法來滿足我的需要。腳本 1: void fill()    {        for (float i = 0; i != 1000; i++)        {            Instantiate(GameObject.Find("Dirt"), new Vector2(xPos++, yPos), Quaternion.identity);            xrepeat++;            if (xrepeat == 1000)            {                xPos = 0;                yPos = yPos - 1;                yrepeat++;                if(yrepeat != 1000)                {                    i = 0;                    xPos = 0;                }                if(xPos < 0) //Prevents an overflow.                {                    break;                }            }        }腳本 2:    void buildx()    {        for (int i = 1000; i != 0; i--)        {            Instantiate(GameObject.Find("Dirt"), new Vector2(xPos++, yPos), Quaternion.identity);            if (xPos == 1000)            {                buildy();            }        }    }    void buildy()    {        if (yPos == -1000)        {            Destroy(this); // Job is done, time to pack up        }        else        {            for (int i = 1000; i != 0; i--)            {                Instantiate(GameObject.Find("Dirt"), new Vector2(xPos, yPos--), Quaternion.identity);                buildx();            }        }    }第一個腳本將我的泥土塊復制了 1000 次,然后將 y 軸減去 1 并重復直到達到配額。那種工作但它在工作結束時放棄了。第二個腳本在 x 軸和 y 軸之間來回來回檢查 1000 配額,但由于某種原因它凍結了。我幾乎放棄了腳本 1 而選擇了腳本 2,因為我認為腳本 2 效率更高。有什么辦法可以讓腳本 2 工作嗎?
查看完整描述

1 回答

?
大話西游666

TA貢獻1817條經驗 獲得超14個贊

我強烈建議查看 Unity 的工作系統,但你的循環令人難以置信的混亂,不確定那里發生了什么......你似乎也太復雜了。所以這就是我將如何實例化一張 1000x1000 圖塊的地圖,如果我必須實例化它們的話:


public int mapWidth = 30;

public int mapHeight = 30;


void fill()

{

    // Doing this once at the beginning, so it isn't super expensive...

    GameObject basePrefab = GameObject.Find("Dirt"); 

    // Creating 1 Vector3 that we can just update the values on

    Vector3 spawnPosition = Vector3.zero;


    for(int x = 0; x < mapWidth; ++x)

    {

        // Update the spawn x Position

        spawnPosition.x = x;

        for(int y = mapHeight; y > 0; y--)

        {

            // Update the spawn y position

            spawnPosition.y = y;

            Instantiate(basePrefab, spawnPosition, Quaternion.identity);

        }

    } 

}

如果你想閱讀或查看類似系統的瓦片地圖的對象池示例,這是我不久前寫的一個答案應該給你它的精神:What is the fastest method to create and render large number of 2d Unity 中的精靈?


查看完整回答
反對 回復 2022-12-24
  • 1 回答
  • 0 關注
  • 124 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號