You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
143 lines
4.5 KiB
143 lines
4.5 KiB
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace uc
|
|
{
|
|
[RequireComponent(typeof(CanvasGroup))]
|
|
public class CanvasActivity : ActivityBase
|
|
{
|
|
[SerializeField]
|
|
protected float fadeTime = 0.5f;
|
|
[SerializeField]
|
|
protected float fadeOutTime = 0.5f;
|
|
|
|
[SerializeField]
|
|
float touchDelayTime = 0.0f;
|
|
|
|
public List<RectTransform> sharedComponents = new List<RectTransform>();
|
|
|
|
CanvasGroup canvas_group;
|
|
List<ActivityBase> canvas_activities = new List<ActivityBase>();
|
|
bool CanvasGroupProperty { set { canvas_group.interactable = canvas_group.blocksRaycasts = value; } }
|
|
float CanvasGroupAlpha { set { canvas_group.alpha = value; } get { return canvas_group.alpha; } }
|
|
|
|
float active_stamp;
|
|
public float ActiveStamp { get { return active_stamp; } }
|
|
|
|
|
|
private void Awake()
|
|
{
|
|
var acts = transform.GetComponentsInChildren<ActivityBase>(true);
|
|
canvas_activities.AddRange(acts);
|
|
canvas_activities.Remove(this);
|
|
// search all ui component to refresh stamp
|
|
foreach (var button in GetComponentsInChildren<Button>(true))
|
|
button.onClick.AddListener(() => RefreshActiveStamp());
|
|
foreach (var toggle in GetComponentsInChildren<Toggle>(true))
|
|
toggle.onValueChanged.AddListener((bool isOn) => RefreshActiveStamp());
|
|
foreach (var slider in GetComponentsInChildren<Slider>(true))
|
|
slider.onValueChanged.AddListener((float value) => RefreshActiveStamp());
|
|
|
|
canvas_group = GetComponent<CanvasGroup>();
|
|
}
|
|
|
|
protected override void OnEnter()
|
|
{
|
|
Debug.Log(gameObject.name + " OnEnter");
|
|
CanvasGroupAlpha = (fadeTime > 0.0f) ? 0.0f : 1.0f;
|
|
foreach (var act in canvas_activities)
|
|
act.Enter();
|
|
CanvasGroupProperty = false;
|
|
|
|
foreach (var rt in sharedComponents)
|
|
{
|
|
if (rt == null)
|
|
continue;
|
|
rt.gameObject.SetActive(true);
|
|
UpdateComponentsAlpha(rt, 0.0f, 1.0f);
|
|
}
|
|
RefreshActiveStamp();
|
|
}
|
|
|
|
protected override void OnUpdate()
|
|
{
|
|
if (IsEntering)
|
|
{
|
|
UpdateAlpha();
|
|
|
|
if (Duration > touchDelayTime && CanvasGroupAlpha == 1.0f)
|
|
{
|
|
CanvasGroupProperty = true;
|
|
|
|
foreach (var rt in sharedComponents)
|
|
UpdateComponentsAlpha(rt, IsEntering ? 1.0f : 0.0f, 0.05f);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
CanvasGroupProperty = false;
|
|
|
|
UpdateAlpha();
|
|
|
|
if (CanvasGroupAlpha == 0.0f)
|
|
{
|
|
gameObject.SetActive(false);
|
|
}
|
|
}
|
|
}
|
|
|
|
void UpdateAlpha()
|
|
{
|
|
float fade = IsEntering ? fadeTime : fadeOutTime;
|
|
float alpha = Mathf.Clamp01(Duration / fade);
|
|
if (IsEntering == false) alpha = 1.0f - alpha;
|
|
alpha = Mathf.Pow(alpha, 1.0f / 2.2f);
|
|
CanvasGroupAlpha = alpha;
|
|
}
|
|
|
|
protected override void OnLeave()
|
|
{
|
|
CanvasGroupProperty = false;
|
|
foreach (var act in canvas_activities)
|
|
act.Leave();
|
|
|
|
foreach (var rt in sharedComponents)
|
|
{
|
|
if (rt != null)
|
|
rt.gameObject.SetActive(false);
|
|
}
|
|
}
|
|
|
|
private void UpdateComponentsAlpha(RectTransform rt, float alpha, float lerp)
|
|
{
|
|
//System.Func<Color, float, float, Color> lerpAlpha = (c, a, t) =>
|
|
//{ return new Color(c.r, c.g, c.b, Mathf.Lerp(c.a, a, t)); };
|
|
if (rt == null)
|
|
return;
|
|
|
|
CanvasGroup g = rt.GetComponent<CanvasGroup>();
|
|
if (g != null)
|
|
{
|
|
float a = Mathf.Lerp(g.alpha, alpha, lerp);
|
|
g.alpha = a;
|
|
bool b = a == 0 ? false : true;
|
|
g.interactable = b;
|
|
g.blocksRaycasts = b;
|
|
return;
|
|
}
|
|
|
|
foreach (var graphic in rt.GetComponentsInChildren<Graphic>(true))
|
|
{
|
|
var c = graphic.color;
|
|
graphic.color = new Color(c.r, c.g, c.b, Mathf.Lerp(c.a, alpha, lerp));
|
|
}
|
|
}
|
|
|
|
private void RefreshActiveStamp()
|
|
{
|
|
active_stamp = Time.time;
|
|
}
|
|
}
|
|
}
|
|
|
|
|