繁体   English   中英

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

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

我正在 Unity 中创建一些艺术程序/游戏,但是我需要为在其中构建东西的艺术家创建一个颜色选择器。 我正在使用 NGUI UI 系统,并创建了一个带有一组颜色的基本精灵,以及自定义颜色输入。

然而,事实证明,要准确地选择其中一种颜色并不容易。 我目前正在这样做:

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

但是,必须有更好的方法来捕获 - 只是 - 特定区域。 但是,这确实可以准确地获得颜色。 但它也会生成一个图像——即使是临时的——在它从缓冲区中清除之前大小在 2 到 8 MB 之间。 给定特定的 UI 系统,是否有更好的方法来创建颜色选择器,或者使用更少内存的方法?

一般来说,我会优化一些东西,比如避免分配/读取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