From eaebb4a9589dc0a1f8d5a484f86447d4a3adbeb0 Mon Sep 17 00:00:00 2001 From: Chase Pettit Date: Wed, 4 May 2016 07:39:45 -0400 Subject: [PATCH] Refactor Settings to be ScriptableObjects. --- .../AssetBundles/BuildAssetBundlesSettings.cs | 51 +++++++- Editor/Build/BuildProject.cs | 33 +---- Editor/Build/Platform/BuildPlatform.cs | 6 +- Editor/Build/Settings/BaseSettings.cs | 34 +++++ ...Generator.cs.meta => BaseSettings.cs.meta} | 4 +- Editor/Build/Settings/BuildSettings.cs | 121 ++++++++++-------- .../Build/Settings/BuildSettingsGenerator.cs | 15 --- .../Build/Settings/BuildSettingsTemplate.txt | 57 --------- .../Settings/BuildSettingsTemplate.txt.meta | 8 -- Editor/Upload/UploadItchSettings.cs | 80 ++++++++++-- 10 files changed, 223 insertions(+), 186 deletions(-) create mode 100644 Editor/Build/Settings/BaseSettings.cs rename Editor/Build/Settings/{BuildSettingsGenerator.cs.meta => BaseSettings.cs.meta} (75%) delete mode 100644 Editor/Build/Settings/BuildSettingsGenerator.cs delete mode 100644 Editor/Build/Settings/BuildSettingsTemplate.txt delete mode 100644 Editor/Build/Settings/BuildSettingsTemplate.txt.meta diff --git a/Editor/AssetBundles/BuildAssetBundlesSettings.cs b/Editor/AssetBundles/BuildAssetBundlesSettings.cs index cf6915e..63df6e1 100644 --- a/Editor/AssetBundles/BuildAssetBundlesSettings.cs +++ b/Editor/AssetBundles/BuildAssetBundlesSettings.cs @@ -1,21 +1,60 @@ using System.IO; +using UnityEditor; +using UnityEngine; namespace UnityBuild { -public class BuildAssetBundlesSettings +[InitializeOnLoad] +public class BuildAssetBundlesSettings : BaseSettings { - #region Constants + #region Singleton + + private static BuildAssetBundlesSettings instance = null; + + public static BuildAssetBundlesSettings Instance + { + get + { + if (instance == null) + { + instance = CreateAsset("BuildAssetBundlesSettings"); + } + + return instance; + } + } + + #endregion + + #region MenuItems + + [MenuItem("Build/AssetBundles/Edit Settings", priority = 0)] + public static void EditSettings() + { + Selection.activeObject = Instance; + EditorApplication.ExecuteMenuItem("Window/Inspector"); + } + + #endregion + + #region Variables + + [Header("AssetBundle Build Settings (Field Info in Tooltips)")] /// /// The path where AssetBundles are built. {0} = binPath /// - private const string _buildPath = "{0}/Bundles"; + [SerializeField] + [Tooltip("The path where AssetBundles are built. {0} = binPath")] + private string _buildPath = "{0}/Bundles"; /// /// Flag indicating if the AssetBundles should be copied into the game's data directory. /// - private const bool _copyToBuild = true; + [SerializeField] + [Tooltip("Flag indicating if the AssetBundles should be copied into the game's data directory.")] + private bool _copyToBuild = true; #endregion @@ -25,7 +64,7 @@ public static string buildPath { get { - return string.Format(_buildPath, BuildProject.settings.binPath); + return string.Format(Instance._buildPath, BuildSettings.binPath); } } @@ -33,7 +72,7 @@ public static bool copyToBuild { get { - return _copyToBuild; + return Instance._copyToBuild; } } diff --git a/Editor/Build/BuildProject.cs b/Editor/Build/BuildProject.cs index 2c438f6..70f103f 100644 --- a/Editor/Build/BuildProject.cs +++ b/Editor/Build/BuildProject.cs @@ -12,7 +12,6 @@ public static class BuildProject { #region Public Variables & Auto-Properties - public static BuildSettings settings { get; private set; } public static List platforms { get; private set; } public static List preBuildActions { get; private set; } public static List postBuildActions { get; private set; } @@ -27,12 +26,6 @@ public static class BuildProject [MenuItem("Build/Run Build", false, 1)] public static void BuildAll() { - if (settings == null) - { - Debug.LogError("No BuildSettings found. Please run Edit/Generate BuildSettings."); - return; - } - if (preBuildActions != null) preBuildActions.Sort(); @@ -77,18 +70,6 @@ private static void DisableAllPlatforms() #region Register Methods - /// - /// Register the project's BuildSettings. - /// - /// - public static void RegisterSettings(BuildSettings settings) - { - if (BuildProject.settings != null && BuildProject.settings.GetType() != settings.GetType()) - Debug.LogError("Multiple BuildSettings classes. There can be only one!"); - - BuildProject.settings = settings; - } - /// /// Register a platform that can be built. /// @@ -138,12 +119,12 @@ public static void PerformBuild(BuildPlatform platform) // Build player. Debug.Log("Building " + platform.name); FileUtil.DeleteFileOrDirectory(platform.buildPath); - BuildPipeline.BuildPlayer(settings.scenesInBuild, platform.buildPath + platform.exeName, platform.target, BuildOptions.None); + BuildPipeline.BuildPlayer(BuildSettings.scenesInBuild, platform.buildPath + platform.exeName, platform.target, BuildOptions.None); // Copy any other data. - for (int i = 0; i < settings.copyToBuild.Length; i++) + for (int i = 0; i < BuildSettings.copyToBuild.Length; i++) { - string item = settings.copyToBuild[i]; + string item = BuildSettings.copyToBuild[i]; // Log an error if file/directory does not exist. if (!File.Exists(item) && !Directory.Exists(item)) @@ -180,8 +161,6 @@ private static void SetAllBuildPlatforms(bool enabled) private static void PerformPreBuild() { - settings.PreBuild(); - if (preBuildActions != null) { for (int i = 0; i < preBuildActions.Count; i++) @@ -194,8 +173,6 @@ private static void PerformPreBuild() private static void PerformPostBuild() { - settings.PostBuild(); - if (postBuildActions != null) { for (int i = 0; i < postBuildActions.Count; i++) @@ -208,8 +185,6 @@ private static void PerformPostBuild() private static void PerformPreBuild(BuildPlatform platform) { - settings.PreBuild(platform); - if (preBuildActions != null) { for (int i = 0; i < preBuildActions.Count; i++) @@ -222,8 +197,6 @@ private static void PerformPreBuild(BuildPlatform platform) private static void PerformPostBuild(BuildPlatform platform) { - settings.PostBuild(platform); - if (postBuildActions != null) { for (int i = 0; i < postBuildActions.Count; i++) diff --git a/Editor/Build/Platform/BuildPlatform.cs b/Editor/Build/Platform/BuildPlatform.cs index bcda6b8..f5d63cb 100644 --- a/Editor/Build/Platform/BuildPlatform.cs +++ b/Editor/Build/Platform/BuildPlatform.cs @@ -107,7 +107,7 @@ public string buildPath { get { - return BuildProject.settings.binPath + Path.DirectorySeparatorChar + name + Path.DirectorySeparatorChar; + return BuildSettings.binPath + Path.DirectorySeparatorChar + name + Path.DirectorySeparatorChar; } } @@ -115,7 +115,7 @@ public string dataDirectory { get { - return buildPath + string.Format(dataDirNameFormat, BuildProject.settings.binName) + Path.DirectorySeparatorChar; + return buildPath + string.Format(dataDirNameFormat, BuildSettings.binName) + Path.DirectorySeparatorChar; } } @@ -123,7 +123,7 @@ public string exeName { get { - return string.Format(binaryNameFormat, BuildProject.settings.binName); + return string.Format(binaryNameFormat, BuildSettings.binName); } } diff --git a/Editor/Build/Settings/BaseSettings.cs b/Editor/Build/Settings/BaseSettings.cs new file mode 100644 index 0000000..4279dd0 --- /dev/null +++ b/Editor/Build/Settings/BaseSettings.cs @@ -0,0 +1,34 @@ +using UnityEngine; +using UnityEditor; +using System.IO; + +namespace UnityBuild +{ + +public class BaseSettings : ScriptableObject +{ + protected const string SettingsPath = "Assets/Editor Default Resources/UnityBuildSettings/"; + + protected static T CreateAsset(string assetName) where T : BaseSettings + { + T instance = EditorGUIUtility.Load("UnityBuildSettings/" + assetName + ".asset") as T; + if (instance == null) + { + Debug.Log("UnityBuild: Creating settings file - " + SettingsPath + assetName + ".asset"); + instance = CreateInstance(); + instance.name = assetName; + + if (!Directory.Exists("Assets/Editor Default Resources")) + AssetDatabase.CreateFolder("Assets", "Editor Default Resources"); + + if (!Directory.Exists("Assets/Editor Default Resources/UnityBuildSettings")) + AssetDatabase.CreateFolder("Assets/Editor Default Resources", "UnityBuildSettings"); + + AssetDatabase.CreateAsset(instance, SettingsPath + assetName + ".asset"); + } + + return instance; + } +} + +} \ No newline at end of file diff --git a/Editor/Build/Settings/BuildSettingsGenerator.cs.meta b/Editor/Build/Settings/BaseSettings.cs.meta similarity index 75% rename from Editor/Build/Settings/BuildSettingsGenerator.cs.meta rename to Editor/Build/Settings/BaseSettings.cs.meta index e4ad431..9b662f9 100644 --- a/Editor/Build/Settings/BuildSettingsGenerator.cs.meta +++ b/Editor/Build/Settings/BaseSettings.cs.meta @@ -1,6 +1,6 @@ fileFormatVersion: 2 -guid: 417d0d29ac079f5458abf8a246d95806 -timeCreated: 1460654943 +guid: 91440c4539ce71046bb765fbdf89cd40 +timeCreated: 1462357368 licenseType: Pro MonoImporter: serializedVersion: 2 diff --git a/Editor/Build/Settings/BuildSettings.cs b/Editor/Build/Settings/BuildSettings.cs index 71b6d76..478dcfd 100644 --- a/Editor/Build/Settings/BuildSettings.cs +++ b/Editor/Build/Settings/BuildSettings.cs @@ -2,91 +2,100 @@ using System.IO; using System.Reflection; using UnityEditor; +using UnityEngine; namespace UnityBuild { [InitializeOnLoad] -public abstract class BuildSettings +public class BuildSettings : BaseSettings { - #region Abstract + #region Singleton - /// - /// The name of executable file (e.g. mygame.exe, mygame.app) - /// - public abstract string binName { get; } + private static BuildSettings instance = null; - /// - /// The base path where builds are output. Relative to the Unity project's base folder. - /// - public abstract string binPath { get; } - - /// - /// A list of scenes to include in the build. The first listed scene will be loaded first. - /// - public abstract string[] scenesInBuild { get; } + public static BuildSettings Instance + { + get + { + if (instance == null) + { + instance = CreateAsset("BuildSettings"); + } - /// - /// A list of files/directories to include with the build. Relative to the Unity project's base folder. - /// - public abstract string[] copyToBuild { get; } + return instance; + } + } #endregion - #region Contructor + #region MenuItems - /// - /// Constructor - /// - static BuildSettings() + [MenuItem("Build/Edit Settings", priority = 0)] + public static void EditSettings() { - // Find all classes that inherit from BuildPlatform and register them with BuildProject. - Type ti = typeof(BuildSettings); - - foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies()) - { - foreach (Type t in asm.GetTypes()) - { - if (ti.IsAssignableFrom(t) && ti != t) - { - BuildProject.RegisterSettings((BuildSettings)Activator.CreateInstance(t)); - } - } - } + Selection.activeObject = Instance; + EditorApplication.ExecuteMenuItem("Window/Inspector"); } #endregion - #region Public Methods + #region Variables + + [Header("Build Settings (Field Info in Tooltips)")] + + // The name of executable file (e.g. mygame.exe, mygame.app) + [SerializeField] + [Tooltip("The name of executable file (e.g. mygame.exe, mygame.app)")] + private string _binName = Application.productName; + + // The base path where builds are output. + // Path is relative to the Unity project's base folder unless an absolute path is given. + [SerializeField] + [Tooltip("The base path where builds are output.")] + private string _binPath = "bin"; + + // A list of scenes (filepaths) to include in the build. The first listed scene will be loaded first. + [SerializeField] + [Tooltip("A list of scenes to include in the build. First listed scene will be loaded first. ")] + private string[] _scenesInBuild = new string[] { + // @"Assets/Scenes/scene1.unity", + // @"Assets/Scenes/scene2.unity", + // ... + }; + + // A list of files/directories to include with the build. + // Paths are relative to Unity project's base folder unless an absolute path is given. + [SerializeField] + [Tooltip("A list of files/directories to include with the build.")] + private string[] _copyToBuild = new string[] { + // @"DirectoryToInclude/", + // @"FileToInclude.txt", + // ... + }; + + #endregion + + #region Methods & Properties - /// - /// Method executed before doing any build actions. - /// - public virtual void PreBuild() + public static string binName { + get { return Instance._binName; } } - /// - /// Method executed after performing all standard build actions. - /// - public virtual void PostBuild() + public static string binPath { + get { return Instance._binPath.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); } } - /// - /// Method executed prior to doing the build for a particular platform. - /// - /// - public virtual void PreBuild(BuildPlatform platform) + public static string[] scenesInBuild { + get { return Instance._scenesInBuild; } } - /// - /// Method executed after doing the build for a particular platform. - /// - /// - public virtual void PostBuild(BuildPlatform platform) + public static string[] copyToBuild { + get { return Instance._copyToBuild; } } #endregion diff --git a/Editor/Build/Settings/BuildSettingsGenerator.cs b/Editor/Build/Settings/BuildSettingsGenerator.cs deleted file mode 100644 index e1b0078..0000000 --- a/Editor/Build/Settings/BuildSettingsGenerator.cs +++ /dev/null @@ -1,15 +0,0 @@ -using UnityEditor; - -namespace UnityBuild -{ - -public static class BuildSettingsGenerator -{ - [MenuItem("Build/Generate/BuildSettings", false, 100)] - private static void GenerateBuildSettings() - { - Generator.Generate("ProjectBuildSettings.cs", "BuildSettings", "BuildSettingsTemplate"); - } -} - -} \ No newline at end of file diff --git a/Editor/Build/Settings/BuildSettingsTemplate.txt b/Editor/Build/Settings/BuildSettingsTemplate.txt deleted file mode 100644 index 831c0d8..0000000 --- a/Editor/Build/Settings/BuildSettingsTemplate.txt +++ /dev/null @@ -1,57 +0,0 @@ -using System.IO; -using UnityEditor; - -namespace UnityBuild -{ - -[InitializeOnLoad] -public class ProjectBuildSettings : BuildSettings -{ - // The name of executable file (e.g. mygame.exe, mygame.app) - private const string _binName = "mygame"; - - // The base path where builds are output. - // Path is relative to the Unity project's base folder unless an absolute path is given. - private const string _binPath = "bin"; - - // A list of scenes to include in the build. The first listed scene will be loaded first. - private readonly string[] _scenesInBuild = new string[] { - // @"Assets/Scenes/scene1.unity", - // @"Assets/Scenes/scene2.unity", - // ... - }; - - // A list of files/directories to include with the build. - // Paths are relative to Unity project's base folder unless an absolute path is given. - private readonly string[] _copyToBuild = new string[] { - // @"DirectoryToInclude/", - // @"FileToInclude.txt", - // ... - }; - - #region Methods & Properties (DO NOT EDIT) - - public override string binName - { - get { return _binName; } - } - - public override string binPath - { - get { return _binPath.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); } - } - - public override string[] scenesInBuild - { - get { return _scenesInBuild; } - } - - public override string[] copyToBuild - { - get { return _copyToBuild; } - } - - #endregion -} - -} \ No newline at end of file diff --git a/Editor/Build/Settings/BuildSettingsTemplate.txt.meta b/Editor/Build/Settings/BuildSettingsTemplate.txt.meta deleted file mode 100644 index f48d1ef..0000000 --- a/Editor/Build/Settings/BuildSettingsTemplate.txt.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: e008ce6fb3429b74192a53e57e4cd0b6 -timeCreated: 1460654944 -licenseType: Pro -TextScriptImporter: - userData: - assetBundleName: - assetBundleVariant: diff --git a/Editor/Upload/UploadItchSettings.cs b/Editor/Upload/UploadItchSettings.cs index 17206b3..8057867 100644 --- a/Editor/Upload/UploadItchSettings.cs +++ b/Editor/Upload/UploadItchSettings.cs @@ -1,23 +1,83 @@ using UnityEngine; using UnityEditor; -public class UploadItchSettings +namespace UnityBuild { - #region Constants - private const string _butlerPath = @"D:\User\Chase\Downloads\game-dev\butler\butler.exe"; - private const string _itchUserName = "supersystems"; - private const string _itchGameName = "butler-test"; +public class UploadItchSettings : BaseSettings +{ + #region Singleton + + private static UploadItchSettings instance = null; + + public static UploadItchSettings Instance + { + get + { + if (instance == null) + { + instance = CreateAsset("UploadItchSettings"); + } + + return instance; + } + } + + public UploadItchSettings() + { +#if UNITY_EDITOR_WIN + _butlerPath = @"C:\game-dev\butler.exe"; +#elif UNITY_EDITOR_OSX + _butlerPath = @"/Users/username/game-dev/butler"; +#endif + } + + #endregion + + #region MenuItems + + [MenuItem("Build/Upload/itch.io/Edit Settings", priority = 0)] + public static void EditSettings() + { + Selection.activeObject = Instance; + EditorApplication.ExecuteMenuItem("Window/Inspector"); + } #endregion + #region Variables + + [Header("Itch.io Upload Settings (Field Info in Tooltips)")] + + [SerializeField] + [Tooltip("Path to butler executable.")] + private string _butlerPath = ""; + + [SerializeField] + [Tooltip("itch.io username.")] + private string _itchUserName = "username"; + + [SerializeField] + [Tooltip("itch.io project name.")] + private string _itchGameName = "project"; + + [SerializeField] + [Tooltip("Upload version number (optional).")] + private string _versionNumber = ""; + + #endregion + #region Public Properties public static string versionNumber { get { - return string.Empty; + return Instance._versionNumber; + } + set + { + Instance._versionNumber = value; } } @@ -25,7 +85,7 @@ public static string butlerPath { get { - return _butlerPath; + return Instance._butlerPath; } } @@ -33,7 +93,7 @@ public static string itchUserName { get { - return _itchUserName; + return Instance._itchUserName; } } @@ -41,9 +101,11 @@ public static string itchGameName { get { - return _itchGameName; + return Instance._itchGameName; } } #endregion +} + } \ No newline at end of file