[英]Check for RGB in one command instead of color.red color.blue color.green
我有一个脚本,它逐像素检查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 ---代码现在可以正常工作--------------
这是整个代码
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
}
}
}
}
}
}
}
您仅打印常量。 要从图像中获取像素,必须使用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中所看到的,这些颜色是静态定义的。 它们与您的特定图像无关。
并注意“ +”符号。 第一个代码不会产生您提供的日志。 颜色统一使“ +”运算符重载。 这意味着您实际上可以(在数学上)添加颜色。 因此,我希望看到在您的日志中添加的颜色。 您的日志与众不同的原因是因为它前面有一个字符串。 该代码从左到右执行。 因此,颜色将首先转换为字符串,然后添加。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.