简体   繁体   中英

Check for RGB in one command instead of color.red color.blue color.green

I have a script that's checking RGB values of a PNG file pixel by pixel

what I'm trying to figure out is instead of using this code (Bad Code)

Debug.Log(Color.red + Color.green + Color.blue);

this code returns this to the log

 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)

Fixed Code

Color32 currentPixel = mapImage.GetPixel(x, z);
Debug.Log(currentPixel);

as you can see its already checking rgb each time but only looking for the red, green and blue from the codes Color.red and so on commands

can you do the same thing but have the debug.log return

RGBA(255, 255, 255, 255)

EDIT2---Code Now Works-------------

this is the entire code

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
                        }
                    }
                }
            }
        }
    }
}

You are only printing constants. To get the pixel from your image you have to use the GetPixel function:

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

Just to clarify, the reason your code did not work is that you where not actually addressing your image. If you write Color.red, Color.green etc. you get constants, which store rgb values for a specific color.

http://docs.unity3d.com/ScriptReference/Color.html

As you can see in the ScriptReference, these colors are statically defined. They have nothing to do with your particular image.

And another note to the "+" symbol. The first code does not produce the log you provided. In unity Colors have the "+" operator overloaded. This means you can actually add Colors together(mathematically). So i expected to see the colors added together in your log. The reason your log is different is because you had a string in front of that. The code gets executed from left to right. So the colors will be converted to strings first and then get added.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM