using System.Collections.Generic; using UnityEngine; namespace Superlazy.UI { [DisallowMultipleComponent] [RequireComponent(typeof(Canvas))] public class SLUICanvas : SLUIObjectOnOff { protected override bool ActiveSelf => !useOnOff || comparison.Result; public string bindCamera; public bool useOnOff; public SLValueComparison comparison; private Canvas canvas; private List childs; private bool oldResult; protected override void Validate() { canvas = GetComponent(); } protected override void Init() { childs = new List(); for (var i = 0; i < transform.childCount; ++i) { childs.Add(transform.GetChild(i).gameObject); } if (useOnOff) { oldResult = false; foreach (var child in childs) { child.SetActive(false); } } if (string.IsNullOrEmpty(bindCamera) == false) { canvas.worldCamera = GameObject.Find(bindCamera).GetComponent(); } } protected override void Enable() { if (useOnOff == false) { } comparison.OnEnable(BindPath, OnChange); } protected override void Disable() { if (useOnOff == false) return; comparison.OnDisable(); UpdateEnabled(false); } private void UpdateEnabled(bool result) { oldResult = result; foreach (var child in childs) { child.SetActive(result); } } private void OnChange() { var result = comparison.Result; if (oldResult == result) return; UpdateEnabled(result); } } }