76 lines
2.2 KiB
C#
76 lines
2.2 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using TMPro;
|
|
|
|
public class Canvas_Library : MonoBehaviour
|
|
{
|
|
List<UIDragSpawner> spawners;
|
|
Transform scrollRectContent;
|
|
|
|
static string thumbmailPath = "Library/LibraryThumbnails/";
|
|
|
|
private void Awake()
|
|
{
|
|
spawners = new List<UIDragSpawner>();
|
|
scrollRectContent = GetComponentInChildren<ScrollRect>().content;
|
|
|
|
var models = Resources.LoadAll<SimulationModel>("Library/LibraryPrefab");
|
|
var spawner = Resources.Load<UIDragSpawner>("UI/InventoryItem");
|
|
var libraryTabs = GetComponentsInChildren<Toggle>();
|
|
|
|
foreach ( var item in models)
|
|
{
|
|
|
|
if ( item.modelType == SimulationModelType.UnSeen)
|
|
{
|
|
continue;
|
|
}
|
|
else
|
|
{
|
|
var tmpSpawner = Instantiate(spawner);
|
|
var thumbnailTexturePath = thumbmailPath + item.name;
|
|
tmpSpawner.SetInfo(item, TextureToSprite(Resources.Load<Texture2D>(thumbnailTexturePath)));
|
|
tmpSpawner.transform.SetParent(scrollRectContent);
|
|
spawners.Add(tmpSpawner);
|
|
}
|
|
|
|
}
|
|
|
|
for (int i = 0; i < libraryTabs.Length; i++)
|
|
{
|
|
var index = i;
|
|
libraryTabs[i].onValueChanged.AddListener(isOn => OnActiveTabChange(index, isOn));
|
|
libraryTabs[i].onValueChanged.AddListener(isOn => libraryTabs[index].GetComponent<LibraryToggle>().OnToggle(isOn));
|
|
}
|
|
}
|
|
|
|
public void OnActiveTabChange(int index, bool isOn)
|
|
{
|
|
if ( isOn)
|
|
{
|
|
foreach( var item in spawners)
|
|
{
|
|
item.gameObject.SetActive(true);
|
|
}
|
|
|
|
if ( index == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
foreach( var item in spawners)
|
|
{
|
|
if ((int)item.targetModel.modelType != index)
|
|
{
|
|
item.gameObject.SetActive(false);
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
public Sprite TextureToSprite(Texture2D texture)
|
|
=> Sprite.Create(texture, new Rect(0f, 0f, texture.width, texture.height), new Vector2(0.5f, 0.5f), 50f, 0, SpriteMeshType.FullRect);
|
|
}
|