【问题标题】:Is there a better way to create a color picker in Unity C# and NGUI?有没有更好的方法在 Unity C# 和 NGUI 中创建颜色选择器?
【发布时间】:2016-08-23 14:37:07
【问题描述】:

我正在 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 系统,是否有更好的方法来创建颜色选择器,或者使用更少内存的方法?

【问题讨论】:

  • 为什么不使用现成的解决方案? found here
  • 我查看了许多现有的解决方案。部分问题是NGUI是否存在,它们似乎不可用我没有使用Unity UI ..
  • 对对对,对此感到抱歉。
  • 最新版本的 Unity UI 很不错,但它仍然有一些我真的不喜欢它的问题,尤其是强制整个 UI 到世界中心等等。
  • 在使用新的 Unity UI 时,我只是在右上角打开和关闭 UI 层可见性。

标签: c# unity3d


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-22
    • 2019-04-08
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多