2D沙箱地形生成的优化问题

时间:2015-07-20 02:52:59

标签: c# unity3d 2d lag terrain

我的游戏:

enter image description here

每个块都有一个在更新时不运行任何内容的类,并且有200 x 10跨度的块。我稍后会实现随机地形生成,这就是我想要做的。

BLOCK CODE :(在每一个区块中,我应该优化吗?还是以不同的方式进行?)

using UnityEngine;
using System.Collections;

public class scr_blockMechanics : MonoBehaviour {
//#EDITABLE VARS
    public string block_blockType;
    private SpriteRenderer spriteRenderer;

    public bool isMineable { get; set; }
    public bool isRandomlyGenerated { get; set; }
    public bool isWall { get; set; }
//#STATIC VARS
    GameObject obj_blockItem;
//#START
    void Start () {
        obj_blockItem = GameObject.Find("main_blockItem");
        spriteRenderer = GetComponent<SpriteRenderer>();

        if(isRandomlyGenerated == false){
            isMineable = false;
        }
//#CREATING WALLS
        /*if(isRandomlyGenerated == true && isWall == false){
            //#TODO Create walls!
            GameObject obj_blockWall = (GameObject)Instantiate(gameObject, transform.localPosition, Quaternion.identity);
            obj_blockWall.name = obj_blockWall.name + " WALL";
            obj_blockWall.transform.localPosition = new Vector3(
                obj_blockWall.transform.localPosition.x,
                obj_blockWall.transform.localPosition.y,
                5);


            Collider2D obj_blockWall_collider = obj_blockWall.GetComponent<Collider2D>();
            obj_blockWall_collider.enabled = false;

            Color obj_blockWall_color = Color.grey;//new Vector4(64, 64, 64, 1);

            SpriteRenderer spriteRenderer_wall = obj_blockWall.GetComponent<SpriteRenderer>();
            spriteRenderer_wall.color = obj_blockWall_color;

            scr_blockMechanics blockMechanics_wall = obj_blockWall.GetComponent<scr_blockMechanics>();
            blockMechanics_wall.isRandomlyGenerated = true;
            blockMechanics_wall.isMineable = false; //#TODO: not yet mineable, because it's a wall.
            blockMechanics_wall.isWall = true;

            obj_blockWall.name = blockMechanics_wall.block_blockType+" WALL";
        }*/
    }
//#CREATE ITEM
    void createItem(int uid){
        GameObject obj_blockItemNew = (GameObject)Instantiate(obj_blockItem, transform.localPosition, Quaternion.identity);
        obj_blockItemNew.rigidbody2D.isKinematic = false;
        obj_blockItemNew.name = gameObject.name + " ITEM";
    }
//#DESTROY BLOCK
    void destroyBlock(){
        //print ("'"+transform.name+"' has been destroyed!");
        //spriteRenderer.color = Color.red;
        createItem(0);
        Destroy(transform.gameObject);
    }
//#UPDATE
    void Update () {
        if(renderer.IsVisibleFrom(Camera.main) == true){
            renderer.enabled = true;//gameObject.SetActive(true);
        }

        if(renderer.IsVisibleFrom(Camera.main) == false){
            renderer.enabled = false;
        }
    }

}

&#39; RANDOM&#39;地形生成

using UnityEngine;
using System.Collections;

public class scr_randomGen : MonoBehaviour {


    private SpriteRenderer spriteRenderer;
    private string[] block_blockTypes = new string[]{
        "DIRT",        //#UID 0
        "STONE",       //#UID 1
        "STONE_BRICK", //#UID 2
    };

    void GENERATE(){
        //#DEFAULTS
        GameObject obj_block = GameObject.Find("main_block");
        int pos_startingPosX = -100;
        int pos_endingPosX = 100;

        int pos_startingPosY = 0;
        int pos_endingPosY = 10;

        float X = 0;
        float Y = 0;
        //#TERRAIN GENERATION
        for(X=pos_startingPosX;X<pos_endingPosX;X=X+1){
            GameObject obj_blockNew = (GameObject)Instantiate(obj_block, new Vector3(0 + (1*X),0,0), Quaternion.identity);
            scr_blockMechanics blockMechanics = obj_blockNew.GetComponent<scr_blockMechanics>();

            blockMechanics.isMineable = true;
            blockMechanics.isRandomlyGenerated = true;
            blockMechanics.block_blockType = block_blockTypes[2];

            obj_blockNew.name = blockMechanics.block_blockType+" "+X+", "+Y;

            for(Y=pos_startingPosY;Y<pos_endingPosY;Y=Y+1){
                obj_blockNew = (GameObject)Instantiate(obj_block, new Vector3(X,1 + (1*Y),0), Quaternion.identity);
                blockMechanics = obj_blockNew.GetComponent<scr_blockMechanics>();

                blockMechanics.isMineable = true;
                blockMechanics.isRandomlyGenerated = true;
                blockMechanics.block_blockType = block_blockTypes[2];

                obj_blockNew.name = blockMechanics.block_blockType+" "+X+", "+Y;
            }
        }
    }

    void Start () {
        GENERATE();
    }

    void Update () {

    }
}

我有它,所以如果相机没有看到块,它会禁用它们的精灵渲染器,因此它不会渲染太多。

我应该整体禁用它们吗? 我在运行游戏时获得6fps

当我只有 10 - 20块而不是2000时,我得到 130 FPS (在我看来仍然很低)

优化此功能的最佳方法是什么?这样我至少得到70 fps?我想使用Java,但我对编辑感到不舒服,而且我已经花了2个小时的工作。

我已经看过人们用Java编写的项目,而且它更顺畅,他们不必担心滞后。我看过人们的帖子说,有可能在Unity中创建一个类似Terarria的游戏,但它总是会变得迟钝。

有人知道如何解决延迟问题吗?

0 个答案:

没有答案