mirror of
https://github.com/mob-sakai/UIEffect.git
synced 2026-11-19 17:22:37 +08:00
feat: add UIEffectTweener component
`UIEffectTweener` component animates `UIEffect` intensity or rate, enabling easy control over effect animations without the need for an `AnimationClip`.
This commit is contained in:
@@ -0,0 +1,149 @@
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Profiling;
|
||||
|
||||
namespace Coffee.UIEffects.Editors
|
||||
{
|
||||
[CanEditMultipleObjects]
|
||||
[CustomEditor(typeof(UIEffectTweener))]
|
||||
internal class UIMaterialPropertyTweenerEditor : Editor
|
||||
{
|
||||
private SerializedProperty _cullingMask;
|
||||
private SerializedProperty _direction;
|
||||
private SerializedProperty _curve;
|
||||
private SerializedProperty _delay;
|
||||
private SerializedProperty _duration;
|
||||
private SerializedProperty _interval;
|
||||
private SerializedProperty _restartOnEnable;
|
||||
private SerializedProperty _updateMode;
|
||||
private SerializedProperty _wrapMode;
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
_cullingMask = serializedObject.FindProperty("m_CullingMask");
|
||||
_direction = serializedObject.FindProperty("m_Direction");
|
||||
_curve = serializedObject.FindProperty("m_Curve");
|
||||
_restartOnEnable = serializedObject.FindProperty("m_RestartOnEnable");
|
||||
_delay = serializedObject.FindProperty("m_Delay");
|
||||
_duration = serializedObject.FindProperty("m_Duration");
|
||||
_interval = serializedObject.FindProperty("m_Interval");
|
||||
_wrapMode = serializedObject.FindProperty("m_WrapMode");
|
||||
_updateMode = serializedObject.FindProperty("m_UpdateMode");
|
||||
}
|
||||
|
||||
public override void OnInspectorGUI()
|
||||
{
|
||||
Profiler.BeginSample("(MPI)[MPTweenerEditor] OnInspectorGUI");
|
||||
serializedObject.UpdateIfRequiredOrScript();
|
||||
EditorGUILayout.PropertyField(_cullingMask);
|
||||
EditorGUILayout.PropertyField(_direction);
|
||||
EditorGUILayout.PropertyField(_curve);
|
||||
EditorGUILayout.PropertyField(_delay);
|
||||
EditorGUILayout.PropertyField(_duration);
|
||||
EditorGUILayout.PropertyField(_interval);
|
||||
EditorGUILayout.PropertyField(_restartOnEnable);
|
||||
EditorGUILayout.PropertyField(_wrapMode);
|
||||
EditorGUILayout.PropertyField(_updateMode);
|
||||
serializedObject.ApplyModifiedProperties();
|
||||
DrawPlayer(target as UIEffectTweener);
|
||||
Profiler.EndSample();
|
||||
}
|
||||
|
||||
private void DrawPlayer(UIEffectTweener tweener)
|
||||
{
|
||||
if (!tweener) return;
|
||||
|
||||
EditorGUILayout.BeginHorizontal();
|
||||
EditorGUI.BeginDisabledGroup(!Application.isPlaying);
|
||||
var icon = EditorGUIUtility.IconContent("icons/playbutton.png");
|
||||
var r = EditorGUILayout.GetControlRect(false);
|
||||
|
||||
var rButton = new Rect(r.x, r.y, 20, r.height);
|
||||
if (GUI.Button(rButton, icon, "IconButton"))
|
||||
{
|
||||
tweener.SetTime(0);
|
||||
}
|
||||
|
||||
EditorGUI.EndDisabledGroup();
|
||||
|
||||
var totalTime = tweener.totalTime;
|
||||
var time = tweener.time;
|
||||
var label = EditorGUIUtility.TrTempContent($"{time:N2}/{totalTime:N2}");
|
||||
var wLabel = Mathf.CeilToInt(EditorStyles.label.CalcSize(label).x / 5f) * 5f;
|
||||
wLabel = 80;
|
||||
var rLabel = new Rect(r.x + r.width - wLabel, r.y, wLabel, r.height);
|
||||
GUI.Label(rLabel, label, "RightLabel");
|
||||
EditorGUILayout.EndHorizontal();
|
||||
|
||||
EditorGUI.BeginChangeCheck();
|
||||
var rSlider = new Rect(r.x + 20, r.y, r.width - wLabel - 20, r.height);
|
||||
|
||||
//
|
||||
|
||||
var
|
||||
r0 = rSlider; //new Rect(rSlider.x, rSlider.y, rSlider.width * tweener.interval / totalTime, rSlider.height);
|
||||
r0.y += 4;
|
||||
r0.height -= 8;
|
||||
r0.width = rSlider.width * tweener.delay / totalTime;
|
||||
GUI.color = Color.blue;
|
||||
GUI.Label(r0, GUIContent.none, "TE DefaultTime");
|
||||
|
||||
r0.x += r0.width;
|
||||
r0.width = rSlider.width * tweener.duration / totalTime;
|
||||
GUI.color = Color.green;
|
||||
GUI.Label(r0, GUIContent.none, "TE DefaultTime");
|
||||
|
||||
r0.x += r0.width;
|
||||
r0.width = rSlider.width * tweener.interval / totalTime;
|
||||
GUI.color = Color.red;
|
||||
GUI.Label(r0, GUIContent.none, "TE DefaultTime");
|
||||
|
||||
if (UIEffectTweener.WrapMode.PingPongOnce <= tweener.wrapMode)
|
||||
{
|
||||
r0.x += r0.width;
|
||||
r0.width = rSlider.width * tweener.duration / totalTime;
|
||||
GUI.color = Color.green;
|
||||
GUI.Label(r0, GUIContent.none, "TE DefaultTime");
|
||||
}
|
||||
|
||||
if (UIEffectTweener.WrapMode.PingPongLoop <= tweener.wrapMode)
|
||||
{
|
||||
r0.x += r0.width;
|
||||
r0.width = rSlider.width * tweener.interval / totalTime;
|
||||
GUI.color = Color.red;
|
||||
GUI.Label(r0, GUIContent.none, "TE DefaultTime");
|
||||
}
|
||||
|
||||
|
||||
GUI.color = Color.white;
|
||||
|
||||
time = GUI.HorizontalSlider(rSlider, time, 0, totalTime);
|
||||
if (EditorGUI.EndChangeCheck())
|
||||
{
|
||||
tweener.SetTime(time);
|
||||
}
|
||||
|
||||
if (Application.isPlaying && tweener.isActiveAndEnabled)
|
||||
{
|
||||
Repaint();
|
||||
}
|
||||
}
|
||||
|
||||
// private static void PostAddElement(SerializedProperty prop, string propertyName)
|
||||
// {
|
||||
// prop.FindPropertyRelative("m_From.m_Type").intValue = -1;
|
||||
// prop.FindPropertyRelative("m_From.m_PropertyName").stringValue = propertyName;
|
||||
// prop.FindPropertyRelative("m_To.m_Type").intValue = -1;
|
||||
// prop.FindPropertyRelative("m_To.m_PropertyName").stringValue = propertyName;
|
||||
// }
|
||||
//
|
||||
// private void ResetCallback()
|
||||
// {
|
||||
// var current = serializedObject.targetObject as UIEffectTweener;
|
||||
// if (!current) return;
|
||||
//
|
||||
// Undo.RecordObject(current, "Reset Values");
|
||||
// current.ResetPropertiesToDefault();
|
||||
// }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 27bd0b04cc3fb4d338bb2ffc5f9d3e7c
|
||||
timeCreated: 1487152293
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,280 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Coffee.UIEffects
|
||||
{
|
||||
[RequireComponent(typeof(UIEffectBase))]
|
||||
public class UIEffectTweener : MonoBehaviour
|
||||
{
|
||||
[Flags]
|
||||
public enum CullingMask
|
||||
{
|
||||
Tone = 1 << 0,
|
||||
Color = 1 << 1,
|
||||
Sampling = 1 << 2,
|
||||
Transition = 1 << 3
|
||||
}
|
||||
|
||||
public enum UpdateMode
|
||||
{
|
||||
Normal,
|
||||
Unscaled,
|
||||
Manual
|
||||
}
|
||||
|
||||
public enum WrapMode
|
||||
{
|
||||
Once,
|
||||
Loop,
|
||||
PingPongOnce,
|
||||
PingPongLoop
|
||||
}
|
||||
|
||||
public enum Direction
|
||||
{
|
||||
Forward,
|
||||
Reverse
|
||||
}
|
||||
|
||||
[Tooltip("The culling mask of the tween.")]
|
||||
[SerializeField]
|
||||
private CullingMask m_CullingMask =
|
||||
CullingMask.Tone | CullingMask.Color | CullingMask.Sampling | CullingMask.Transition;
|
||||
|
||||
[Tooltip("The direction of the tween.")]
|
||||
[SerializeField]
|
||||
private Direction m_Direction = Direction.Forward;
|
||||
|
||||
[Tooltip("The curve to tween the properties.")]
|
||||
[SerializeField]
|
||||
private AnimationCurve m_Curve = AnimationCurve.Linear(0, 0, 1, 1);
|
||||
|
||||
[Tooltip("The delay in seconds before the tween starts.")]
|
||||
[SerializeField]
|
||||
[Range(0f, 10)]
|
||||
private float m_Delay;
|
||||
|
||||
[Tooltip("The duration in seconds of the tween.")]
|
||||
[SerializeField]
|
||||
[Range(0.05f, 10)]
|
||||
private float m_Duration = 1;
|
||||
|
||||
[Tooltip("The interval in seconds between each loop.")]
|
||||
[SerializeField]
|
||||
[Range(0f, 10)]
|
||||
private float m_Interval;
|
||||
|
||||
[Tooltip("Whether to restart the tween when enabled.")]
|
||||
[SerializeField]
|
||||
private bool m_RestartOnEnable = true;
|
||||
|
||||
[Tooltip("The wrap mode of the tween.\n" +
|
||||
" Clamp: Clamp the tween value (not loop).\n" +
|
||||
" Loop: Loop the tween value.\n" +
|
||||
" PingPongOnce: PingPong the tween value (not loop).\n" +
|
||||
" PingPong: PingPong the tween value.")]
|
||||
[SerializeField]
|
||||
private WrapMode m_WrapMode = WrapMode.Loop;
|
||||
|
||||
[Tooltip("Specifies how to get delta time.\n" +
|
||||
" Normal: Use `Time.deltaTime`.\n" +
|
||||
" Unscaled: Use `Time.unscaledDeltaTime`.\n" +
|
||||
" Manual: Not updated automatically and update manually with `UpdateTime` or `SetTime` method.")]
|
||||
[SerializeField]
|
||||
private UpdateMode m_UpdateMode = UpdateMode.Normal;
|
||||
|
||||
private float _rate;
|
||||
private float _time;
|
||||
private UIEffectBase _target;
|
||||
|
||||
/// <summary>
|
||||
/// The target UIMaterialPropertyInjector to tween.
|
||||
/// </summary>
|
||||
private UIEffectBase target => _target ? _target : _target = GetComponent<UIEffectBase>();
|
||||
|
||||
public CullingMask cullingMask
|
||||
{
|
||||
get => m_CullingMask;
|
||||
set => m_CullingMask = value;
|
||||
}
|
||||
|
||||
public Direction direction
|
||||
{
|
||||
get => m_Direction;
|
||||
set => m_Direction = value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The rate of the tween.
|
||||
/// </summary>
|
||||
public float rate
|
||||
{
|
||||
get => _rate;
|
||||
set
|
||||
{
|
||||
value = Mathf.Clamp01(value);
|
||||
if (Mathf.Approximately(_rate, value)) return;
|
||||
|
||||
_rate = value;
|
||||
|
||||
if (!target || cullingMask == 0) return;
|
||||
var evaluatedRate = m_Curve.Evaluate(_rate);
|
||||
target.SetRate(evaluatedRate, cullingMask);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The duration in seconds of the tween.
|
||||
/// </summary>
|
||||
public float duration
|
||||
{
|
||||
get => m_Duration;
|
||||
set => m_Duration = Mathf.Max(0.001f, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The delay in seconds before the tween starts.
|
||||
/// </summary>
|
||||
public float delay
|
||||
{
|
||||
get => m_Delay;
|
||||
set => m_Delay = Mathf.Max(0, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The interval in seconds between each loop.
|
||||
/// </summary>
|
||||
public float interval
|
||||
{
|
||||
get => m_Interval;
|
||||
set => m_Interval = Mathf.Max(0, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The current time of the tween.
|
||||
/// </summary>
|
||||
/// <exception cref="ArgumentOutOfRangeException"></exception>
|
||||
public float time
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_time < delay) return _time;
|
||||
var t = _time - delay;
|
||||
switch (wrapMode)
|
||||
{
|
||||
case WrapMode.Once:
|
||||
case WrapMode.PingPongOnce:
|
||||
return Mathf.Clamp(t, 0, totalTime - delay) + delay;
|
||||
case WrapMode.Loop:
|
||||
case WrapMode.PingPongLoop:
|
||||
return Mathf.Repeat(t, totalTime - delay) + delay;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public float totalTime
|
||||
{
|
||||
get
|
||||
{
|
||||
switch (wrapMode)
|
||||
{
|
||||
case WrapMode.Once: return delay + duration;
|
||||
case WrapMode.Loop: return delay + duration + interval;
|
||||
case WrapMode.PingPongOnce: return delay + duration * 2 + interval;
|
||||
case WrapMode.PingPongLoop: return delay + duration * 2 + interval * 2;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public bool restartOnEnable
|
||||
{
|
||||
get => m_RestartOnEnable;
|
||||
set => m_RestartOnEnable = value;
|
||||
}
|
||||
|
||||
public WrapMode wrapMode
|
||||
{
|
||||
get => m_WrapMode;
|
||||
set => m_WrapMode = value;
|
||||
}
|
||||
|
||||
public UpdateMode updateMode
|
||||
{
|
||||
get => m_UpdateMode;
|
||||
set => m_UpdateMode = value;
|
||||
}
|
||||
|
||||
public AnimationCurve curve
|
||||
{
|
||||
get => m_Curve;
|
||||
set => m_Curve = value;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
switch (m_UpdateMode)
|
||||
{
|
||||
case UpdateMode.Normal:
|
||||
UpdateTime(Time.deltaTime);
|
||||
break;
|
||||
case UpdateMode.Unscaled:
|
||||
UpdateTime(Time.unscaledDeltaTime);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
if (m_RestartOnEnable)
|
||||
{
|
||||
Restart();
|
||||
}
|
||||
}
|
||||
|
||||
public void Restart()
|
||||
{
|
||||
SetTime(0);
|
||||
}
|
||||
|
||||
public void SetTime(float sec)
|
||||
{
|
||||
_time = 0;
|
||||
UpdateTime(sec);
|
||||
}
|
||||
|
||||
public void UpdateTime(float deltaSec)
|
||||
{
|
||||
rate = UpdateTime_Internal(Mathf.Max(0, deltaSec)) / duration;
|
||||
}
|
||||
|
||||
private float UpdateTime_Internal(float delta)
|
||||
{
|
||||
_time += direction == Direction.Forward ? delta : -delta;
|
||||
if (_time < delay) return 0;
|
||||
|
||||
var t = _time - delay;
|
||||
switch (wrapMode)
|
||||
{
|
||||
case WrapMode.Loop:
|
||||
t = Mathf.Repeat(t, duration + interval);
|
||||
break;
|
||||
case WrapMode.PingPongOnce:
|
||||
t = Mathf.Clamp(t, 0, duration * 2 + interval);
|
||||
t = Mathf.PingPong(t, duration + interval * 0.5f);
|
||||
break;
|
||||
case WrapMode.PingPongLoop:
|
||||
t = Mathf.Repeat(t, (duration + interval) * 2);
|
||||
t = t < duration * 2 + interval
|
||||
? Mathf.PingPong(t, duration + interval * 0.5f)
|
||||
: 0;
|
||||
break;
|
||||
}
|
||||
|
||||
return Mathf.Clamp(t, 0, duration);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 90ac5d06c7b714c29a6b7b3b80e443a7
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {fileID: 617344605838670728, guid: ba0cc6c34514c480c878314286014b4a, type: 2}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user