public static void SetGradientToTexture(GradientField gradient, Texture2D targetTexture) { int width = targetTexture.width; Color[] colors = new Color[width]; for (int i = 0; i < width; i++) { float lerp = (float)i / (width - 1); colors[i] = gradient.Evaluate(lerp); } targetTexture.SetPixels(colors); targetTexture.Apply(); }
bool CheckMouse(Rect gradientArea) { bool needsUpdate = false; if (isDragging && selectedProperty != null) { var array = (editMode == EditMode.alpha ? alphaArrayProperty : colorArrayProperty); switch (Event.current.type) { case EventType.MouseDrag: float targetTime = Mathf.InverseLerp(gradientArea.xMin, gradientArea.xMax, Event.current.mousePosition.x); selectedProperty.FindPropertyRelative("time").floatValue = targetTime * 100; needsUpdate = true; float handleCenter = editMode == EditMode.alpha ? gradientArea.yMin - 9 : gradientArea.yMax + 9; isDraggingToRemove = Mathf.Abs(handleCenter - Event.current.mousePosition.y) > 20 && array.arraySize > 1; break; case EventType.MouseUp: if (isDragging) { DateTime time = DateTime.Now; if (lastMouseUp != null && (lastMouseUp - time).TotalMilliseconds < 500) { /* * Being unable to double click the selected handler and open the color picker */ } lastMouseUp = time; if (isDraggingToRemove) { DeleteSelectedItem(); isDraggingToRemove = false; needsUpdate = true; } isDragging = false; } break; } } gradientArea = ExpandRect(gradientArea, 0, -5); EditorGUIUtility.AddCursorRect(gradientArea, MouseCursor.Link); if (GUI.Button(gradientArea, GUIContent.none, GUIStyle.none)) //Invisible button for color picking { float targetTime = Mathf.InverseLerp(gradientArea.xMin, gradientArea.xMax, Event.current.mousePosition.x); var pickedColor = currentGradient.Evaluate(targetTime); if (editMode == EditMode.alpha) { selectedProperty.FindPropertyRelative("alpha").floatValue = pickedColor.a; } else { pickedColor.a = 1; selectedProperty.FindPropertyRelative("color").colorValue = pickedColor; } } return(needsUpdate); }