2022-03-10 18:10:34 +08:00
|
|
|
|
using Assets.Scripts;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections;
|
2021-04-21 11:19:31 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
using UnityEngine.UI;
|
2022-03-10 18:10:34 +08:00
|
|
|
|
using UnityEngine.EventSystems;
|
2021-04-21 11:19:31 +08:00
|
|
|
|
|
|
|
|
|
|
public class PFUISlider : MonoBehaviour
|
|
|
|
|
|
{
|
|
|
|
|
|
private Text text;
|
|
|
|
|
|
private Slider slider;
|
2022-03-10 18:10:34 +08:00
|
|
|
|
public System.Func<float, float> valueHandler = (a) => a * 100;
|
|
|
|
|
|
private List<Color> colorGradientList;
|
2021-04-21 11:19:31 +08:00
|
|
|
|
private void Awake()
|
|
|
|
|
|
{
|
|
|
|
|
|
text = transform.Find("Text").GetComponent<Text>();
|
|
|
|
|
|
slider = transform.GetComponent<Slider>();
|
2022-03-10 18:10:34 +08:00
|
|
|
|
//text.text = $"{(slider.value * 100).ToString("#0")}";
|
|
|
|
|
|
//slider.onValueChanged.AddListener((f) =>
|
|
|
|
|
|
//{
|
|
|
|
|
|
// text.text = $"{valueHandler(f).ToString("#0")}";
|
|
|
|
|
|
//});
|
|
|
|
|
|
colorGradientList = SetColorGradient(new List<Color>
|
|
|
|
|
|
{
|
|
|
|
|
|
Utils.HexToColorHtml("#ffffff"),
|
|
|
|
|
|
Utils.HexToColorHtml("#27DFE3"),
|
|
|
|
|
|
Utils.HexToColorHtml("#27E363"),
|
|
|
|
|
|
Utils.HexToColorHtml("#E3D427"),
|
|
|
|
|
|
Utils.HexToColorHtml("#E33B27"),
|
|
|
|
|
|
},
|
|
|
|
|
|
new List<int>
|
2021-04-21 11:19:31 +08:00
|
|
|
|
{
|
2022-03-10 18:10:34 +08:00
|
|
|
|
50,50,100,100
|
2021-04-21 11:19:31 +08:00
|
|
|
|
});
|
|
|
|
|
|
}
|
2022-03-10 18:10:34 +08:00
|
|
|
|
public bool runCallback = false;
|
2022-02-10 12:57:43 +08:00
|
|
|
|
public void SetValueChanged(System.Action<float> a)
|
|
|
|
|
|
{
|
|
|
|
|
|
slider = transform.GetComponent<Slider>();
|
|
|
|
|
|
slider.onValueChanged.RemoveAllListeners();
|
|
|
|
|
|
slider.onValueChanged.AddListener((f) =>
|
|
|
|
|
|
{
|
2022-03-10 18:10:34 +08:00
|
|
|
|
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")}";
|
2022-02-10 12:57:43 +08:00
|
|
|
|
a.Invoke(f);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2022-03-10 18:10:34 +08:00
|
|
|
|
public void SetValue(float a)
|
|
|
|
|
|
{
|
|
|
|
|
|
slider.value = a;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public List<Color> SetColorGradient(List<Color> list, int divideCount)
|
|
|
|
|
|
{
|
|
|
|
|
|
var res = new List<Color>();
|
|
|
|
|
|
for (int i = 0; i < list.Count - 1; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
Color c = list[i], nc = list[i + 1];
|
|
|
|
|
|
var diff = new Tuple<float, float, float>((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<Color> SetColorGradient(List<Color> list, List<int> divideList)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (list.Count != divideList.Count+1) return null;
|
|
|
|
|
|
var res = new List<Color>();
|
|
|
|
|
|
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<float, float, float>((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;
|
|
|
|
|
|
}
|
2021-04-21 11:19:31 +08:00
|
|
|
|
// Start is called before the first frame update
|
|
|
|
|
|
void Start()
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Update is called once per frame
|
|
|
|
|
|
void Update()
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|