using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; using UnityEngine; using VketCloudGUITools.Utilities; using UnityImage = UnityEngine.UI.Image; namespace VketCloudGUITools.Runtime { [RequireComponent(typeof(RectTransform))] public class VCLayerList : MonoBehaviour { [SerializeField] private RectTransform _rectTransform; [SerializeField] private bool _isPortrait = false; [SerializeField] private string _projectName = string.Empty; //TODO:マスク系は後で //[SerializeField] private List _mask = new List(); //[SerializeField] private bool _maskOn; [SerializeField] private bool _vertical = false; [SerializeField] private bool _spreadMode = false; [SerializeField] private bool _autoLoading = false; [HideInInspector][SerializeField] private List _components = new List(); [HideInInspector][SerializeField] private VCLayerMask _mask = new VCLayerMask(); [HideInInspector] public bool MaskFoldout { get; set; } [SerializeField] private List _imageList = new List(); [SerializeField] private List _textList = new List(); [SerializeField] private List _buttonList = new List(); [SerializeField] private List _textButtonList = new List(); [SerializeField] private List _loadingBarList = new List(); [SerializeField] private List _sliderList = new List(); [SerializeField] private List _chatLogViewList = new List(); [SerializeField] private VCCanvas _vcguiCanvas = null; public string Name { get => gameObject.name; set => gameObject.name = value; } public bool IsPortrait { get => _isPortrait; set => _isPortrait = value; } public bool Show { get => gameObject.activeSelf; set { if (gameObject.activeSelf != value) { gameObject.SetActive(value); OnShowChanged(); } } } public float Z { get => -_rectTransform.localPosition.z; set => _rectTransform.localPosition = _rectTransform.localPosition.SetZ(-value); } public string ProjectName { get => _projectName; set => _projectName = value; } public bool Vertical => _vertical; public bool SpreadMode { get => _spreadMode; set => _spreadMode = value; } public bool AutoLoading { get => _autoLoading; set => _autoLoading = value; } public List Components { get => _components; set => _components = value; } public VCLayerMask Mask { get => _mask; set => _mask = value; } public bool IsValid() { return false; } public Vector2 Pos { get => _rectTransform.localPosition.ToVector2XY(); set => _rectTransform.localPosition = _rectTransform.localPosition.SetXY(value); } public Vector2 Size { get => _rectTransform.sizeDelta; set => _rectTransform.sizeDelta = value; } public VCCanvas ParentCanvas => _vcguiCanvas; public void AddImage(VCImage image) { _imageList.Add(image); } public List GetImageList() { return _imageList; } public void AddText(VCText text) { _textList.Add(text); } public List GetTextList() { return _textList; } public void AddButton(VCButton button) { _buttonList.Add(button); } public List GetButtonList() { return _buttonList; } public void AddSlider(VCSlider slider) { _sliderList.Add(slider); } public List GetSliderList() { return _sliderList; } public void AddChatLogView(VCChatLogView chatLogView) { _chatLogViewList.Add(chatLogView); } public List GetChatLogViewList() { return _chatLogViewList; } public void ResolveRequireComponents() { _rectTransform = GetComponent(); } #if UNITY_EDITOR private void Reset() { ResolveRequireComponents(); } #endif private IVCGUIItem FindGUIItem(string guiName) { foreach (var item in _imageList) { if (item.Name == guiName) { return item; } } foreach (var item in _textList) { if (item.Name == guiName) { return item; } } foreach (var item in _buttonList) { if (item.Name == guiName) { return item; } } // TextButtonはNameをもっていないので検索できない foreach (var item in _loadingBarList) { if (item.Name == guiName) { return item; } } return null; } public bool SetGUIShow(string guiName, bool show) { var guiItem = FindGUIItem(guiName); if (guiItem == null) return false; guiItem.Show = show; return true; } public bool SetGUIText(string guiName, string text) { var guiItem = FindGUIItem(guiName); if (guiItem == null) return false; var guiText = guiItem as VCText; if (guiText == null) return false; guiText.Text = text; return true; } public bool SetGUIImage(string guiName, Sprite sprite) { var guiItem = FindGUIItem(guiName); if (guiItem == null) return false; var guiImage = guiItem as VCImage; if (guiImage == null) return false; guiImage.Sprite = sprite; return true; } private void OnShowChanged() { foreach (var image in _imageList) { image.OnParentLayerShowChanged(); } foreach (var text in _textList) { text.OnParentLayerShowChanged(); } foreach (var button in _buttonList) { button.OnParentLayerShowChanged(); } foreach (var slider in _sliderList) { slider.OnParentLayerShowChanged(); } } public void OnResolveReferences(VCCanvas canvas) { this._vcguiCanvas = canvas; _imageList = GetComponentsInChildren(includeInactive: true).ToList(); _textList = GetComponentsInChildren(includeInactive: true).ToList(); _buttonList = GetComponentsInChildren(includeInactive: true).ToList(); _sliderList = GetComponentsInChildren(includeInactive: true).ToList(); foreach (var image in _imageList) { image.OnResolveReferences(this); } foreach (var text in _textList) { text.OnResolveReferences(this); } foreach (var button in _buttonList) { button.OnResolveReferences(this); } foreach (var slider in _sliderList) { slider.OnResolveReferences(this); } } private void OnValidate() { var rectTransform = GetComponent(); if (rectTransform != null && rectTransform.gameObject != this.gameObject) { rectTransform = null; } if (rectTransform == null) { rectTransform = gameObject.AddComponent(); } } } }