-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
180 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,154 +1,141 @@ | ||
using UnityEngine; | ||
using UnityEditor; | ||
using UnityEditor; | ||
using UnityEditor.SceneManagement; | ||
using System; | ||
using System.IO; | ||
using System.Collections.Generic; | ||
|
||
namespace chsxf | ||
{ | ||
[InitializeOnLoad] | ||
public static class AutoScene | ||
{ | ||
private const string VERSION = "1.1.0"; | ||
|
||
private static string ProjectPath { | ||
get { | ||
string assetsPath = Application.dataPath; | ||
return Directory.GetParent(assetsPath).FullName; | ||
} | ||
} | ||
|
||
private static string PrefsKey { | ||
get { return ProjectPath + ".AutoScene"; } | ||
} | ||
|
||
private static string PrefsKeyEnabled { | ||
get { return PrefsKey + ".enabled"; } | ||
} | ||
|
||
private static bool Enabled { | ||
get { return EditorPrefs.GetBool(PrefsKeyEnabled, true); } | ||
} | ||
private static AutoSceneSettings settings = null; | ||
|
||
static AutoScene() { | ||
settings = AutoSceneSettings.LoadSettings(); | ||
|
||
EditorBuildSettings.sceneListChanged += UpdatePlayModeStartScene; | ||
UpdatePlayModeStartScene(); | ||
} | ||
|
||
private static void UpdatePlayModeStartScene() { | ||
SceneAsset sceneAsset = null; | ||
|
||
if (Enabled) { | ||
string value = EditorPrefs.GetString(PrefsKey, "none"); | ||
if (value == "auto") { | ||
if (settings.Enabled) { | ||
if (settings.LoadedScene == "auto") { | ||
foreach (EditorBuildSettingsScene scene in EditorBuildSettings.scenes) { | ||
if (scene.enabled) { | ||
sceneAsset = AssetDatabase.LoadAssetAtPath<SceneAsset>(scene.path); | ||
break; | ||
} | ||
} | ||
} | ||
else if (value != "none") { | ||
sceneAsset = AssetDatabase.LoadAssetAtPath<SceneAsset>(value); | ||
else if (settings.LoadedScene != "none") { | ||
sceneAsset = AssetDatabase.LoadAssetAtPath<SceneAsset>(settings.LoadedScene); | ||
} | ||
} | ||
|
||
EditorSceneManager.playModeStartScene = sceneAsset; | ||
} | ||
|
||
[MenuItem("Tools/AutoScene/Disable")] | ||
[MenuItem("Tools/AutoScene (" + AutoSceneSettings.VERSION + ")/Disable")] | ||
private static void DisableAutoScene() { | ||
EditorPrefs.SetBool(PrefsKeyEnabled, false); | ||
settings.Enabled = false; | ||
UpdatePlayModeStartScene(); | ||
} | ||
|
||
[MenuItem("Tools/AutoScene/Disable", true)] | ||
[MenuItem("Tools/AutoScene (" + AutoSceneSettings.VERSION + ")/Disable", true)] | ||
private static bool CanDisableAutoScene() { | ||
return Enabled; | ||
return settings.Enabled; | ||
} | ||
|
||
[MenuItem("Tools/AutoScene/Enable")] | ||
[MenuItem("Tools/AutoScene (" + AutoSceneSettings.VERSION + ")/Enable")] | ||
private static void EnableAutoScene() { | ||
EditorPrefs.SetBool(PrefsKeyEnabled, true); | ||
settings.Enabled = true; | ||
UpdatePlayModeStartScene(); | ||
} | ||
|
||
[MenuItem("Tools/AutoScene/Enable", true)] | ||
[MenuItem("Tools/AutoScene (" + AutoSceneSettings.VERSION + ")/Enable", true)] | ||
private static bool CanEnableAutoScene() { | ||
return !Enabled; | ||
return !settings.Enabled; | ||
} | ||
|
||
[PreferenceItem("AutoScene")] | ||
public static void AutoScenePreferences() { | ||
string prefsValue = EditorPrefs.GetString(PrefsKey, "none"); | ||
[SettingsProvider] | ||
public static SettingsProvider AutoSceneSettingsProvider() { | ||
SettingsProvider provider = new SettingsProvider("AutoScene", SettingsScope.User) { | ||
keywords = new HashSet<string>(new[] { "scene", "autoscene", "play mode" }), | ||
|
||
EditorGUILayout.LabelField("Version", VERSION, EditorStyles.boldLabel); | ||
EditorGUILayout.Space(); | ||
guiHandler = (searchContext) => { | ||
EditorGUILayout.LabelField("Version", AutoSceneSettings.VERSION, EditorStyles.boldLabel); | ||
EditorGUILayout.Space(); | ||
|
||
// Build scene list | ||
string[] sceneGuids = AssetDatabase.FindAssets("t:Scene"); | ||
string[] scenePathes = new string[sceneGuids.Length]; | ||
for (int i = 0; i < sceneGuids.Length; i++) { | ||
scenePathes[i] = AssetDatabase.GUIDToAssetPath(sceneGuids[i]); | ||
} | ||
Array.Sort(scenePathes, string.Compare); | ||
// Build scene list | ||
string[] sceneGuids = AssetDatabase.FindAssets("t:Scene"); | ||
string[] scenePathes = new string[sceneGuids.Length]; | ||
for (int i = 0; i < sceneGuids.Length; i++) { | ||
scenePathes[i] = AssetDatabase.GUIDToAssetPath(sceneGuids[i]); | ||
} | ||
Array.Sort(scenePathes, string.Compare); | ||
|
||
// Finding selected index | ||
int selectedIndex = 0; | ||
if (prefsValue == "auto") { | ||
selectedIndex = 1; | ||
} | ||
else { | ||
int arrayIndex = Array.IndexOf(scenePathes, prefsValue); | ||
if (arrayIndex >= 0) { | ||
selectedIndex = arrayIndex + 2; | ||
} | ||
} | ||
// Finding selected index | ||
string prefsValue = settings.LoadedScene; | ||
int selectedIndex = 0; | ||
if (prefsValue == "auto") { | ||
selectedIndex = 1; | ||
} | ||
else { | ||
int arrayIndex = Array.IndexOf(scenePathes, prefsValue); | ||
if (arrayIndex >= 0) { | ||
selectedIndex = arrayIndex + 2; | ||
} | ||
} | ||
|
||
string[] menuEntries = new string[scenePathes.Length + 2]; | ||
menuEntries[0] = "None"; | ||
menuEntries[1] = "Auto"; | ||
Array.Copy(scenePathes, 0, menuEntries, 2, scenePathes.Length); | ||
string[] menuEntries = new string[scenePathes.Length + 2]; | ||
menuEntries[0] = "None"; | ||
menuEntries[1] = "Auto"; | ||
Array.Copy(scenePathes, 0, menuEntries, 2, scenePathes.Length); | ||
|
||
EditorGUI.BeginChangeCheck(); | ||
|
||
bool enabled = Enabled; | ||
enabled = EditorGUILayout.Toggle("Enable AutoScene", enabled); | ||
EditorGUILayout.Space(); | ||
EditorGUI.BeginChangeCheck(); | ||
|
||
selectedIndex = EditorGUILayout.Popup("Scene to load on Play", selectedIndex, menuEntries); | ||
bool enabled = settings.Enabled; | ||
enabled = EditorGUILayout.Toggle("Enable AutoScene", enabled); | ||
EditorGUILayout.Space(); | ||
|
||
if (EditorGUI.EndChangeCheck()) { | ||
if (selectedIndex == 0) { | ||
prefsValue = "none"; | ||
} | ||
else if (selectedIndex == 1) { | ||
prefsValue = "auto"; | ||
} | ||
else { | ||
prefsValue = menuEntries[selectedIndex]; | ||
} | ||
selectedIndex = EditorGUILayout.Popup("Scene to load on Play", selectedIndex, menuEntries); | ||
|
||
EditorPrefs.SetString(PrefsKey, prefsValue); | ||
EditorPrefs.SetBool(PrefsKeyEnabled, enabled); | ||
UpdatePlayModeStartScene(); | ||
} | ||
if (EditorGUI.EndChangeCheck()) { | ||
if (selectedIndex == 0) { | ||
prefsValue = "none"; | ||
} | ||
else if (selectedIndex == 1) { | ||
prefsValue = "auto"; | ||
} | ||
else { | ||
prefsValue = menuEntries[selectedIndex]; | ||
} | ||
|
||
string helpBoxMessage; | ||
if (selectedIndex == 0) { | ||
helpBoxMessage = "The scenes currently loaded in the editor will be maintained when entering Play mode.\n\nThis is the default Unity behaviour."; | ||
} | ||
else if (selectedIndex == 1) { | ||
helpBoxMessage = "The first enabled scene in the Build Settings box will be loaded when entering Play mode. If no such scene exists, falls back to 'None'."; | ||
} | ||
else { | ||
helpBoxMessage = string.Format("The scene '{0}' will be loaded when entring Play mode. If the scene does not exist anymore, falls back to 'None'.", prefsValue); | ||
} | ||
EditorGUILayout.Space(); | ||
EditorGUILayout.HelpBox(helpBoxMessage, MessageType.Info, true); | ||
settings.LoadedScene = prefsValue; | ||
settings.Enabled = enabled; | ||
UpdatePlayModeStartScene(); | ||
} | ||
|
||
string helpBoxMessage; | ||
if (selectedIndex == 0) { | ||
helpBoxMessage = "The scenes currently loaded in the editor will be maintained when entering Play mode.\n\nThis is the default Unity behaviour."; | ||
} | ||
else if (selectedIndex == 1) { | ||
helpBoxMessage = "The first enabled scene in the Build Settings box will be loaded when entering Play mode. If no such scene exists, falls back to 'None'."; | ||
} | ||
else { | ||
helpBoxMessage = string.Format("The scene '{0}' will be loaded when entring Play mode. If the scene does not exist anymore, falls back to 'None'.", prefsValue); | ||
} | ||
EditorGUILayout.Space(); | ||
EditorGUILayout.HelpBox(helpBoxMessage, MessageType.Info, true); | ||
|
||
EditorGUILayout.Space(); | ||
EditorGUILayout.LabelField("Made with ❤️ by chsxf"); | ||
EditorGUILayout.Space(); | ||
EditorGUILayout.LabelField("Made with ❤️ by chsxf"); | ||
} | ||
}; | ||
return provider; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using UnityEngine; | ||
using UnityEditor; | ||
using System; | ||
using System.IO; | ||
|
||
namespace chsxf | ||
{ | ||
[Serializable] | ||
public class AutoSceneSettings | ||
{ | ||
public const string VERSION = "1.2.0"; | ||
|
||
[SerializeField] private string version = VERSION; | ||
public string Version { get { return version; } } | ||
|
||
[SerializeField] private bool enabled = true; | ||
public bool Enabled { | ||
get { return enabled; } | ||
set { | ||
if (enabled != value) { | ||
enabled = value; | ||
SaveSettings(); | ||
} | ||
} | ||
} | ||
|
||
[SerializeField] private string loadedScene = "none"; | ||
public string LoadedScene { | ||
get { return loadedScene; } | ||
set { | ||
if (loadedScene != value) { | ||
loadedScene = value; | ||
SaveSettings(); | ||
} | ||
} | ||
} | ||
|
||
private static string ProjectPath { | ||
get { | ||
string assetsPath = Application.dataPath; | ||
return Directory.GetParent(assetsPath).FullName; | ||
} | ||
} | ||
|
||
private static string PrefsKey { | ||
get { return ProjectPath + ".AutoScene"; } | ||
} | ||
|
||
private static string LegacyPrefsKeyEnabled { | ||
get { return PrefsKey + ".enabled"; } | ||
} | ||
|
||
private void SaveSettings() { | ||
string prefs = JsonUtility.ToJson(this); | ||
EditorPrefs.SetString(PrefsKey, prefs); | ||
} | ||
|
||
public static AutoSceneSettings LoadSettings() { | ||
string prefs = EditorPrefs.GetString(PrefsKey, null); | ||
bool hasKey = string.IsNullOrEmpty(prefs); | ||
bool isLegacy = (hasKey && !prefs.StartsWith("{")); | ||
if (!hasKey || isLegacy) { | ||
AutoSceneSettings settings = new AutoSceneSettings(); | ||
if (isLegacy) { | ||
settings.enabled = EditorPrefs.GetBool(LegacyPrefsKeyEnabled, true); | ||
settings.loadedScene = prefs; | ||
} | ||
settings.SaveSettings(); | ||
if (isLegacy) { | ||
EditorPrefs.DeleteKey(LegacyPrefsKeyEnabled); | ||
} | ||
return settings; | ||
} | ||
return JsonUtility.FromJson<AutoSceneSettings>(prefs); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters