Skip to content
This repository was archived by the owner on May 9, 2023. It is now read-only.

Commit 587842f

Browse files
committed
Add search filter to scene loader dropdown
1 parent b9c641f commit 587842f

File tree

1 file changed

+57
-25
lines changed

1 file changed

+57
-25
lines changed

src/ObjectExplorer/SceneExplorer.cs

Lines changed: 57 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ public SceneExplorer(ObjectExplorerPanel parent)
4242
private Dropdown sceneDropdown;
4343
private readonly Dictionary<Scene, Dropdown.OptionData> sceneToDropdownOption = new Dictionary<Scene, Dropdown.OptionData>();
4444

45+
// scene loader
46+
private Dropdown allSceneDropdown;
47+
private ButtonRef loadButton;
48+
private ButtonRef loadAdditiveButton;
49+
4550
private IEnumerable<GameObject> GetRootEntries() => SceneHandler.CurrentRootObjects;
4651

4752
public void Update()
@@ -155,7 +160,7 @@ private void OnFilterInput(string input)
155160

156161
private void TryLoadScene(LoadSceneMode mode, Dropdown allSceneDrop)
157162
{
158-
var text = allSceneDrop.options[allSceneDrop.value].text;
163+
var text = allSceneDrop.captionText.text;
159164

160165
if (text == DEFAULT_LOAD_TEXT)
161166
return;
@@ -250,6 +255,38 @@ public override void ConstructUI(GameObject content)
250255

251256
private const string DEFAULT_LOAD_TEXT = "[Select a scene]";
252257

258+
private void RefreshSceneLoaderOptions(string filter)
259+
{
260+
allSceneDropdown.options.Clear();
261+
allSceneDropdown.options.Add(new Dropdown.OptionData(DEFAULT_LOAD_TEXT));
262+
263+
foreach (var scene in SceneHandler.AllSceneNames)
264+
{
265+
if (string.IsNullOrEmpty(filter) || scene.ContainsIgnoreCase(filter))
266+
allSceneDropdown.options.Add(new Dropdown.OptionData(Path.GetFileNameWithoutExtension(scene)));
267+
}
268+
269+
allSceneDropdown.RefreshShownValue();
270+
271+
if (loadButton != null)
272+
RefreshSceneLoaderButtons();
273+
}
274+
275+
private void RefreshSceneLoaderButtons()
276+
{
277+
var text = allSceneDropdown.captionText.text;
278+
if (text == DEFAULT_LOAD_TEXT)
279+
{
280+
loadButton.Component.interactable = false;
281+
loadAdditiveButton.Component.interactable = false;
282+
}
283+
else
284+
{
285+
loadButton.Component.interactable = true;
286+
loadAdditiveButton.Component.interactable = true;
287+
}
288+
}
289+
253290
private void ConstructSceneLoader()
254291
{
255292
// Scene Loader
@@ -259,36 +296,41 @@ private void ConstructSceneLoader()
259296
{
260297
var sceneLoaderObj = UIFactory.CreateVerticalGroup(m_uiRoot, "SceneLoader", true, true, true, true);
261298
UIFactory.SetLayoutElement(sceneLoaderObj, minHeight: 25);
262-
//sceneLoaderObj.SetActive(false);
299+
300+
// Title
263301

264302
var loaderTitle = UIFactory.CreateLabel(sceneLoaderObj, "SceneLoaderLabel", "Scene Loader", TextAnchor.MiddleLeft, Color.white, true, 14);
265303
UIFactory.SetLayoutElement(loaderTitle.gameObject, minHeight: 25, flexibleHeight: 0);
266304

267-
var allSceneDropObj = UIFactory.CreateDropdown(sceneLoaderObj, out Dropdown allSceneDrop, "", 14, null);
268-
UIFactory.SetLayoutElement(allSceneDropObj, minHeight: 25, minWidth: 150, flexibleWidth: 0, flexibleHeight: 0);
305+
// Search filter
306+
307+
var searchFilterObj = UIFactory.CreateInputField(sceneLoaderObj, "SearchFilterInput", "Filter scene names...");
308+
UIFactory.SetLayoutElement(searchFilterObj.UIRoot, minHeight: 25, flexibleHeight: 0);
309+
searchFilterObj.OnValueChanged += RefreshSceneLoaderOptions;
269310

270-
allSceneDrop.options.Add(new Dropdown.OptionData(DEFAULT_LOAD_TEXT));
311+
// Dropdown
312+
313+
var allSceneDropObj = UIFactory.CreateDropdown(sceneLoaderObj, out allSceneDropdown, "", 14, null);
314+
UIFactory.SetLayoutElement(allSceneDropObj, minHeight: 25, minWidth: 150, flexibleWidth: 0, flexibleHeight: 0);
271315

272-
foreach (var scene in SceneHandler.AllSceneNames)
273-
allSceneDrop.options.Add(new Dropdown.OptionData(Path.GetFileNameWithoutExtension(scene)));
316+
RefreshSceneLoaderOptions(string.Empty);
274317

275-
allSceneDrop.value = 1;
276-
allSceneDrop.value = 0;
318+
// Button row
277319

278320
var buttonRow = UIFactory.CreateHorizontalGroup(sceneLoaderObj, "LoadButtons", true, true, true, true, 4);
279321

280-
var loadButton = UIFactory.CreateButton(buttonRow, "LoadSceneButton", "Load (Single)", new Color(0.1f, 0.3f, 0.3f));
322+
loadButton = UIFactory.CreateButton(buttonRow, "LoadSceneButton", "Load (Single)", new Color(0.1f, 0.3f, 0.3f));
281323
UIFactory.SetLayoutElement(loadButton.Component.gameObject, minHeight: 25, minWidth: 150);
282324
loadButton.OnClick += () =>
283325
{
284-
TryLoadScene(LoadSceneMode.Single, allSceneDrop);
326+
TryLoadScene(LoadSceneMode.Single, allSceneDropdown);
285327
};
286328

287-
var loadAdditiveButton = UIFactory.CreateButton(buttonRow, "LoadSceneButton", "Load (Additive)", new Color(0.1f, 0.3f, 0.3f));
329+
loadAdditiveButton = UIFactory.CreateButton(buttonRow, "LoadSceneButton", "Load (Additive)", new Color(0.1f, 0.3f, 0.3f));
288330
UIFactory.SetLayoutElement(loadAdditiveButton.Component.gameObject, minHeight: 25, minWidth: 150);
289331
loadAdditiveButton.OnClick += () =>
290332
{
291-
TryLoadScene(LoadSceneMode.Additive, allSceneDrop);
333+
TryLoadScene(LoadSceneMode.Additive, allSceneDropdown);
292334
};
293335

294336
var disabledColor = new Color(0.24f, 0.24f, 0.24f);
@@ -298,19 +340,9 @@ private void ConstructSceneLoader()
298340
loadButton.Component.interactable = false;
299341
loadAdditiveButton.Component.interactable = false;
300342

301-
allSceneDrop.onValueChanged.AddListener((int val) =>
343+
allSceneDropdown.onValueChanged.AddListener((int val) =>
302344
{
303-
var text = allSceneDrop.options[val].text;
304-
if (text == DEFAULT_LOAD_TEXT)
305-
{
306-
loadButton.Component.interactable = false;
307-
loadAdditiveButton.Component.interactable = false;
308-
}
309-
else
310-
{
311-
loadButton.Component.interactable = true;
312-
loadAdditiveButton.Component.interactable = true;
313-
}
345+
RefreshSceneLoaderButtons();
314346
});
315347
}
316348
}

0 commit comments

Comments
 (0)