164 lines
5.7 KiB
C#
164 lines
5.7 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace Assets.Scenes.Ride.Scripts
|
|
{
|
|
[RequireComponent(typeof(Image))]
|
|
public class GradientBackGround : MonoBehaviour
|
|
{
|
|
public enum GradientType
|
|
{
|
|
None,
|
|
Vertical,
|
|
VerticalDown,
|
|
Horizontal,
|
|
Diagonal
|
|
}
|
|
|
|
[System.Serializable]
|
|
public class GradientColorNode
|
|
{
|
|
[Range(0, 1)]
|
|
public float ratio = 0;
|
|
public Color color = Color.white;
|
|
|
|
public GradientColorNode(float _ratio, Color _color)
|
|
{
|
|
ratio = _ratio;
|
|
color = _color;
|
|
}
|
|
}
|
|
|
|
private Image bgImage;
|
|
|
|
[SerializeField, Header("渐变方式")]
|
|
private GradientType gradientType = GradientType.Vertical;
|
|
|
|
[SerializeField, Header("渐变曲线(0~1)")]
|
|
private AnimationCurve gradientCurve = new AnimationCurve(new Keyframe(0, 0), new Keyframe(1, 1));
|
|
|
|
[SerializeField, Header("渐变颜色")]
|
|
private List<GradientColorNode> gradientColorNodeList = new List<GradientColorNode>() { new GradientColorNode(0, Color.white), new GradientColorNode(1, Color.black) };
|
|
|
|
private void Awake()
|
|
{
|
|
bgImage = GetComponent<Image>();
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
SetGradientColor();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
//if (Input.GetKeyDown(KeyCode.Alpha1))
|
|
//{
|
|
// gradientType = GradientType.None;
|
|
// SetGradientColor();
|
|
//}
|
|
//if (Input.GetKeyDown(KeyCode.Alpha2))
|
|
//{
|
|
// gradientType = GradientType.Vertical;
|
|
// SetGradientColor();
|
|
//}
|
|
//if (Input.GetKeyDown(KeyCode.Alpha3))
|
|
//{
|
|
// gradientType = GradientType.Horizontal;
|
|
// SetGradientColor();
|
|
//}
|
|
//if (Input.GetKeyDown(KeyCode.Alpha4))
|
|
//{
|
|
// gradientType = GradientType.Diagonal;
|
|
// SetGradientColor();
|
|
//}
|
|
}
|
|
|
|
private void SetGradientColor()
|
|
{
|
|
//创建Texture2D
|
|
Vector2Int imageSize = new Vector2Int(Screen.width, Screen.height);
|
|
Texture2D texture2D = new Texture2D(imageSize.x, imageSize.y);
|
|
//遍历像素点
|
|
switch (gradientType)
|
|
{
|
|
case GradientType.Vertical:
|
|
for (int y = 0; y < imageSize.y; y++)
|
|
{
|
|
Color pixelColor = GetColorByCurve(1.0f * y / imageSize.y);
|
|
for (int x = 0; x < imageSize.x; x++)
|
|
{
|
|
texture2D.SetPixel(x, y, pixelColor);
|
|
}
|
|
}
|
|
break;
|
|
case GradientType.VerticalDown:
|
|
for (int y = imageSize.y - 1; y > 0; y--)
|
|
{
|
|
Color pixelColor = GetColorByCurve(1.0f * y / imageSize.y);
|
|
for (int x = imageSize.x - 1; x > 0; x--)
|
|
{
|
|
texture2D.SetPixel(x, y, pixelColor);
|
|
}
|
|
}
|
|
break;
|
|
case GradientType.Horizontal:
|
|
for (int x = 0; x < imageSize.x; x++)
|
|
{
|
|
Color pixelColor = GetColorByCurve(1.0f * x / imageSize.x);
|
|
for (int y = 0; y < imageSize.y; y++)
|
|
{
|
|
texture2D.SetPixel(x, y, pixelColor);
|
|
}
|
|
}
|
|
break;
|
|
case GradientType.Diagonal:
|
|
for (int x = 0; x < imageSize.x; x++)
|
|
{
|
|
for (int y = 0; y < imageSize.y; y++)
|
|
{
|
|
Color pixelColor = GetColorByCurve(0.5f * x / imageSize.x + 0.5f * y / imageSize.y);
|
|
texture2D.SetPixel(x, y, pixelColor);
|
|
}
|
|
}
|
|
break;
|
|
default:
|
|
if (gradientColorNodeList.Count > 0)
|
|
{
|
|
Color pixelColor = gradientColorNodeList[0].color;
|
|
for (int x = 0; x < imageSize.x; x++)
|
|
{
|
|
for (int y = 0; y < imageSize.y; y++)
|
|
{
|
|
texture2D.SetPixel(x, y, pixelColor);
|
|
}
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
texture2D.Apply();
|
|
//创建Sprite
|
|
Sprite sprite = Sprite.Create(texture2D, new Rect(0, 0, imageSize.x, imageSize.y), new Vector2(0.5f, 0.5f));
|
|
sprite.name = "GradientBg";
|
|
if (bgImage != null)
|
|
bgImage.sprite = sprite;
|
|
}
|
|
|
|
private Color GetColorByCurve(float ratio)
|
|
{
|
|
for (int i = 1; i < gradientColorNodeList.Count; i++)
|
|
{
|
|
if (ratio < gradientColorNodeList[i].ratio)
|
|
{
|
|
float curveValue = (ratio - gradientColorNodeList[i - 1].ratio) / (gradientColorNodeList[i].ratio - gradientColorNodeList[i - 1].ratio);
|
|
curveValue = gradientCurve.Evaluate(curveValue);
|
|
return Color.Lerp(gradientColorNodeList[i - 1].color, gradientColorNodeList[i].color, curveValue);
|
|
}
|
|
}
|
|
return Color.white;
|
|
}
|
|
|
|
}
|
|
}
|