首页 > 解决方案 > Unity:从大图到地图/地形/纹理

问题描述

在 Unity 中,我想从一个大图像文件(9650 12573)生成 2D 地图。我已经有一个平铺/图像块生成器,但它生成了 1900 个图像 256 256,并且像这样手动创建地图似乎很困难(而且我有不止一个大图像要处理......)。

这些图像片段是这样排序的:

x/y.png

其中x从左到右开始,y从上到下。

正如 MelvMay在这里建议我的那样,我应该使用纹理来实现这一点,但是如何实现呢?

您对如何以编程方式实现这一目标有任何想法吗?或者可能与现有捆绑包一起使用?

非常感谢!

[编辑] 作为一种解决方法,我尝试了这个:

public class MapGenerator : MonoBehaviour {
    
    private Renderer m_Renderer;
    private Texture2D m_MainTexture;

    // Use this for initialization
    void Start () {

        //Fetch the Renderer from the GameObject
        m_Renderer = GetComponent<Renderer> ();
        m_MainTexture = LoadPNG("Assets/Tiles/Ressources/Map/0/3.png");
        m_Renderer.material.mainTexture = m_MainTexture;
        
    }

    private Texture2D LoadPNG(string filePath) {
 
        Texture2D tex = null;
        byte[] fileData;
    
        if (File.Exists(filePath))     {
            UnityEngine.Debug.Log(filePath);
            fileData = File.ReadAllBytes(filePath);
            tex = new Texture2D(2, 2);
            tex.LoadImage(fileData); //..this will auto-resize the texture dimensions.
        }

        return tex;

    }

}

但即使我将它应用到一个空对象上,我也只能在检查器中看到我的动态纹理,而不是在我按下运行时的场景中......

标签: c#unity3d

解决方案


推荐阅读