using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; using System.Collections.Generic; public class ColorPicktest : MonoBehaviour, IPointerDownHandler, IDragHandler { public Slider hueSlider; public Slider alphaSlider; // Alpha slider to control transparency public Image saturationValueImage; public InputField hexInput; public Image colorPreview; public RectTransform saturationValueRect; // RectTransform for the Saturation/Value box //public Button[] presetColorButtons; // Buttons for preset colors public GameObject PresetContainer; public Button ButtonTemplate; private Texture2D saturationValueTexture; private Color currentColor = Color.white; void OnEnable() { // Initialize saturation/value texture with 200x200 resolution saturationValueTexture = new Texture2D(200, 200); saturationValueImage.sprite = Sprite.Create(saturationValueTexture, new Rect(0, 0, 200, 200), new Vector2(0.5f, 0.5f)); hueSlider.onValueChanged.AddListener(OnHueChanged); alphaSlider.onValueChanged.AddListener(OnAlphaChanged); // Add listener for the alpha slider hexInput.onEndEdit.AddListener(OnHexInputChanged); // Generate the initial saturation/value image based on the hue OnHueChanged(hueSlider.value); // Assign preset color buttons if(PresetContainer!=null && PresetContainer.transform.childCount==0){ List HexColor=GetListColor(); for(int i=0; i().color=presetColor; Btn.onClick.AddListener(() => SetPresetColor(presetColor)); } } print("Picker NAme==="+transform.name); // foreach (Button presetButton in presetColorButtons) // { // Color presetColor = presetButton.GetComponent().color; // presetButton.onClick.AddListener(() => SetPresetColor(presetColor)); // } } void OnDisable(){ DOIT.DELETEAllObjectChild(PresetContainer); } void OnHueChanged(float value) { // Generate the saturation/value gradient for the selected hue UpdateSaturationValueImage(value); } void UpdateSaturationValueImage(float hue) { // Update the saturation/value texture to match the given hue for (int x = 0; x < saturationValueTexture.width; x++) { for (int y = 0; y < saturationValueTexture.height; y++) { float saturation = x / (float)saturationValueTexture.width; float brightness = y / (float)saturationValueTexture.height; saturationValueTexture.SetPixel(x, y, Color.HSVToRGB(hue, saturation, brightness)); } } saturationValueTexture.Apply(); } public void OnPointerDown(PointerEventData eventData) { UpdateSaturationValue(eventData); } public void OnDrag(PointerEventData eventData) { UpdateSaturationValue(eventData); } private void UpdateSaturationValue(PointerEventData eventData) { // Convert screen position to local position within the Saturation/Value box Vector2 localPoint; RectTransformUtility.ScreenPointToLocalPointInRectangle(saturationValueRect, eventData.position, eventData.pressEventCamera, out localPoint); // Normalize the local position to get saturation and brightness values, taking into account the actual size (200x200) float saturation = Mathf.Clamp01((localPoint.x - saturationValueRect.rect.x) / 200f); float brightness = Mathf.Clamp01((localPoint.y - saturationValueRect.rect.y) / 200f); // Update the color based on the new saturation/brightness OnSaturationValueChanged(new Vector2(saturation, brightness)); } public void OnSaturationValueChanged(Vector2 value) { // Update the color based on the position in the saturation/value box float saturation = value.x; float brightness = value.y; // Calculate RGB based on hue, saturation, and brightness, then apply alpha Color hueColor = Color.HSVToRGB(hueSlider.value, saturation, brightness); currentColor = new Color(hueColor.r, hueColor.g, hueColor.b, alphaSlider.value); // Include alpha from the slider // Update color preview and hex field colorPreview.color = currentColor; UpdateHexField(); } void OnAlphaChanged(float value) { // Update the alpha value for the current color currentColor.a = value; colorPreview.color = currentColor; UpdateHexField(); } void OnHexInputChanged(string hex) { if (ColorUtility.TryParseHtmlString(hex, out Color newColor)) { currentColor = newColor; colorPreview.color = currentColor; // Update sliders based on the new color Color.RGBToHSV(currentColor, out float h, out float s, out float v); hueSlider.value = h; alphaSlider.value = currentColor.a; // Sync the alpha slider with the new color's alpha OnHueChanged(h); } } void UpdateHexField() { // Update the hex field to reflect the current color (including alpha as RGBA) hexInput.text = "#" + ColorUtility.ToHtmlStringRGBA(currentColor); } public void SetPresetColor(Color presetColor) { // Set the current color to the preset color currentColor = presetColor; // Convert presetColor to HSV to update the hue, saturation, and value sliders Color.RGBToHSV(presetColor, out float h, out float s, out float v); // Update the hue slider and saturation/value image to match the preset color's hue hueSlider.value = h; UpdateSaturationValueImage(h); // Ensure saturation/value image matches the preset hue // Set saturation and brightness directly OnSaturationValueChanged(new Vector2(s, v)); // Update the alpha slider and the preview alphaSlider.value = presetColor.a; colorPreview.color = currentColor; // Update the hex field to reflect the preset color UpdateHexField(); } public List GetListColor(){ List HexColor=new (); print("Transform Name==="+transform.name); if (transform.name.IndexOf("CabTexture") != -1) { HexColor = new(){ "#ffffff","#CCCCCC","#878585","#545150","#E7DDBF","#8F7B5D","#536674","#B8C0B7","#7C877C", "#D4BA91","#95745E","#905037","#BE6A50","#98352A","#735540","#737580","#638068","#58929A" }; } if(transform.name.IndexOf("ColorPickerSoft")!=-1) { HexColor=new(){ "FAFF09","#FF6408","#878585","#FF0815","FF08DC","1D08FF","088AFF","08FF66","53FF08", }; } if (transform.name == "ColorPickerTexture" || transform.name == "ColorPickerGrouth") { HexColor = new(){ "#F5F0EB","#EEE6DB", "#E9DED2","#DDCBB5", "#D4BBA3","#D1AF97","#D4B187","#C39B68","#B99161", "#D4A77B","#C39B73","#BC8D66","#BC9266","#C08551","#AD774F","#A46735","#98683D","#90541B", "#985D2E","#7D4319", "#6A360C","#431C00","#A16151","#B47564","#763333","#8E4646", "#951D1D", "#84A1D9","#ECB5D4","#E7A7A7","#D9A6EC","#9BB6C0", "#AB9A9A","#C4C89F","#D0D990", "#7DA87F", }; } return HexColor; } }