[英]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.