我有一个脚本可以逐个像素地检查PNG文件的RGB值
我想弄清楚的不是使用此代码(错误代码)
Debug.Log(Color.red + Color.green + Color.blue);
此代码将此内容返回到日志
RGBA(1.000, 0.000, 0.000, 1.000)RGBA(0.000, 1.000, 0.000, 1.000)RGBA(0.000, 0.000, 1.000, 1.000)
固定代码
Color32 currentPixel = mapImage.GetPixel(x, z);
Debug.Log(currentPixel);
你可以看到它每次都检查rgb,但只从代码Color.red等命令中查找红色,绿色和蓝色
你可以做同样的事情,但让debug.log返回
RGBA(255, 255, 255, 255)
EDIT2 --- Code Now Works -------------
这是整个代码
using UnityEngine;
using System.Collections;
using UnityEditor;
public class CustomWindow : EditorWindow
{
[MenuItem("Window/My Custom Window")]
static void ShowWindow()
{
EditorWindow.GetWindow(typeof(CustomWindow));
}
void OnInspectorUpdate()
{
Repaint();
}
Texture2D mapImage = null;
void OnGUI()
{
mapImage = EditorGUILayout.ObjectField("Map to render", mapImage, typeof(Texture2D), true) as Texture2D;
if (mapImage != null)
{
if (GUILayout.Button("Add to scene"))
{
int width = mapImage.width;
int height = 1;
int depth = mapImage.height;
for (int z = 0; z < depth; z++)
{
for (int x = 0; x < width; x++)
{
//Something to detect what color the pixel is at position x, z
//and tell what gameobject to place depending on what color was chosen
//bool isWall = mazeImage.GetPixel(x, z).r < 0.5; //example code snippet from Cubiquity ColoredCubeMazeFromImage.cs
//mazeImage.GetPixel(x, z).r < 0.5;
Color32 currentPixel = mapImage.GetPixel(x, z);
Debug.Log("X:" + x + " Z:" + z + "/ Width:" + width + " Depth:" + depth + " / " + currentPixel);
// THIS IS THE LINE I NEED TO EDIT TO SHOW THE VALUES OF RGB FROM THE SCAN ABOVE
//System.IO.File.WriteAllText("G:/Save/RGB.txt", Color.red);
for (int y = height - 1; y > 0; y--)
{
//Nothing for now but possibly
//if color of x, z = thisColor
//Place thisGameObject 1 tile above thisColor
}
}
}
}
}
}
}
答案 0 :(得分:0)
您只打印常量。要从图像中获取像素,您必须使用GetPixel函数:
Color currentPixel = mapImage.GetPixel(x, z);
Debug.Log("X:" + x + " Z:" + z + "/ Width:" + width + " Depth:" + depth + " / " + currentPixel);
http://docs.unity3d.com/ScriptReference/Texture2D.GetPixel.html
只是为了澄清,你的代码不起作用的原因是你没有真正解决你的图像。如果你编写Color.red,Color.green等,你会得到常量,它存储特定颜色的rgb值。
http://docs.unity3d.com/ScriptReference/Color.html
正如您在ScriptReference中所看到的,这些颜色是静态定义的。它们与您的特定形象无关。
“+”符号的另一个注释。第一个代码不会生成您提供的日志。在统一颜色中有“+”运算符重载。这意味着您可以实际添加颜色(数学)。所以我希望在你的日志中看到颜色加在一起。您的日志不同的原因是因为您前面有一个字符串。代码从左到右执行。因此,颜色将首先转换为字符串,然后再添加。