2021-04-15 20:18:03 +10:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Collections.ObjectModel;
|
2021-04-16 17:49:05 +10:00
|
|
|
|
using System.IO;
|
2021-04-15 20:18:03 +10:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.SceneManagement;
|
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
using UnityExplorer.Core;
|
2021-04-16 17:49:05 +10:00
|
|
|
|
using UnityExplorer.Core.Config;
|
2021-04-15 20:18:03 +10:00
|
|
|
|
using UnityExplorer.UI.Models;
|
|
|
|
|
using UnityExplorer.UI.Utility;
|
|
|
|
|
using UnityExplorer.UI.Widgets;
|
|
|
|
|
|
|
|
|
|
namespace UnityExplorer.UI.Panels
|
|
|
|
|
{
|
2021-04-16 17:49:05 +10:00
|
|
|
|
public class SceneExplorer : UIPanel
|
2021-04-15 20:18:03 +10:00
|
|
|
|
{
|
2021-04-16 17:49:05 +10:00
|
|
|
|
public override string Name => "Scene Explorer";
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
2021-04-16 04:33:13 +10:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Whether to automatically update per auto-update interval or not.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool AutoUpdate = false;
|
|
|
|
|
|
2021-04-15 20:18:03 +10:00
|
|
|
|
public TransformTree Tree;
|
|
|
|
|
private float timeOfLastUpdate = -1f;
|
|
|
|
|
|
2021-04-16 17:49:05 +10:00
|
|
|
|
private GameObject refreshRow;
|
2021-04-15 20:18:03 +10:00
|
|
|
|
private Dropdown sceneDropdown;
|
|
|
|
|
private readonly Dictionary<int, Dropdown.OptionData> sceneToDropdownOption = new Dictionary<int, Dropdown.OptionData>();
|
|
|
|
|
|
|
|
|
|
public SceneExplorer()
|
|
|
|
|
{
|
|
|
|
|
SceneHandler.OnInspectedSceneChanged += SceneHandler_OnInspectedSceneChanged;
|
|
|
|
|
SceneHandler.OnLoadedScenesChanged += SceneHandler_OnLoadedScenesChanged;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerable<GameObject> GetRootEntries() => SceneHandler.CurrentRootObjects;
|
|
|
|
|
|
2021-04-16 04:33:13 +10:00
|
|
|
|
public void ForceUpdate()
|
|
|
|
|
{
|
|
|
|
|
ExpensiveUpdate();
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-15 20:18:03 +10:00
|
|
|
|
public override void Update()
|
|
|
|
|
{
|
2021-04-16 17:49:05 +10:00
|
|
|
|
if ((AutoUpdate || !SceneHandler.InspectingAssetScene) && Time.realtimeSinceStartup - timeOfLastUpdate >= 1f)
|
2021-04-16 04:33:13 +10:00
|
|
|
|
{
|
|
|
|
|
timeOfLastUpdate = Time.realtimeSinceStartup;
|
|
|
|
|
ExpensiveUpdate();
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
2021-04-16 04:33:13 +10:00
|
|
|
|
public void ExpensiveUpdate()
|
|
|
|
|
{
|
2021-04-21 18:44:43 +10:00
|
|
|
|
//Tree.Scroller.WritingLocked = true;
|
2021-04-15 20:18:03 +10:00
|
|
|
|
SceneHandler.Update();
|
2021-04-21 18:44:43 +10:00
|
|
|
|
Tree.RefreshData(true);
|
|
|
|
|
////Tree.Scroller.WritingLocked = false;
|
2021-04-15 20:18:03 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnDropdownChanged(int value)
|
|
|
|
|
{
|
|
|
|
|
if (value < 0 || SceneHandler.LoadedScenes.Count <= value)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
SceneHandler.SelectedScene = SceneHandler.LoadedScenes[value];
|
|
|
|
|
SceneHandler.Update();
|
|
|
|
|
Tree.RefreshData(true);
|
2021-04-16 17:49:05 +10:00
|
|
|
|
OnSelectedSceneChanged(SceneHandler.SelectedScene.Value);
|
2021-04-15 20:18:03 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SceneHandler_OnInspectedSceneChanged(Scene scene)
|
|
|
|
|
{
|
|
|
|
|
if (!sceneToDropdownOption.ContainsKey(scene.handle))
|
|
|
|
|
PopulateSceneDropdown();
|
|
|
|
|
|
|
|
|
|
if (sceneToDropdownOption.ContainsKey(scene.handle))
|
|
|
|
|
{
|
2021-04-16 02:47:15 +10:00
|
|
|
|
var opt = sceneToDropdownOption[scene.handle];
|
|
|
|
|
int idx = sceneDropdown.options.IndexOf(opt);
|
2021-04-15 20:18:03 +10:00
|
|
|
|
if (sceneDropdown.value != idx)
|
|
|
|
|
sceneDropdown.value = idx;
|
2021-04-16 02:47:15 +10:00
|
|
|
|
else
|
|
|
|
|
sceneDropdown.captionText.text = opt.text;
|
2021-04-15 20:18:03 +10:00
|
|
|
|
}
|
2021-04-16 17:49:05 +10:00
|
|
|
|
|
|
|
|
|
OnSelectedSceneChanged(scene);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnSelectedSceneChanged(Scene scene)
|
|
|
|
|
{
|
|
|
|
|
if (refreshRow)
|
|
|
|
|
refreshRow.SetActive(!scene.IsValid());
|
2021-04-15 20:18:03 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SceneHandler_OnLoadedScenesChanged(ReadOnlyCollection<Scene> loadedScenes)
|
|
|
|
|
{
|
|
|
|
|
PopulateSceneDropdown();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void PopulateSceneDropdown()
|
|
|
|
|
{
|
|
|
|
|
sceneToDropdownOption.Clear();
|
|
|
|
|
sceneDropdown.options.Clear();
|
|
|
|
|
|
|
|
|
|
foreach (var scene in SceneHandler.LoadedScenes)
|
|
|
|
|
{
|
|
|
|
|
string name = scene.name?.Trim();
|
|
|
|
|
|
|
|
|
|
if (!scene.IsValid())
|
|
|
|
|
name = "HideAndDontSave";
|
|
|
|
|
else if (string.IsNullOrEmpty(name))
|
|
|
|
|
name = "<untitled>";
|
|
|
|
|
|
2021-04-16 02:47:15 +10:00
|
|
|
|
var option = new Dropdown.OptionData(name);
|
|
|
|
|
sceneDropdown.options.Add(option);
|
|
|
|
|
sceneToDropdownOption.Add(scene.handle, option);
|
2021-04-15 20:18:03 +10:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnFilterInput(string input)
|
|
|
|
|
{
|
|
|
|
|
Tree.CurrentFilter = input;
|
2021-04-22 17:53:29 +10:00
|
|
|
|
Tree.RefreshData(true, true);
|
2021-04-15 20:18:03 +10:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-21 23:07:15 +10:00
|
|
|
|
//private float highestRectHeight;
|
|
|
|
|
|
|
|
|
|
//public override void OnFinishResize(RectTransform panel)
|
|
|
|
|
//{
|
|
|
|
|
// base.OnFinishResize(panel);
|
|
|
|
|
// RuntimeProvider.Instance.StartCoroutine(DelayedRefresh(panel));
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
//private IEnumerator DelayedRefresh(RectTransform obj)
|
|
|
|
|
//{
|
|
|
|
|
// yield return null;
|
|
|
|
|
|
|
|
|
|
// if (obj.rect.height > highestRectHeight)
|
|
|
|
|
// {
|
|
|
|
|
// // height increased, hard refresh required.
|
|
|
|
|
// highestRectHeight = obj.rect.height;
|
|
|
|
|
// //Tree.Scroller.ReloadData();
|
|
|
|
|
// }
|
|
|
|
|
// Tree.Scroller.RefreshCells(true);
|
|
|
|
|
//}
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
2021-04-16 21:07:32 +10:00
|
|
|
|
public override void SaveToConfigManager()
|
|
|
|
|
{
|
|
|
|
|
ConfigManager.SceneExplorerData.Value = this.ToSaveData();
|
2021-04-15 20:18:03 +10:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-16 17:49:05 +10:00
|
|
|
|
public override void LoadSaveData()
|
2021-04-15 20:18:03 +10:00
|
|
|
|
{
|
2021-04-16 21:07:32 +10:00
|
|
|
|
ApplySaveData(ConfigManager.SceneExplorerData.Value);
|
2021-04-16 17:49:05 +10:00
|
|
|
|
}
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
2021-04-16 17:49:05 +10:00
|
|
|
|
public override void SetDefaultPosAndAnchors()
|
|
|
|
|
{
|
|
|
|
|
mainPanelRect.localPosition = Vector2.zero;
|
|
|
|
|
mainPanelRect.anchorMin = Vector3.zero;
|
|
|
|
|
mainPanelRect.anchorMax = new Vector2(0, 1);
|
|
|
|
|
mainPanelRect.sizeDelta = new Vector2(300f, mainPanelRect.sizeDelta.y);
|
2021-04-16 23:59:54 +10:00
|
|
|
|
mainPanelRect.anchoredPosition = new Vector2(200, 0);
|
|
|
|
|
mainPanelRect.offsetMin = new Vector2(mainPanelRect.offsetMin.x, 100); // bottom
|
|
|
|
|
mainPanelRect.offsetMax = new Vector2(mainPanelRect.offsetMax.x, -50); // top
|
2021-04-16 17:49:05 +10:00
|
|
|
|
mainPanelRect.pivot = new Vector2(0.5f, 0.5f);
|
|
|
|
|
}
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
2021-04-16 17:49:05 +10:00
|
|
|
|
public override void ConstructPanelContent()
|
|
|
|
|
{
|
|
|
|
|
// Tool bar (top area)
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
2021-04-16 17:49:05 +10:00
|
|
|
|
var toolbar = UIFactory.CreateVerticalGroup(content, "Toolbar", true, true, true, true, 2, new Vector4(2, 2, 2, 2),
|
2021-04-15 20:18:03 +10:00
|
|
|
|
new Color(0.15f, 0.15f, 0.15f));
|
2021-04-16 04:33:13 +10:00
|
|
|
|
|
|
|
|
|
// Scene selector dropdown
|
2021-04-16 17:49:05 +10:00
|
|
|
|
|
2021-04-15 20:18:03 +10:00
|
|
|
|
var dropdownObj = UIFactory.CreateDropdown(toolbar, out sceneDropdown, "<notset>", 13, OnDropdownChanged);
|
|
|
|
|
UIFactory.SetLayoutElement(dropdownObj, minHeight: 25, flexibleHeight: 0);
|
|
|
|
|
|
|
|
|
|
SceneHandler.Update();
|
|
|
|
|
PopulateSceneDropdown();
|
2021-04-16 02:47:15 +10:00
|
|
|
|
sceneDropdown.captionText.text = sceneToDropdownOption.First().Value.text;
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
2021-04-16 04:33:13 +10:00
|
|
|
|
// Filter row
|
|
|
|
|
|
|
|
|
|
var filterRow = UIFactory.CreateHorizontalGroup(toolbar, "FilterGroup", true, true, true, true, 2, new Vector4(2, 2, 2, 2));
|
|
|
|
|
UIFactory.SetLayoutElement(filterRow, minHeight: 25, flexibleHeight: 0);
|
|
|
|
|
|
2021-04-15 20:18:03 +10:00
|
|
|
|
//Filter input field
|
2021-04-16 04:33:13 +10:00
|
|
|
|
var inputFieldObj = UIFactory.CreateInputField(filterRow, "FilterInput", "Search...", out InputField inputField, 13);
|
|
|
|
|
inputField.targetGraphic.color = new Color(0.2f, 0.2f, 0.2f);
|
|
|
|
|
RuntimeProvider.Instance.SetColorBlock(inputField, new Color(0.4f, 0.4f, 0.4f), new Color(0.2f, 0.2f, 0.2f), new Color(0.08f, 0.08f, 0.08f));
|
2021-04-15 20:18:03 +10:00
|
|
|
|
UIFactory.SetLayoutElement(inputFieldObj, minHeight: 25);
|
|
|
|
|
inputField.onValueChanged.AddListener(OnFilterInput);
|
|
|
|
|
|
2021-04-16 17:49:05 +10:00
|
|
|
|
// refresh row
|
|
|
|
|
|
|
|
|
|
refreshRow = UIFactory.CreateHorizontalGroup(toolbar, "RefreshGroup", true, true, true, true, 2, new Vector4(2, 2, 2, 2));
|
|
|
|
|
UIFactory.SetLayoutElement(refreshRow, minHeight: 30, flexibleHeight: 0);
|
|
|
|
|
|
|
|
|
|
var refreshButton = UIFactory.CreateButton(refreshRow, "RefreshButton", "Update", ForceUpdate);
|
|
|
|
|
UIFactory.SetLayoutElement(refreshButton.gameObject, minWidth: 65, flexibleWidth: 0);
|
|
|
|
|
|
|
|
|
|
var refreshToggle = UIFactory.CreateToggle(refreshRow, "RefreshToggle", out Toggle toggle, out Text text);
|
|
|
|
|
UIFactory.SetLayoutElement(refreshToggle, flexibleWidth: 9999);
|
|
|
|
|
text.text = "Auto-update (1 second)";
|
|
|
|
|
text.alignment = TextAnchor.MiddleLeft;
|
|
|
|
|
text.color = Color.white;
|
|
|
|
|
text.fontSize = 12;
|
|
|
|
|
toggle.isOn = false;
|
|
|
|
|
toggle.onValueChanged.AddListener((bool val) => AutoUpdate = val);
|
|
|
|
|
|
|
|
|
|
refreshRow.SetActive(false);
|
|
|
|
|
|
2021-04-15 20:18:03 +10:00
|
|
|
|
// Transform Tree
|
2021-04-21 21:04:26 +10:00
|
|
|
|
|
2021-04-22 17:53:29 +10:00
|
|
|
|
var scrollPool = UIFactory.CreateScrollPool(content, "TransformTree", out GameObject scrollObj,
|
2021-04-16 02:47:15 +10:00
|
|
|
|
out GameObject scrollContent, new Color(0.15f, 0.15f, 0.15f));
|
|
|
|
|
UIFactory.SetLayoutElement(scrollObj, flexibleHeight: 9999);
|
2021-04-15 20:18:03 +10:00
|
|
|
|
UIFactory.SetLayoutElement(scrollContent, flexibleHeight: 9999);
|
|
|
|
|
|
2021-04-22 17:53:29 +10:00
|
|
|
|
Tree = new TransformTree(scrollPool) { GetRootEntriesMethod = GetRootEntries };
|
2021-04-16 02:47:15 +10:00
|
|
|
|
Tree.Init();
|
|
|
|
|
|
2021-04-22 17:53:29 +10:00
|
|
|
|
//scrollPool.Viewport.GetComponent<Mask>().enabled = false;
|
|
|
|
|
//UIRoot.GetComponent<Mask>().enabled = false;
|
|
|
|
|
|
2021-04-21 23:07:15 +10:00
|
|
|
|
// Scene Loader
|
|
|
|
|
|
|
|
|
|
ConstructSceneLoader();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private const string DEFAULT_LOAD_TEXT = "[Select a scene]";
|
2021-04-16 17:49:05 +10:00
|
|
|
|
|
2021-04-21 23:07:15 +10:00
|
|
|
|
private void ConstructSceneLoader()
|
|
|
|
|
{
|
2021-04-16 17:49:05 +10:00
|
|
|
|
// Scene Loader
|
|
|
|
|
try
|
|
|
|
|
{
|
2021-04-16 18:37:26 +10:00
|
|
|
|
if (SceneHandler.WasAbleToGetScenesInBuild)
|
|
|
|
|
{
|
2021-04-21 18:44:43 +10:00
|
|
|
|
var sceneLoaderObj = UIFactory.CreateVerticalGroup(content, "SceneLoader", true, true, true, true);
|
|
|
|
|
UIFactory.SetLayoutElement(sceneLoaderObj, minHeight: 25);
|
|
|
|
|
//sceneLoaderObj.SetActive(false);
|
|
|
|
|
|
|
|
|
|
var loaderTitle = UIFactory.CreateLabel(sceneLoaderObj, "SceneLoaderLabel", "Scene Loader", TextAnchor.MiddleLeft, Color.white, true, 14);
|
2021-04-16 18:37:26 +10:00
|
|
|
|
UIFactory.SetLayoutElement(loaderTitle.gameObject, minHeight: 25, flexibleHeight: 0);
|
2021-04-16 17:49:05 +10:00
|
|
|
|
|
2021-04-21 18:44:43 +10:00
|
|
|
|
var allSceneDropObj = UIFactory.CreateDropdown(sceneLoaderObj, out Dropdown allSceneDrop, "", 14, null);
|
2021-04-16 18:37:26 +10:00
|
|
|
|
UIFactory.SetLayoutElement(allSceneDropObj, minHeight: 25, minWidth: 150, flexibleWidth: 0, flexibleHeight: 0);
|
2021-04-16 17:49:05 +10:00
|
|
|
|
|
2021-04-21 23:07:15 +10:00
|
|
|
|
allSceneDrop.options.Add(new Dropdown.OptionData(DEFAULT_LOAD_TEXT));
|
2021-04-21 18:44:43 +10:00
|
|
|
|
|
2021-04-16 18:37:26 +10:00
|
|
|
|
foreach (var scene in SceneHandler.AllSceneNames)
|
2021-04-16 21:07:32 +10:00
|
|
|
|
allSceneDrop.options.Add(new Dropdown.OptionData(Path.GetFileNameWithoutExtension(scene)));
|
2021-04-16 17:49:05 +10:00
|
|
|
|
|
2021-04-16 18:37:26 +10:00
|
|
|
|
allSceneDrop.value = 1;
|
|
|
|
|
allSceneDrop.value = 0;
|
2021-04-16 17:49:05 +10:00
|
|
|
|
|
2021-04-21 18:44:43 +10:00
|
|
|
|
var buttonRow = UIFactory.CreateHorizontalGroup(sceneLoaderObj, "LoadButtons", true, true, true, true, 4);
|
2021-04-16 17:49:05 +10:00
|
|
|
|
|
2021-04-16 18:37:26 +10:00
|
|
|
|
var loadButton = UIFactory.CreateButton(buttonRow, "LoadSceneButton", "Load (Single)", () =>
|
2021-04-16 17:49:05 +10:00
|
|
|
|
{
|
2021-04-21 18:44:43 +10:00
|
|
|
|
TryLoadScene(LoadSceneMode.Single, allSceneDrop);
|
2021-04-16 18:37:26 +10:00
|
|
|
|
}, new Color(0.1f, 0.3f, 0.3f));
|
|
|
|
|
UIFactory.SetLayoutElement(loadButton.gameObject, minHeight: 25, minWidth: 150);
|
|
|
|
|
|
|
|
|
|
var loadAdditiveButton = UIFactory.CreateButton(buttonRow, "LoadSceneButton", "Load (Additive)", () =>
|
2021-04-16 17:49:05 +10:00
|
|
|
|
{
|
2021-04-21 18:44:43 +10:00
|
|
|
|
TryLoadScene(LoadSceneMode.Additive, allSceneDrop);
|
2021-04-16 18:37:26 +10:00
|
|
|
|
}, new Color(0.1f, 0.3f, 0.3f));
|
|
|
|
|
UIFactory.SetLayoutElement(loadAdditiveButton.gameObject, minHeight: 25, minWidth: 150);
|
2021-04-21 23:07:15 +10:00
|
|
|
|
|
|
|
|
|
var disabledColor = new Color(0.24f, 0.24f, 0.24f);
|
|
|
|
|
RuntimeProvider.Instance.SetColorBlock(loadButton, disabled: disabledColor);
|
|
|
|
|
RuntimeProvider.Instance.SetColorBlock(loadAdditiveButton, disabled: disabledColor);
|
|
|
|
|
|
|
|
|
|
loadButton.interactable = false;
|
|
|
|
|
loadAdditiveButton.interactable = false;
|
|
|
|
|
|
|
|
|
|
allSceneDrop.onValueChanged.AddListener((int val) =>
|
|
|
|
|
{
|
|
|
|
|
var text = allSceneDrop.options[val].text;
|
|
|
|
|
if (text == DEFAULT_LOAD_TEXT)
|
|
|
|
|
{
|
|
|
|
|
loadButton.interactable = false;
|
|
|
|
|
loadAdditiveButton.interactable = false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
loadButton.interactable = true;
|
|
|
|
|
loadAdditiveButton.interactable = true;
|
|
|
|
|
}
|
|
|
|
|
});
|
2021-04-16 18:37:26 +10:00
|
|
|
|
}
|
2021-04-16 17:49:05 +10:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
ExplorerCore.LogWarning($"Could not create the Scene Loader helper! {ex.ReflectionExToString()}");
|
|
|
|
|
}
|
2021-04-15 20:18:03 +10:00
|
|
|
|
}
|
2021-04-21 18:44:43 +10:00
|
|
|
|
|
|
|
|
|
private void TryLoadScene(LoadSceneMode mode, Dropdown allSceneDrop)
|
|
|
|
|
{
|
|
|
|
|
var text = allSceneDrop.options[allSceneDrop.value].text;
|
|
|
|
|
|
2021-04-21 23:07:15 +10:00
|
|
|
|
if (text == DEFAULT_LOAD_TEXT)
|
2021-04-21 18:44:43 +10:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
SceneManager.LoadScene(text, mode);
|
|
|
|
|
allSceneDrop.value = 0;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
ExplorerCore.LogWarning($"Unable to load the Scene! {ex.ReflectionExToString()}");
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-15 20:18:03 +10:00
|
|
|
|
}
|
|
|
|
|
}
|