ProjectDDD/Packages/SLUnity/SLUI/SLUIEntity.cs

221 lines
6.0 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.EventSystems;
namespace Superlazy.UI
{
[DisallowMultipleComponent]
public class SLUIEntity : SLUIObjectOnOff
{
public SLEntity Session => SLGame.SessionGet(BindPath);
protected override bool ActiveSelf => useOnOff ? comparison.Result : (isActiveAndEnabled && inList == false || (SLGame.SessionGet(BindPath)));
public bool useOnOff;
public SLValueComparison comparison;
public string onCommand;
public string offCommand;
private SLUIComponent[] uicomponents;
private UIBehaviour[] behaviors;
private Renderer[] renderers;
private Animator animator;
private SLUIOnOffTransition[] uiOnOffComponents;
private List<GameObject> childs;
private bool oldResult;
private bool inList;
protected override void Validate()
{
uicomponents = GetComponents<SLUIComponent>().Where(c => c != this).ToArray();
behaviors = GetComponents<UIBehaviour>();
renderers = GetComponents<Renderer>();
animator = GetComponent<Animator>();
}
protected override void Init()
{
uiOnOffComponents = GetComponentsInChildren<SLUIOnOffTransition>().Where(t => t.GetComponentInParent<SLUIEntity>() == this).ToArray();
foreach (var onOffCompo in uiOnOffComponents)
{
onOffCompo.InitState(this);
}
childs = new List<GameObject>();
for (var i = 0; i < transform.childCount; ++i)
{
childs.Add(transform.GetChild(i).gameObject);
}
if (useOnOff)
{
oldResult = false;
foreach (var ui in behaviors)
{
ui.enabled = false;
}
if (animator != null) animator.enabled = false;
foreach (var render in renderers)
{
render.enabled = false;
}
foreach (var compo in uicomponents)
{
compo.enabled = false;
}
foreach (var child in childs)
{
child.SetActive(false);
}
}
}
protected override void Enable()
{
if (useOnOff == false)
{
if (uiOnOffComponents.Length > 0)
{
foreach (var onOffCompo in uiOnOffComponents)
{
onOffCompo.StartChange(true, this, UpdateEnabled);
}
}
return;
}
comparison.OnEnable(BindPath, OnChange);
}
protected override void Disable()
{
if (useOnOff == false)
{
if (inList)
{
//foreach (var onOffCompo in uiOnOffComponents)
//{
// onOffCompo.InitState(false, this, onEnd);
//}
inList = false; // 리스트가 수명을 관리할 때에만 사용
}
return;
}
comparison.OnDisable();
UpdateEnabled(false);
}
private void UpdateEnabled(bool result)
{
if (useOnOff == false) return;
if (oldResult != result)
{
if (result)
{
if (string.IsNullOrEmpty(onCommand) == false)
{
SLGame.Command(onCommand, Session);
}
}
else
{
if (string.IsNullOrEmpty(offCommand) == false)
{
SLGame.Command(offCommand, Session);
}
}
}
oldResult = result;
foreach (var ui in behaviors)
{
ui.enabled = result;
}
if (animator != null) animator.enabled = result;
foreach (var render in renderers)
{
render.enabled = result;
}
foreach (var compo in uicomponents)
{
compo.enabled = result;
}
foreach (var child in childs)
{
child.SetActive(result);
}
//연출로 인한 딜레이 후에 상태를 변화 시켰는데,
//딜레이 동안 값이 변경되어서 연이어 또 변경이 필요할 때를 위함.
if (uiOnOffComponents.Length > 0 && comparison.Result != oldResult)
{
OnChange();
}
}
private void OnChange()
{
if (bindParent != null && bindParent.Active == false) return;
var result = true;
result = comparison.Result;
if (oldResult == result) return;
if (uiOnOffComponents.Length > 0)
{
foreach (var onOffCompo in uiOnOffComponents)
{
onOffCompo.StartChange(result, this, UpdateEnabled);
}
}
else
{
UpdateEnabled(result);
}
}
public void SetupByParent(string key)
{
inherit = true;
useOnOff = false;
bind = key;
inList = true;
name = key;
}
public bool OnRemoveByParent(Action<bool> onEnd)
{
if (useOnOff) return false;
if (uiOnOffComponents.Length > 0)
{
foreach (var onOffCompo in uiOnOffComponents)
{
onOffCompo.StartChange(false, this, onEnd);
}
return true;
}
return false;
}
}
}