Creating a map with tiles of different shapes by reading an image
Update:
I managed to narrow down the problem to the 'resource.load' function. It appears the function cannot load the bitmap or cannot find it since its returning a null. The bitmap 'provinces.bmp' is found inside folder 'Map' which is found inside folder 'Resources'. The function resource.load has the string "Assets/Resources/Map/provinces.bmp" as variable in order to find the path. I tried removing the .bmp from the path, remove the folder Map and even remove 'Assets/Resources' from the path. None have worked. Does anyone know why this is happening?
Original:
Hi,
I am following this tutorial about Procedural Cave Generation and decided to try and create a script that uses an image instead of using procedural generation. The image consists of a map that is made up of tiles (similar to this). The tiles have different shapes and each tile uses a unique RGBA code. I created a new project with an empty object, created the folder 'Map' to store the image and stored the folder in another folder named 'Resources'. I also created a script named 'MeshGenerator' and attached it to the empty object. The following is the code inside the script.
using UnityEngine;
using System.Collections;
public class MeshGenerator : MonoBehaviour
{
public Texture2D mapImage;
public string mapImagePath = "Assets/Resources/Map/provinces.bmp";
private Color[,] mapArray;
void Start()
{
ImageToArray();
}
public void ImageToArray()
{
mapImage = (Texture2D)Resources.Load(mapImagePath, typeof (Texture2D));
mapArray = new Color[mapImage.width, mapImage.height];
for (int x = 0; x < mapArray.GetLength(0); x++)
{
for (int y = 0; y < mapArray.GetLength(1); y++)
{
mapArray[x, y] = mapImage.GetPixel(x,y);
}
}
}
public void OnDrawGizmos()
{
if(mapArray != null)
{
for (int x = 0; x < mapArray.GetLength(0); x++)
{
for (int y = 0; y < mapArray.GetLength(1); y++)
{
Gizmos.color = mapArray[x, y];
Vector3 position = new Vector3(-mapArray.GetLength(0) / 2 + x + .5f, 0, -mapArray.GetLength(1) / 2 + y + .5f);
Gizmos.DrawCube(position, Vector3.one);
}
}
}
}
}
Method ImageToArray loads the image and copies the RGBA of every pixel in an array. OnDrawGizmos uses the array to draw the map. When I try to run the game, I get the following errors:
The referenced script on this Behaviour is missing!
The referenced script on this Behaviour (Game Object 'Map Generator') is missing!
NullReferenceException: Object reference not set to an instance of an object
MeshGenerator.ImageToArray () (at Assets/Scripts/MeshGenerator.cs:18)
MeshGenerator.Start () (at Assets/Scripts/MeshGenerator.cs:12)
What am I doing wrong exactly?
Answer by Cmushi · Oct 06, 2016 at 05:29 PM
Okay I managed to fix this.
First don't include 'Assests/Resources' in the path.
Second, remove the file extension of the image in the path (.bmp).
Third, make sure that the the file can be read by going in the inspector, change 'texture type' to 'advanced' and tick 'read/write enabled'.
Your answer
Follow this Question
Related Questions
Namespace Media does not exist 1 Answer
How to show both sides of a plane 2 Answers
Bitmap texture from 3DS Max not importing 2 Answers
How can I convert a PhotoCaptureFrame byte data into SoftwareBitmap? 0 Answers
FBX import: Bitmap not linking to object 2 Answers