简体   繁体   English

有没有更好的方法在 Unity C# 和 NGUI 中创建颜色选择器?

[英]Is there a better way to create a color picker in Unity C# and NGUI?

I am creating a bit of an art program / game in Unity, however I need to create a color picker for the artists who are building things inside it.我正在 Unity 中创建一些艺术程序/游戏,但是我需要为在其中构建东西的艺术家创建一个颜色选择器。 I am using the NGUI UI system, and I've created a basic sprite with a set of colors on it, along with a custom color input.我正在使用 NGUI UI 系统,并创建了一个带有一组颜色的基本精灵,以及自定义颜色输入。

However, actually selecting one of those colors accurately is not proving easy.然而,事实证明,要准确地选择其中一种颜色并不容易。 I'm currently doing it like this:我目前正在这样做:

IEnumerator CaptureTempArea()
{
    yield return new WaitForEndOfFrame();

    tex = new Texture2D(Screen.width, Screen.height);
    tex.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
    tex.Apply();


    Vector2 pos = UICamera.lastEventPosition;

    int x = (int)pos.x;

    int y = (int)pos.y;

    Color color = tex.GetPixel(x, y);

    Debug.Log(ColorToHex(color));

    Debug.Log(tex.GetRawTextureData().Length / 1000);

    Destroy(tex);
    tex = null;
}

However, there must be a better way to capture -just- the specific area.但是,必须有更好的方法来捕获 - 只是 - 特定区域。 This does accurately get the colors, however.但是,这确实可以准确地获得颜色。 But it also generates an image - even temporary - that is anywhere from 2 to 8 MB in size before it gets cleared from the buffer.但它也会生成一个图像——即使是临时的——在它从缓冲区中清除之前大小在 2 到 8 MB 之间。 Given the specific UI system, is there a better way to create a color picker, or one that will use less memory?给定特定的 UI 系统,是否有更好的方法来创建颜色选择器,或者使用更少内存的方法?

in general, I would optimize some stuff, like avoiding allocating/reading the while texture just to obtain a single pixel, and keeping a live 1x1 texture throughout the entire session... if your device support it you can also use computebuffer to pass the color value from a shader... this is how your code should look like (I haven't tested it, just rewritten your code a bit)一般来说,我会优化一些东西,比如避免分配/读取while纹理只是为了获得一个像素,并在整个会话中保持一个实时的1x1纹理......如果你的设备支持它,你也可以使用computebuffer来传递来自着色器的颜色值......这就是你的代码应该是什么样子(我没有测试它,只是重写了你的代码)

Texture2D tex;

void Start() {
    tex = new Texture2D(1, 1);
    //get the color printed by calling:
    StartCoroutine(CaptureTempArea());
}

IEnumerator CaptureTempArea() {
    yield return new WaitForEndOfFrame();
    Vector2 pos = UICamera.lastEventPosition;
    tex.ReadPixels(new Rect(pos.x, pos.y, 1, 1), 0, 0);
    tex.Apply();
    Color color = tex.GetPixel(0, 0);
    Debug.Log(ColorToHex(color));
}

void OnDestroy() {
    Destroy(tex);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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