1
1
1kvin2016-04-11 05:03:54
C++ / C#
1kvin, 2016-04-11 05:03:54

[Unity3d] How to determine the color of where the mouse is?

How to determine the color where the mouse is located? 2D game using Sprites.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lertmind, 2016-04-11
@1kvin

If you change the example WaitForEndOfFrame , in which the screenshot is taken, then you can take the color of only one pixel:

using UnityEngine;
using System.Collections;

public class ColorClick : MonoBehaviour
{
    Texture2D tex;
    //public Renderer test;

    void Start()
    {
        tex = new Texture2D(1, 1, TextureFormat.RGB24, false);
    }

    IEnumerator ReadPixelColor()
    {
        // We should only read the screen buffer after rendering is complete
        yield return new WaitForEndOfFrame();

        float x = Input.mousePosition.x;
        float y = Input.mousePosition.y;

        // Read screen contents into the texture
        tex.ReadPixels(new Rect(x, y, 1, 1), 0, 0);
        tex.Apply();

        Color color = tex.GetPixel(0, 0);
        //test.material.color = tex.GetPixel(0, 0);
        //Debug.Log(color);
    }

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            StartCoroutine(ReadPixelColor());
        }
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question