refactor
This commit is contained in:
95
Editor/RebootWindow/WorldsView.cs
Normal file
95
Editor/RebootWindow/WorldsView.cs
Normal file
@@ -0,0 +1,95 @@
|
||||
using RebootKit.Engine;
|
||||
using RebootKit.Engine.Services.Simulation;
|
||||
using RebootKit.Engine.UI;
|
||||
using RebootKitEditor.Utils;
|
||||
using UnityEditor;
|
||||
using UnityEditor.SceneManagement;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace RebootKitEditor.RebootWindow {
|
||||
public class WorldsView : IView {
|
||||
public void Dispose() {
|
||||
}
|
||||
|
||||
public VisualElement Build() {
|
||||
VisualElement root = new() {
|
||||
style = {
|
||||
paddingBottom = 4,
|
||||
paddingTop = 4,
|
||||
paddingLeft = 4,
|
||||
paddingRight = 4,
|
||||
}
|
||||
};
|
||||
|
||||
WorldConfigAsset[] worlds = AssetDatabaseEx.LoadAllAssets<WorldConfigAsset>();
|
||||
foreach (WorldConfigAsset worldConfigAsset in worlds) {
|
||||
VisualElement worldView = CreateWorldView(worldConfigAsset);
|
||||
root.Add(worldView);
|
||||
}
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
VisualElement CreateWorldView(WorldConfigAsset worldConfigAsset) {
|
||||
VisualElement root = new() {
|
||||
style = {
|
||||
backgroundColor = new Color(0.1f, 0.1f, 0.1f),
|
||||
paddingBottom = 4,
|
||||
paddingTop = 4,
|
||||
paddingLeft = 4,
|
||||
paddingRight = 4,
|
||||
borderBottomLeftRadius = 4,
|
||||
borderBottomRightRadius = 4
|
||||
}
|
||||
};
|
||||
|
||||
Label label = new(worldConfigAsset.name) {
|
||||
style = {
|
||||
color = new Color(0.7f, 0.9f, 0.9f),
|
||||
unityFontStyleAndWeight = FontStyle.Bold
|
||||
}
|
||||
};
|
||||
root.Add(label);
|
||||
|
||||
Button openButton = new(() => OpenWorldScenes(worldConfigAsset)) {
|
||||
text = "Open",
|
||||
style = {
|
||||
backgroundColor = new Color(0.2f, 0.2f, 0.2f),
|
||||
paddingBottom = 4,
|
||||
paddingTop = 4,
|
||||
paddingLeft = 4,
|
||||
paddingRight = 4,
|
||||
borderBottomLeftRadius = 4,
|
||||
borderBottomRightRadius = 4
|
||||
}
|
||||
};
|
||||
root.Add(openButton);
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
static void OpenWorldScenes(WorldConfigAsset worldConfigAsset) {
|
||||
if (worldConfigAsset == null) {
|
||||
Debug.LogError("WorldConfigAsset is null");
|
||||
return;
|
||||
}
|
||||
|
||||
EditorSceneManager.SaveOpenScenes();
|
||||
|
||||
// Load first scene from build settings
|
||||
string mainScenePath = SceneManager.GetSceneByBuildIndex(RConsts.k_MainSceneBuildIndex).path;
|
||||
EditorSceneManager.OpenScene(mainScenePath, OpenSceneMode.Single);
|
||||
|
||||
// Load world scene
|
||||
string worldScenePath = AssetDatabase.GetAssetPath(worldConfigAsset.Config.mainScene.editorAsset);
|
||||
if (string.IsNullOrEmpty(worldScenePath)) {
|
||||
Debug.LogError($"WorldConfigAsset {worldConfigAsset.name} has invalid main scene path");
|
||||
return;
|
||||
}
|
||||
|
||||
SceneManager.SetActiveScene(EditorSceneManager.OpenScene(worldScenePath, OpenSceneMode.Additive));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user