using Assets.Scripts; using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; public class PFUISlider : MonoBehaviour { private Text text; private Slider slider; public System.Func valueHandler = (a) => a * 100; private List colorGradientList; private void Awake() { text = transform.Find("Text").GetComponent(); slider = transform.GetComponent(); //text.text = $"{(slider.value * 100).ToString("#0")}"; //slider.onValueChanged.AddListener((f) => //{ // text.text = $"{valueHandler(f).ToString("#0")}"; //}); colorGradientList = SetColorGradient(new List { Utils.HexToColorHtml("#ffffff"), Utils.HexToColorHtml("#27DFE3"), Utils.HexToColorHtml("#27E363"), Utils.HexToColorHtml("#E3D427"), Utils.HexToColorHtml("#E33B27"), }, new List { 50,50,100,100 }); } public bool runCallback = false; public void SetValueChanged(System.Action a) { slider = transform.GetComponent(); slider.onValueChanged.RemoveAllListeners(); slider.onValueChanged.AddListener((f) => { var step = 1f / (colorGradientList.Count-1); var index = (int)Math.Round(f / step, 0); slider.targetGraphic.color = colorGradientList[index]; text.text = $"{valueHandler(f).ToString("#0")}"; a.Invoke(f); }); } public void SetValue(float a) { print("slider值" + a); slider.value = a; } public List SetColorGradient(List list, int divideCount) { var res = new List(); for (int i = 0; i < list.Count - 1; i++) { Color c = list[i], nc = list[i + 1]; var diff = new Tuple((nc.r - c.r) / divideCount, (nc.g - c.g) / divideCount, (nc.b - c.b) / divideCount); for (int j = 0; j < divideCount; j++) { res.Add(new Color(c.r + diff.Item1 * j, c.g + diff.Item2 * j, c.b + diff.Item3 * j)); } } return res; } public List SetColorGradient(List list, List divideList) { if (list.Count != divideList.Count+1) return null; var res = new List(); for (int i = 0; i < list.Count - 1; i++) { Color c = list[i], nc = list[i + 1]; var divideCount = divideList[i]; var diff = new Tuple((nc.r - c.r) / divideCount, (nc.g - c.g) / divideCount, (nc.b - c.b) / divideCount); for (int j = 0; j < divideCount; j++) { res.Add(new Color(c.r + diff.Item1 * j, c.g + diff.Item2 * j, c.b + diff.Item3 * j)); } } return res; } // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { } }