├── .github
├── .gitignore
├── CHANGELOG.md
├── LICENSE
└── README.md
├── Scripts.meta
├── Scripts
├── Editor.meta
├── Editor
│ ├── SceneData_Editor.cs
│ ├── SceneData_Editor.cs.meta
│ ├── SceneData_Icon.png
│ ├── SceneData_Icon.png.meta
│ ├── zeshmouttsassets.unityscenesasscriptableobjects.editor.asmdef
│ └── zeshmouttsassets.unityscenesasscriptableobjects.editor.asmdef.meta
├── SceneData.cs
└── SceneData.cs.meta
├── package.json
├── package.json.meta
├── zeshmouttsassets.unityscenesasscriptableobjects.asmdef
└── zeshmouttsassets.unityscenesasscriptableobjects.asmdef.meta
/.github/.gitignore:
--------------------------------------------------------------------------------
1 | # This .gitignore file should be placed at the root of your Unity project directory
2 | #
3 | # Get latest from https://github.com/github/gitignore/blob/master/Unity.gitignore
4 | #
5 | /[Ll]ibrary/
6 | /[Tt]emp/
7 | /[Oo]bj/
8 | /[Bb]uild/
9 | /[Bb]uilds/
10 | /[Ll]ogs/
11 | /[Mm]emoryCaptures/
12 |
13 | # Never ignore Asset meta data
14 | !/[Aa]ssets/**/*.meta
15 |
16 | # Uncomment this line if you wish to ignore the asset store tools plugin
17 | # /[Aa]ssets/AssetStoreTools*
18 |
19 | # TextMesh Pro files
20 | [Aa]ssets/TextMesh*Pro/
21 |
22 | # Autogenerated Jetbrains Rider plugin
23 | [Aa]ssets/Plugins/Editor/JetBrains*
24 |
25 | # Visual Studio cache directory
26 | .vs/
27 |
28 | # Gradle cache directory
29 | .gradle/
30 |
31 | # Autogenerated VS/MD/Consulo solution and project files
32 | ExportedObj/
33 | .consulo/
34 | *.csproj
35 | *.unityproj
36 | *.sln
37 | *.suo
38 | *.tmp
39 | *.user
40 | *.userprefs
41 | *.pidb
42 | *.booproj
43 | *.svd
44 | *.pdb
45 | *.mdb
46 | *.opendb
47 | *.VC.db
48 |
49 | # Unity3D generated meta files
50 | *.pidb.meta
51 | *.pdb.meta
52 | *.mdb.meta
53 |
54 | # Unity3D generated file on crash reports
55 | sysinfo.txt
56 |
57 | # Builds
58 | *.apk
59 | *.unitypackage
60 |
61 | # Crashlytics generated file
62 | crashlytics-build.properties
63 |
64 |
--------------------------------------------------------------------------------
/.github/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # Changelog
2 | All notable changes to this package will be documented in this file.
3 |
4 | The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/), and this project *somewhat* adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
5 |
6 | ## [1.0.0] - 2020-06-01
7 | ### First release
8 | - Added the required parts to turn it into a valid package
9 | - Updated README.md
--------------------------------------------------------------------------------
/.github/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2018
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/.github/README.md:
--------------------------------------------------------------------------------
1 | # Unity Scenes as ScriptableObjects
2 |
3 | A way of storing scenes in a ScriptableObject.
4 |
5 | ## Uses and restrictions
6 |
7 | As per the MIT license, this is pretty much unrestricted in use and modification.
8 |
9 | I'd appreciate a lot if you could mention me somewhere if you use it, though.
10 |
11 | ## Links
12 |
13 | - [License](LICENSE)
14 | - [Changelog](CHANGELOG.md)
15 |
16 | ## Installation
17 |
18 | - Unity 2019 or newer : Open the Package manager, select and import the package with the git URL.
19 | - Unity 2018 or older : Clone/download the repo and put it in your Unity project's assets.
20 |
21 | -----
22 |
23 | ## Why would I need to store a scene in a ScriptableObject ?
24 |
25 | In Unity, you can load a scene by either its name, its path or its build index - so the common way of picking the scene is by adding a `public int` or a `public string` to your script and load from that.
26 |
27 | Six months later, you have dozens of scenes and dozens of different scripts loading scenes. If you need to change a single scene (remove it from the build settings, rename it, whatever), you *will* forget to do the corresponding change somewhere, resulting in incorrect scenes being loaded or trying to load a scene that doesn't even exist anymore.
28 |
29 | By using my SceneData, you can directly reference the SceneData anywhere you need it, and let it do the work of figuring where the scene is in your built game. It doesn't care about you changing the name, path or build index : as long as you tell it which scene it must point to, it *will* find it.
30 |
31 | With all that free time you suddenly get from no longer having to debug every single scene, you can now focus on important stuff like how terrible Unity's default way of loading scenes is.
32 |
33 | ## How to create a Scene ScriptableObject
34 |
35 | As usual with ScriptableObjects :
36 |
37 | * Using Unity's menu bar : `Assets/Create/ZeShmoutt's Assets/Data Containers/Scene Data`
38 | * In the Project window : `Create/ZeShmoutt's Assets/Data Containers/Scene Data`
39 | * As a context menu on a SceneAsset : `Create/ZeShmoutt's Assets/Data Containers/Scene Data from SceneAsset`
40 |
41 | ## How to use in your scripts
42 |
43 | **Step 1 :** Add `using ZeShmouttsAssets.DataContainers;` at the top with the other `using`s.
44 |
45 | **Step 2 :** Directly assign a scene on the ScriptableObject, while the Editor script automatically saves the corresponding name, path and build index.
46 |
47 | **Step 3 :** You can then use one of the `LoadAsync` methods directly from the ScriptableObject like you'd do with `SceneManagement` : `LoadAsyncFromName()`, `LoadAsyncFromPath()`, and `LoadAsyncFromBuildIndex()`. As a "default" option, `LoadAsync()` is a shortcut that will attempt to use the build index, or the scene's path if the build index is unavailable or invalid. You can also directly get all three values (name, path, or build index) through public properties : `SceneName`, `ScenePath`, and `SceneIndex`.
48 |
49 | **IMPORTANT :** If no scene is assigned, `SceneName` and `ScenePath` return `null` as expected, but `SceneIndex` returns `-2`, because as explained [here](https://docs.unity3d.com/ScriptReference/SceneManagement.Scene-buildIndex.html), `Scene.buildIndex` already returns `-1` if the scene comes from an AssetBundle. `SceneIndex` will also return `-2` if the scene is assigned but not in the build settings.
50 |
--------------------------------------------------------------------------------
/Scripts.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: 476ff7cc29b3bdd45822d8dd6933c949
3 | folderAsset: yes
4 | DefaultImporter:
5 | externalObjects: {}
6 | userData:
7 | assetBundleName:
8 | assetBundleVariant:
9 |
--------------------------------------------------------------------------------
/Scripts/Editor.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: e15a03629a684e74baa79f181e59554a
3 | folderAsset: yes
4 | DefaultImporter:
5 | externalObjects: {}
6 | userData:
7 | assetBundleName:
8 | assetBundleVariant:
9 |
--------------------------------------------------------------------------------
/Scripts/Editor/SceneData_Editor.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using System.Reflection;
3 | using UnityEditor;
4 | using UnityEngine;
5 |
6 | namespace ZeShmouttsAssets.DataContainers.EditorScripts
7 | {
8 | [CustomEditor(typeof(SceneData))]
9 | [CanEditMultipleObjects]
10 | public class SceneData_Editor : Editor
11 | {
12 | #region Context Menu
13 |
14 | private const string MenuItemPath = "Assets/Create/ZeShmoutt's Assets/Data Containers/Scene Data from SceneAsset";
15 |
16 | [MenuItem(MenuItemPath, false, 10)]
17 | private static void CreateFromSceneObject()
18 | {
19 | Object file = Selection.activeObject;
20 | if (file is SceneAsset)
21 | {
22 | CreateSceneData(file as SceneAsset);
23 | }
24 | else
25 | {
26 | Debug.LogError("Selected Object isn't a SceneAsset.");
27 | }
28 | }
29 |
30 | [MenuItem(MenuItemPath, true, 10)]
31 | private static bool IsSceneAsset()
32 | {
33 | Object file = Selection.activeObject;
34 | return (file is SceneAsset);
35 | }
36 |
37 | ///
38 | /// Creates a new SceneData based on an existing SceneAsset in the same folder.
39 | ///
40 | /// SceneAsset used as a reference.
41 | private static void CreateSceneData(SceneAsset scene)
42 | {
43 | SceneData assetObject = ScriptableObject.CreateInstance();
44 |
45 | BindingFlags bindFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static;
46 | FieldInfo field = typeof(SceneData).GetField("scene", bindFlags);
47 | field.SetValue(assetObject, scene);
48 |
49 | string scenePath = AssetDatabase.GetAssetPath(scene);
50 | string folderPath = scenePath.Substring(0, scenePath.LastIndexOf('/'));
51 | string assetPath = string.Format("{0}/{1}_Data.asset", folderPath, scene.name);
52 |
53 | AssetDatabase.CreateAsset(assetObject, assetPath);
54 | AssetDatabase.SaveAssets();
55 |
56 | EditorUtility.FocusProjectWindow();
57 |
58 | Selection.activeObject = assetObject;
59 | }
60 |
61 | #endregion
62 |
63 | #region Variables
64 |
65 | private SceneData script;
66 |
67 | SerializedProperty sceneObject;
68 | SerializedProperty sceneName;
69 | SerializedProperty sceneIndex;
70 | SerializedProperty scenePath;
71 |
72 | #endregion
73 |
74 | #region Unity Editor GUI
75 |
76 | private void OnEnable()
77 | {
78 | script = (SceneData)target;
79 |
80 | sceneObject = serializedObject.FindProperty("scene");
81 | sceneName = serializedObject.FindProperty("sceneName");
82 | sceneIndex = serializedObject.FindProperty("sceneIndex");
83 | scenePath = serializedObject.FindProperty("scenePath");
84 |
85 | EditorBuildSettings.sceneListChanged += HandleExternalSceneListChange;
86 | }
87 |
88 | public override void OnInspectorGUI()
89 | {
90 | serializedObject.Update();
91 |
92 | DrawScriptField();
93 |
94 | EditorGUILayout.Space();
95 |
96 | UIScene();
97 |
98 | if (GUI.changed)
99 | {
100 | Undo.RecordObject(target, "Field change");
101 | serializedObject.ApplyModifiedProperties();
102 | }
103 | }
104 |
105 | private void OnDisable()
106 | {
107 | EditorBuildSettings.sceneListChanged -= HandleExternalSceneListChange;
108 | }
109 |
110 | private void HandleExternalSceneListChange()
111 | {
112 | script.OnBeforeSerialize();
113 | Repaint();
114 | }
115 |
116 | #endregion
117 |
118 | #region Parts
119 |
120 | ///
121 | /// Draws a script field similar to the regular inspector, allowing quick access to the script itself.
122 | ///
123 | protected void DrawScriptField()
124 | {
125 | bool wasEnabled = GUI.enabled;
126 |
127 | GUI.enabled = false;
128 | SerializedProperty prop = serializedObject.FindProperty("m_Script");
129 | EditorGUILayout.PropertyField(prop, true);
130 | GUI.enabled = wasEnabled;
131 | }
132 |
133 | ///
134 | /// Parent for drawing editor settings.
135 | ///
136 | void UIScene()
137 | {
138 | bool sceneNotNull = sceneObject.objectReferenceValue != null;
139 |
140 | EditorGUILayout.BeginVertical(GUI.skin.box);
141 |
142 | EditorGUILayout.PropertyField(sceneObject, false);
143 |
144 | script.OnBeforeSerialize();
145 |
146 | EditorGUI.indentLevel++;
147 |
148 | bool wasEnabled = GUI.enabled;
149 |
150 | GUI.enabled = false;
151 |
152 | string pathValue = scenePath.stringValue;
153 | string nameValue = sceneName.stringValue;
154 | int indexValue = sceneIndex.intValue;
155 |
156 | EditorGUILayout.LabelField("Path", sceneNotNull ? pathValue : "N/A");
157 |
158 | EditorGUILayout.LabelField("Name", sceneNotNull ? nameValue : "N/A");
159 |
160 | EditorGUILayout.LabelField("Build Index", sceneNotNull && indexValue >= -1 ? indexValue.ToString() : "N/A");
161 |
162 | GUI.enabled = wasEnabled;
163 |
164 | EditorGUI.indentLevel--;
165 |
166 | EditorGUILayout.EndVertical();
167 |
168 | if (sceneNotNull)
169 | {
170 | if (indexValue < 0)
171 | {
172 | NotInBuildSettings(pathValue);
173 | }
174 | else if (indexValue >= 0 && EditorBuildSettings.scenes.Length >= indexValue + 1 && !EditorBuildSettings.scenes[indexValue].enabled)
175 | {
176 | DisabledInBuildSettings(sceneIndex.intValue);
177 | }
178 | }
179 | else
180 | {
181 | EditorGUILayout.HelpBox("No scene selected.", MessageType.Error);
182 | }
183 | }
184 |
185 | ///
186 | /// Warning info label + button drawn when the selected scene isn't in the build settings.
187 | ///
188 | void NotInBuildSettings(string path)
189 | {
190 | EditorGUILayout.BeginHorizontal();
191 |
192 | EditorGUILayout.HelpBox("The selected scene is not in the build settings.", MessageType.Warning);
193 | if (GUILayout.Button("Add to build", GUILayout.Width(100), GUILayout.Height(40)))
194 | {
195 | EditorBuildSettingsScene sceneToAdd = new EditorBuildSettingsScene(path, true);
196 | List scenesInBuild = new List(EditorBuildSettings.scenes);
197 | scenesInBuild.Add(sceneToAdd);
198 | EditorBuildSettings.scenes = scenesInBuild.ToArray();
199 |
200 | Debug.LogFormat(sceneObject.objectReferenceValue, "Scene '{0}' has been added to build settings at index {1}.", path, scenesInBuild.Count - 1);
201 | }
202 |
203 | EditorGUILayout.EndHorizontal();
204 | }
205 |
206 | ///
207 | /// Warning info label + button drawn when the selected scene is disabled in the build settings.
208 | ///
209 | void DisabledInBuildSettings(int index)
210 | {
211 | EditorGUILayout.BeginHorizontal();
212 |
213 | EditorGUILayout.HelpBox("The selected scene is disabled in the build settings.", MessageType.Warning);
214 | if (GUILayout.Button("Enable in build", GUILayout.Width(100), GUILayout.Height(40)))
215 | {
216 | EditorBuildSettingsScene[] buildSettingsScenes = EditorBuildSettings.scenes;
217 | buildSettingsScenes[index].enabled = true;
218 | EditorBuildSettings.scenes = buildSettingsScenes;
219 |
220 | Debug.LogFormat(sceneObject.objectReferenceValue, "Scene #{0} has been enabled.", index);
221 | }
222 |
223 | EditorGUILayout.EndHorizontal();
224 | }
225 |
226 | #endregion
227 | }
228 | }
--------------------------------------------------------------------------------
/Scripts/Editor/SceneData_Editor.cs.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: aa487084c5256014f8f4761cac2fc1b3
3 | MonoImporter:
4 | externalObjects: {}
5 | serializedVersion: 2
6 | defaultReferences: []
7 | executionOrder: 0
8 | icon: {instanceID: 0}
9 | userData:
10 | assetBundleName:
11 | assetBundleVariant:
12 |
--------------------------------------------------------------------------------
/Scripts/Editor/SceneData_Icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ZeShmoutt/Unity-Scene-ScriptableObject/a06bd1af7dd1443be87d5ed8389ea5342d5cd854/Scripts/Editor/SceneData_Icon.png
--------------------------------------------------------------------------------
/Scripts/Editor/SceneData_Icon.png.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: 26eecbd1537bd9a42a31eb4765c4769e
3 | TextureImporter:
4 | fileIDToRecycleName: {}
5 | externalObjects: {}
6 | serializedVersion: 9
7 | mipmaps:
8 | mipMapMode: 0
9 | enableMipMap: 0
10 | sRGBTexture: 1
11 | linearTexture: 0
12 | fadeOut: 0
13 | borderMipMap: 0
14 | mipMapsPreserveCoverage: 0
15 | alphaTestReferenceValue: 0.5
16 | mipMapFadeDistanceStart: 1
17 | mipMapFadeDistanceEnd: 3
18 | bumpmap:
19 | convertToNormalMap: 0
20 | externalNormalMap: 0
21 | heightScale: 0.25
22 | normalMapFilter: 0
23 | isReadable: 0
24 | streamingMipmaps: 0
25 | streamingMipmapsPriority: 0
26 | grayScaleToAlpha: 0
27 | generateCubemap: 6
28 | cubemapConvolution: 0
29 | seamlessCubemap: 0
30 | textureFormat: 1
31 | maxTextureSize: 2048
32 | textureSettings:
33 | serializedVersion: 2
34 | filterMode: -1
35 | aniso: 1
36 | mipBias: -100
37 | wrapU: 1
38 | wrapV: 1
39 | wrapW: -1
40 | nPOTScale: 0
41 | lightmap: 0
42 | compressionQuality: 50
43 | spriteMode: 0
44 | spriteExtrude: 1
45 | spriteMeshType: 1
46 | alignment: 0
47 | spritePivot: {x: 0.5, y: 0.5}
48 | spritePixelsToUnits: 100
49 | spriteBorder: {x: 0, y: 0, z: 0, w: 0}
50 | spriteGenerateFallbackPhysicsShape: 1
51 | alphaUsage: 1
52 | alphaIsTransparency: 1
53 | spriteTessellationDetail: -1
54 | textureType: 2
55 | textureShape: 1
56 | singleChannelComponent: 0
57 | maxTextureSizeSet: 0
58 | compressionQualitySet: 0
59 | textureFormatSet: 0
60 | platformSettings:
61 | - serializedVersion: 2
62 | buildTarget: DefaultTexturePlatform
63 | maxTextureSize: 128
64 | resizeAlgorithm: 0
65 | textureFormat: -1
66 | textureCompression: 1
67 | compressionQuality: 50
68 | crunchedCompression: 0
69 | allowsAlphaSplitting: 0
70 | overridden: 0
71 | androidETC2FallbackOverride: 0
72 | - serializedVersion: 2
73 | buildTarget: Standalone
74 | maxTextureSize: 128
75 | resizeAlgorithm: 0
76 | textureFormat: -1
77 | textureCompression: 1
78 | compressionQuality: 50
79 | crunchedCompression: 0
80 | allowsAlphaSplitting: 0
81 | overridden: 0
82 | androidETC2FallbackOverride: 0
83 | - serializedVersion: 2
84 | buildTarget: Android
85 | maxTextureSize: 128
86 | resizeAlgorithm: 0
87 | textureFormat: -1
88 | textureCompression: 1
89 | compressionQuality: 50
90 | crunchedCompression: 0
91 | allowsAlphaSplitting: 0
92 | overridden: 0
93 | androidETC2FallbackOverride: 0
94 | - serializedVersion: 2
95 | buildTarget: WebGL
96 | maxTextureSize: 128
97 | resizeAlgorithm: 0
98 | textureFormat: -1
99 | textureCompression: 1
100 | compressionQuality: 50
101 | crunchedCompression: 0
102 | allowsAlphaSplitting: 0
103 | overridden: 0
104 | androidETC2FallbackOverride: 0
105 | spriteSheet:
106 | serializedVersion: 2
107 | sprites: []
108 | outline: []
109 | physicsShape: []
110 | bones: []
111 | spriteID:
112 | vertices: []
113 | indices:
114 | edges: []
115 | weights: []
116 | spritePackingTag:
117 | pSDRemoveMatte: 0
118 | pSDShowRemoveMatteOption: 0
119 | userData:
120 | assetBundleName:
121 | assetBundleVariant:
122 |
--------------------------------------------------------------------------------
/Scripts/Editor/zeshmouttsassets.unityscenesasscriptableobjects.editor.asmdef:
--------------------------------------------------------------------------------
1 | {
2 | "name": "Unity Scenes as ScriptableObjects (Editor)",
3 | "references": [
4 | "GUID:ce447086f33e03349a94957d9b19d39b"
5 | ],
6 | "includePlatforms": [
7 | "Editor"
8 | ],
9 | "excludePlatforms": [],
10 | "allowUnsafeCode": false,
11 | "overrideReferences": false,
12 | "precompiledReferences": [],
13 | "autoReferenced": true,
14 | "defineConstraints": [
15 | "UNITY_2018_4_OR_NEWER"
16 | ],
17 | "versionDefines": [],
18 | "noEngineReferences": false
19 | }
--------------------------------------------------------------------------------
/Scripts/Editor/zeshmouttsassets.unityscenesasscriptableobjects.editor.asmdef.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: 14fb81f9e7117804385a206b306c1d75
3 | AssemblyDefinitionImporter:
4 | externalObjects: {}
5 | userData:
6 | assetBundleName:
7 | assetBundleVariant:
8 |
--------------------------------------------------------------------------------
/Scripts/SceneData.cs:
--------------------------------------------------------------------------------
1 | using UnityEngine;
2 | using UnityEngine.SceneManagement;
3 |
4 | namespace ZeShmouttsAssets.DataContainers
5 | {
6 | [CreateAssetMenu(fileName = "New Scene Data", menuName = "ZeShmoutt's Assets/Data Containers/Scene Data")]
7 | public class SceneData : ScriptableObject, ISerializationCallbackReceiver
8 | {
9 | #region Variables
10 |
11 | #if UNITY_EDITOR
12 | #pragma warning disable 0649
13 | ///
14 | /// Editor-only scene object. Do not use in a runtime script !
15 | ///
16 | [SerializeField] private UnityEditor.SceneAsset scene;
17 | #pragma warning restore 0649
18 | #endif
19 |
20 | ///
21 | /// The scene's name.
22 | ///
23 | public string SceneName { get { return sceneName; } }
24 |
25 | ///
26 | /// The scene's build index.
27 | ///
28 | public int SceneIndex { get { return sceneIndex; } }
29 |
30 | ///
31 | /// The scene's path.
32 | ///
33 | public string ScenePath { get { return scenePath; } }
34 |
35 | ///
36 | /// Internal scene name.
37 | ///
38 | [SerializeField] private string sceneName = null;
39 |
40 | ///
41 | /// Internal scene index.
42 | ///
43 | [SerializeField] private int sceneIndex = -2;
44 |
45 | ///
46 | /// Internal scene path.
47 | ///
48 | [SerializeField] private string scenePath = null;
49 |
50 | #endregion
51 |
52 | #region Serialization Callback
53 |
54 | public void OnAfterDeserialize()
55 | {
56 |
57 | }
58 |
59 | public void OnBeforeSerialize()
60 | {
61 | #if UNITY_EDITOR
62 |
63 | string nameValue = (scene != null) ? scene.name : null;
64 | int indexValue = -2;
65 | string pathValue = (scene != null) ? UnityEditor.AssetDatabase.GetAssetPath(scene) : null;
66 |
67 | UnityEditor.EditorBuildSettingsScene[] buildSettingsScenes = UnityEditor.EditorBuildSettings.scenes;
68 | if (buildSettingsScenes.Length > 0)
69 | {
70 | for (int i = 0; i < buildSettingsScenes.Length; i++)
71 | {
72 | if (UnityEditor.EditorBuildSettings.scenes[i].path == pathValue)
73 | {
74 | indexValue = i;
75 | break;
76 | }
77 | }
78 | }
79 |
80 | sceneName = nameValue;
81 | sceneIndex = indexValue;
82 | scenePath = pathValue;
83 |
84 | #endif
85 | }
86 |
87 | #endregion
88 |
89 | #region Load From Default
90 |
91 | ///
92 | /// Shortcut to load the scene asynchronously in the background.
93 | ///
94 | /// If LoadSceneMode.Single then all current Scenes will be unloaded before loading.
95 | /// Use the AsyncOperation to determine if the operation has completed.
96 | public AsyncOperation LoadAsync(LoadSceneMode mode)
97 | {
98 | if (sceneIndex >= 0)
99 | {
100 | return LoadAsyncFromBuildIndex(mode);
101 | }
102 | else
103 | {
104 | return LoadAsyncFromPath(mode);
105 | }
106 | }
107 |
108 | ///
109 | /// Shortcut to load the Scene asynchronously in the background.
110 | ///
111 | /// Struct that collects the various parameters into a single place except for the name and index.
112 | /// Use the AsyncOperation to determine if the operation has completed.
113 | public AsyncOperation LoadAsync(LoadSceneParameters parameters)
114 | {
115 | if (sceneIndex >= 0)
116 | {
117 | return LoadAsyncFromBuildIndex(parameters);
118 | }
119 | else
120 | {
121 | return LoadAsyncFromPath(parameters);
122 | }
123 | }
124 |
125 | #endregion
126 |
127 | #region Load From Name
128 |
129 | ///
130 | /// Shortcut to load the scene asynchronously in the background, using the scene's name.
131 | ///
132 | /// If LoadSceneMode.Single then all current Scenes will be unloaded before loading.
133 | /// Use the AsyncOperation to determine if the operation has completed.
134 | public AsyncOperation LoadAsyncFromName(LoadSceneMode mode)
135 | {
136 | return SceneManager.LoadSceneAsync(sceneName, mode);
137 | }
138 |
139 | ///
140 | /// Shortcut to load the Scene asynchronously in the background, using the scene's name.
141 | ///
142 | /// Struct that collects the various parameters into a single place except for the name and index.
143 | /// Use the AsyncOperation to determine if the operation has completed.
144 | public AsyncOperation LoadAsyncFromName(LoadSceneParameters parameters)
145 | {
146 | return SceneManager.LoadSceneAsync(sceneName, parameters);
147 | }
148 |
149 | #endregion
150 |
151 | #region Load From Path
152 |
153 | ///
154 | /// Shortcut to load the scene asynchronously in the background, using the scene's path.
155 | ///
156 | /// If LoadSceneMode.Single then all current Scenes will be unloaded before loading.
157 | /// Use the AsyncOperation to determine if the operation has completed.
158 | public AsyncOperation LoadAsyncFromPath(LoadSceneMode mode)
159 | {
160 | return SceneManager.LoadSceneAsync(scenePath, mode);
161 | }
162 |
163 | ///
164 | /// Shortcut to load the Scene asynchronously in the background, using the scene's path.
165 | ///
166 | /// Struct that collects the various parameters into a single place except for the name and index.
167 | /// Use the AsyncOperation to determine if the operation has completed.
168 | public AsyncOperation LoadAsyncFromPath(LoadSceneParameters parameters)
169 | {
170 | return SceneManager.LoadSceneAsync(scenePath, parameters);
171 | }
172 |
173 | #endregion
174 |
175 | #region Load From Build Index
176 |
177 | ///
178 | /// Shortcut to load the scene asynchronously in the background, using the scene's build index.
179 | ///
180 | /// If LoadSceneMode.Single then all current Scenes will be unloaded before loading.
181 | /// Use the AsyncOperation to determine if the operation has completed.
182 | public AsyncOperation LoadAsyncFromBuildIndex(LoadSceneMode mode)
183 | {
184 | return SceneManager.LoadSceneAsync(sceneIndex, mode);
185 | }
186 |
187 | ///
188 | /// Shortcut to load the Scene asynchronously in the background, using the scene's build index.
189 | ///
190 | /// Struct that collects the various parameters into a single place except for the name and index.
191 | /// Use the AsyncOperation to determine if the operation has completed.
192 | public AsyncOperation LoadAsyncFromBuildIndex(LoadSceneParameters parameters)
193 | {
194 | return SceneManager.LoadSceneAsync(sceneIndex, parameters);
195 | }
196 |
197 | #endregion
198 | }
199 | }
--------------------------------------------------------------------------------
/Scripts/SceneData.cs.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: 2077f3cfec81adb4f812211b4737f3e6
3 | MonoImporter:
4 | externalObjects: {}
5 | serializedVersion: 2
6 | defaultReferences: []
7 | executionOrder: 0
8 | icon: {fileID: 2800000, guid: 26eecbd1537bd9a42a31eb4765c4769e, type: 3}
9 | userData:
10 | assetBundleName:
11 | assetBundleVariant:
12 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "com.zeshmouttsassets.unityscenesasscriptableobjects",
3 | "displayName":"Unity Scenes as ScriptableObjects",
4 | "version": "1.0.0",
5 | "unity": "2018.4",
6 | "unityRelease": "15f1",
7 | "description": "Assign your Scenes into ScriptableObjects for easier handling.",
8 | "keywords": ["unity", "scripting", "tool", "tools", "C#"],
9 | "dependencies": { }
10 | }
--------------------------------------------------------------------------------
/package.json.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: 4f5e4178ba4dbd1408f2cf9d39783591
3 | TextScriptImporter:
4 | externalObjects: {}
5 | userData:
6 | assetBundleName:
7 | assetBundleVariant:
8 |
--------------------------------------------------------------------------------
/zeshmouttsassets.unityscenesasscriptableobjects.asmdef:
--------------------------------------------------------------------------------
1 | {
2 | "name": "Unity Scenes as ScriptableObjects",
3 | "references": [],
4 | "includePlatforms": [],
5 | "excludePlatforms": [],
6 | "allowUnsafeCode": false,
7 | "overrideReferences": false,
8 | "precompiledReferences": [],
9 | "autoReferenced": true,
10 | "defineConstraints": [
11 | "UNITY_2018_4_OR_NEWER"
12 | ],
13 | "versionDefines": [],
14 | "noEngineReferences": false
15 | }
--------------------------------------------------------------------------------
/zeshmouttsassets.unityscenesasscriptableobjects.asmdef.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: ce447086f33e03349a94957d9b19d39b
3 | AssemblyDefinitionImporter:
4 | externalObjects: {}
5 | userData:
6 | assetBundleName:
7 | assetBundleVariant:
8 |
--------------------------------------------------------------------------------