EditorTool3D/Assets/ZXL/Scripts/Test/FS/FileSystemUIDisplay.cs

43 lines
1.3 KiB
C#

using System;
using UnityEngine;
namespace ZXLA
{
class FileSystemUIDisplay : MonoBehaviour
{
[SerializeField]
private UnityEngine.UI.ScrollRect _scrollRect;
[SerializeField]
private FileSystemItem _itemPrefab;
private FileSystem _fileSystem;
void Start()
{
_fileSystem = GameObject.FindObjectOfType<FileSystem>();
_fileSystem.OnFileSystemChanged += OnRepaint;
}
private void OnRepaint(FileSystemOperation obj)
{
if (!_itemPrefab)
throw new NullReferenceException("_itemPrefab is null");
var contentTransform = _scrollRect.content.transform;
var componentsInChildren = contentTransform.GetComponentsInChildren<FileSystemItem>();
foreach (var componentsInChild in componentsInChildren)
{
GameObject.Destroy(componentsInChild.gameObject);
}
var readOnlyDictionary = _fileSystem.GetAllObjects();
foreach (var kv in readOnlyDictionary)
{
if (!kv.Value.isDisplay)
continue;
var fileSystemItem = GameObject.Instantiate(_itemPrefab,contentTransform);
fileSystemItem.SetObject(kv.Value);
}
}
}
}