Problem: An unexpected value is observed when reading the Color32 pixel color of a 2D texture
sprite.
EDIT: SOLVED
SOLUTION: the problem was in the dictionary the alpha channel was set to zero. In the script I check for an alpha of zero (if so then no object is created). Changing the dictionary to reflect an alpha of greater than zero solved, the issue
Black pixels are read correctly and produce wall prefabs. But the 1 red pixel reads as a white pixel when observed in the debug console.
//EXPECTED VALUE
//c: RGBA(255, 0, 0, 0)
c: RGBA(0, 0, 0, 0)
UnityEngine.Debug:Log(Object)
LevelGenerator:GenerateTile(Color32, Int32, Int32) (at Assets/scripts/LevelGenerator.cs:30)
LevelGenerator:GenerateLevel() (at Assets/scripts/LevelGenerator.cs:23)
LevelGenerator:Start() (at Assets/scripts/LevelGenerator.cs:11)
Below is the code generating a level
// this is the color dictionary
public class ColorToPrefab {
public Color32 color;
public GameObject prefab;
}
// this is the generator script
public Texture2D map;
public ColorToPrefab[] colorMappings;
void Start () {
GenerateLevel();
}
void GenerateLevel ()
{
Color32[] pColor = map.GetPixels32 (); // I store pixels in a list
for (int x = 0; x < map.width; x++) // then go through the list
{
for (int y = 0; y < map.height; y++)
{
Debug.Log ("* * * * * GeneratingTile --> x: " + x + ", y: " + y+" * * * * *");
int index = x + y * map.width;
GenerateTile(pColor[index],x, y); // and generate the tile
}
}
}
void GenerateTile (Color32 c, int x, int y)
{
Debug.Log ("c: "+c);
if (c.a == 0) {
// The pixel is a path. Let's ignore it!
Debug.Log("PATH FOUND -RETURN-");
return;
}
foreach (ColorToPrefab colorMapping in colorMappings) // I go through each index in the dictionary
{
Debug.Log ("ColorMapping.color: "+colorMapping.prefab.name);
if (colorMapping.color.Equals(c)) // and see if the pixel color matches a dictionary color
{
Debug.Log ("ColorMapping.color == pixelColor --> CREATING OBJECT");
Vector3 position = new Vector3(x, 1, y);
Instantiate(colorMapping.prefab, position, Quaternion.identity, transform);
return;
}
}
}
I’ve uploaded the sprite and below is the sprite’s Meta data
fileFormatVersion: 2
guid: 0adda6bc42ae9234f88f40448e037606
timeCreated: 1504362991
licenseType: Free
TextureImporter:
fileIDToRecycleName: {}
serializedVersion: 4
mipmaps:
mipMapMode: 0
enableMipMap: 0
sRGBTexture: 1
linearTexture: 0
fadeOut: 0
borderMipMap: 0
mipMapsPreserveCoverage: 0
alphaTestReferenceValue: 0.5
mipMapFadeDistanceStart: 1
mipMapFadeDistanceEnd: 3
bumpmap:
convertToNormalMap: 0
externalNormalMap: 0
heightScale: 0.25
normalMapFilter: 0
isReadable: 1
grayScaleToAlpha: 0
generateCubemap: 6
cubemapConvolution: 0
seamlessCubemap: 0
textureFormat: 1
maxTextureSize: 2048
textureSettings:
serializedVersion: 2
filterMode: 0
aniso: 1
mipBias: -1
wrapU: 1
wrapV: 1
wrapW: 1
nPOTScale: 0
lightmap: 0
compressionQuality: 50
spriteMode: 1
spriteExtrude: 1
spriteMeshType: 0
alignment: 0
spritePivot: {x: 0.5, y: 0.5}
spriteBorder: {x: 0, y: 0, z: 0, w: 0}
spritePixelsToUnits: 100
alphaUsage: 1
alphaIsTransparency: 1
spriteTessellationDetail: -1
textureType: 8
textureShape: 1
maxTextureSizeSet: 0
compressionQualitySet: 0
textureFormatSet: 0
platformSettings:
- buildTarget: DefaultTexturePlatform
maxTextureSize: 2048
textureFormat: -1
textureCompression: 0
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
- buildTarget: Standalone
maxTextureSize: 2048
textureFormat: -1
textureCompression: 0
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
- buildTarget: WebGL
maxTextureSize: 2048
textureFormat: -1
textureCompression: 0
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
spriteSheet:
serializedVersion: 2
sprites: []
outline: []
physicsShape: []
spritePackingTag:
userData:
assetBundleName:
assetBundleVariant: