- Home /
Issue with Drawing a GUI texture
So I'm creating a color edit mode for my game. I've determine how to do it and am working on implementing it in Unity. Thing is the images need some preprocessing to turn them into maps since photoshop aliasing of lines doesn't mean every pixel that needs a specific color is that specific color in the image.
Thing is I want to be able to show the changes as they are made so I am cacheing a copy of the texture and manipulating it. When I use that texture in GUI.DrawTexture(rect,texture) though it draws an opaque grey rectangle whereas if I use the original texture it draws the texture as it should be seen. How can I get the copy to render properly in the GUI.
public float tolerance = 0.1f;
public void GetPallette()
{
if (texture != null)
{
if(ColorPallette.Count != 0)
ColorPallette.Clear();
Color temp = new Color();
if (tolerance < 1.0f / 255.0f)
tolerance = 1.0f / 255.0f;
int indexer = -1;
InternalTextureCopy = new Texture2D(texture.width, texture.height,texture.format,false);
InternalTextureCopy.SetPixels(texture.GetPixels(0),0);
for (int i = 0; i < InternalTextureCopy.height; i++)
{
for (int j = 0; j < InternalTextureCopy.width; j++)
{
temp = InternalTextureCopy.GetPixel(j, i);
if (temp.a != 1.0f)//Ignore pure alpha colors
continue;
//See if color is unique
indexer = isUniqueColor(ref temp);
if (indexer < 0)
{
ColorPallette.Add(new ColorMap(temp));
ColorPallette[ColorPallette.Count - 1].indexes.Add((uint)(i * InternalTextureCopy.width + j));
}
else
{
ColorPallette[indexer].indexes.Add((uint)(i
*InternalTextureCopy.width + j));
}
}
}
}
public void OnGUI()
{
Rect rect = new Rect();
if (texture)
{
rect.x = rect.y = 0;
if (texture.height > texture.width)
{
if (texture.height > 250)
rect.height = 250;
else
rect.height = texture.height;
rect.width = rect.height * (float)texture.width / (float)texture.height;
}
else
{
if (texture.width > 250)
rect.width = 250;
else
{
rect.width = texture.width;
}
rect.height = rect.width * (float)texture.height / (float)texture.width;
}
rect.height = rect.width * (float)texture.height / (float)texture.width;
}
if (texture)
{
texture = EditorGUILayout.ObjectField(texture, typeof(Texture2D), GUILayout.Width(rect.width), GUILayout.Height(rect.height)) as Texture2D;
}
else
texture = EditorGUILayout.ObjectField(texture, typeof(Texture2D), GUILayout.MaxWidth(250.0f)) as Texture2D;
if (InternalTextureCopy != null)
{
rect.x += rect.width + 10;
GUI.DrawTexture(rect,texture,ScaleMode.ScaleToFit,true);
}
if (GUILayout.Button("Get Pallette"))
{
GetPallette();
}
tolerance = EditorGUILayout.FloatField("Tolerance: ", tolerance);
EditorGUILayout.LabelField("Colors: "
+ ColorPallette.Count);
for (int i = 0; i < ColorPallette.Count; i++)
{
GUILayout.BeginHorizontal();
int g = 0;
//Highligh color channel in picture for easy analysis
if(GUILayout.Button("Highlight",GUILayout.MaxWidth(75.0f)))
{
uint Length = (uint)InternalTextureCopy.width*(uint)InternalTextureCopy.height;
int k = 0;
Color dummy = new Color();
for (int j = 0; j < texture.height; j++)
{
for(int l = 0;l < texture.width;l++)
if (j == ColorPallette[i].indexes[k])
{
InternalTextureCopy.SetPixel(l, j, Color.white);
k++;
}
else
{
dummy = InternalTextureCopy.GetPixel(l,j);
dummy.a *= 0.4f;
InternalTextureCopy.SetPixel(l, j, dummy);
}
}
}
//Delete Color Channel Completely
if (GUILayout.Button("Delete", GUILayout.MaxWidth(75.0f)))
{
if (!ReplacementMode)
ColorPallette.Remove(ColorPallette[i]);
}
//Replace Selection with another existing color channel - bring up window
if (i < ColorPallette.Count && GUILayout.Button("Replace", GUILayout.MaxWidth(75.0f)))
{
if (!ReplacementMode)
{
ReplacementMode = true;
ReplacementIndex = i;
}
else
{
if (ReplacementIndex == i)
{
ReplacementMode = false;
ReplacementIndex = -1;
}
else
{
ColorPallette[i].indexes.AddRange(ColorPallette[ReplacementIndex].indexes);
ColorPallette.Remove(ColorPallette[ReplacementIndex]);
ColorPallette[i].indexes.Sort();
ReplacementMode = false;
ReplacementIndex = -1;
}
}
}
if (i < ColorPallette.Count) //Needed because if a delete happens this will throw an exception since the count will have changed
ColorPallette[i].color = EditorGUILayout.ColorField(ColorPallette[i].color,GUILayout.MaxWidth(125.0f));
GUILayout.EndHorizontal();
}
}
}
//How I draw it
Your answer
Follow this Question
Related Questions
Compnent.guiTexture is obsolete 1 Answer
Wearied Error 1 Answer
GUI Texture error. 1 Answer