Skip to content

Commit

Permalink
Merge branch 'dev-1.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
chsxf committed Jun 30, 2021
2 parents 9d3a939 + 9f25420 commit 73c268a
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 99 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
## [1.2.0] - 2021-06-30

### Changed

- Changed minimum requirement to Unity 2018.4
- Migrated [PreferenceItem](https://docs.unity3d.com/ScriptReference/PreferenceItem.html) to [SettingsProvider](https://docs.unity3d.com/ScriptReference/SettingsProvider.html)
- Refactored settings storage

## [1.1.0] - 2019-03-27

Expand Down
181 changes: 84 additions & 97 deletions Editor/AutoScene.cs
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;
}
}
}
77 changes: 77 additions & 0 deletions Editor/AutoSceneSettings.cs
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);
}
}
}
11 changes: 11 additions & 0 deletions Editor/AutoSceneSettings.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "chsxf.autoscene",
"version": "1.1.0",
"version": "1.2.0",
"displayName": "AutoScene",
"description": "AutoScene allows you to work on your Unity scene setup in the editor while loading a specific scene when entering Play mode.",
"unity": "2018.4",
Expand Down

0 comments on commit 73c268a

Please sign in to comment.