Skip to content

Commit dab8021

Browse files
committed
alpha modifier
1 parent a18333a commit dab8021

25 files changed

+508
-0
lines changed

Assets/CatCode.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/CatCode/AlphaModifiers.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/CatCode/AlphaModifiers/AlphaModifierStrategies.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using UnityEngine;
2+
3+
namespace CatCode.AlphaModifiers
4+
{
5+
public sealed class CanvasGroupAlphaModifierStrategy : IAlphaModifierStrategy
6+
{
7+
[SerializeField] private CanvasGroup _canvasGroup;
8+
[SerializeField] private float _alpha;
9+
10+
public CanvasGroupAlphaModifierStrategy(CanvasGroup canvasGroup)
11+
{
12+
_canvasGroup = canvasGroup;
13+
_alpha = _canvasGroup.alpha;
14+
}
15+
16+
public void SetAlpha(float value)
17+
{
18+
var alpha = Mathf.Clamp01(_alpha * value);
19+
_canvasGroup.alpha = alpha;
20+
}
21+
}
22+
}

Assets/CatCode/AlphaModifiers/AlphaModifierStrategies/CanvasGroupAlphaModifierStrategy.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using UnityEngine;
2+
3+
namespace CatCode.AlphaModifiers
4+
{
5+
public static class GradientUtils
6+
{
7+
public static void UpdateAlphaKeys(Gradient gradient, GradientAlphaKey[] defaultAlphaKeys, float alpha)
8+
{
9+
var alphaKeys = gradient.alphaKeys;
10+
var length = Mathf.Min(alphaKeys.Length, defaultAlphaKeys.Length);
11+
12+
for (int i = 0; i < length; i++)
13+
{
14+
ref var alphaKey = ref alphaKeys[i];
15+
alphaKey.alpha = defaultAlphaKeys[i].alpha * alpha;
16+
}
17+
18+
gradient.alphaKeys = alphaKeys;
19+
}
20+
}
21+
}

Assets/CatCode/AlphaModifiers/AlphaModifierStrategies/GradientUtils.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
using UnityEngine;
3+
using UnityEngine.UI;
4+
5+
namespace CatCode.AlphaModifiers
6+
{
7+
[Serializable]
8+
public class GraphicAlphaModifierStrategy : IAlphaModifierStrategy
9+
{
10+
[Serializable]
11+
public struct AlphaGraphicData
12+
{
13+
public float alpha;
14+
public Graphic graphic;
15+
16+
17+
public AlphaGraphicData(float alpha, Graphic graphic)
18+
{
19+
this.alpha = alpha;
20+
this.graphic = graphic;
21+
}
22+
}
23+
24+
25+
[SerializeField] private AlphaGraphicData[] _graphics;
26+
27+
public GraphicAlphaModifierStrategy(Graphic graphic) : this(new Graphic[] { graphic })
28+
{ }
29+
30+
public GraphicAlphaModifierStrategy(Graphic[] graphics)
31+
{
32+
_graphics = new AlphaGraphicData[graphics.Length];
33+
for (int i = 0; i < _graphics.Length; i++)
34+
_graphics[i] = new AlphaGraphicData(graphics[i].color.a, graphics[i]);
35+
}
36+
37+
public void Initialize()
38+
{
39+
}
40+
41+
public void SetAlpha(float value)
42+
{
43+
for (int i = 0; i < _graphics.Length; i++)
44+
{
45+
var graphicData = _graphics[i];
46+
var alpha = Mathf.Clamp(graphicData.alpha * value, 0f, 1f);
47+
48+
var color = graphicData.graphic.color;
49+
color.a = alpha;
50+
graphicData.graphic.color = color;
51+
}
52+
}
53+
}
54+
}

Assets/CatCode/AlphaModifiers/AlphaModifierStrategies/GraphicAlphaModifierStrategy.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace CatCode.AlphaModifiers
2+
{
3+
public interface IAlphaModifierStrategy
4+
{
5+
void SetAlpha(float value);
6+
}
7+
}

0 commit comments

Comments
 (0)