Skip to content

Commit

Permalink
Refactor Settings to be ScriptableObjects.
Browse files Browse the repository at this point in the history
  • Loading branch information
Chaser324 committed May 4, 2016
1 parent 58183f8 commit eaebb4a
Show file tree
Hide file tree
Showing 10 changed files with 223 additions and 186 deletions.
51 changes: 45 additions & 6 deletions Editor/AssetBundles/BuildAssetBundlesSettings.cs
Original file line number Diff line number Diff line change
@@ -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>("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)")]

/// <summary>
/// The path where AssetBundles are built. {0} = binPath
/// </summary>
private const string _buildPath = "{0}/Bundles";
[SerializeField]
[Tooltip("The path where AssetBundles are built. {0} = binPath")]
private string _buildPath = "{0}/Bundles";

/// <summary>
/// Flag indicating if the AssetBundles should be copied into the game's data directory.
/// </summary>
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

Expand All @@ -25,15 +64,15 @@ public static string buildPath
{
get
{
return string.Format(_buildPath, BuildProject.settings.binPath);
return string.Format(Instance._buildPath, BuildSettings.binPath);
}
}

public static bool copyToBuild
{
get
{
return _copyToBuild;
return Instance._copyToBuild;
}
}

Expand Down
33 changes: 3 additions & 30 deletions Editor/Build/BuildProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public static class BuildProject
{
#region Public Variables & Auto-Properties

public static BuildSettings settings { get; private set; }
public static List<BuildPlatform> platforms { get; private set; }
public static List<BuildAction> preBuildActions { get; private set; }
public static List<BuildAction> postBuildActions { get; private set; }
Expand All @@ -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();

Expand Down Expand Up @@ -77,18 +70,6 @@ private static void DisableAllPlatforms()

#region Register Methods

/// <summary>
/// Register the project's BuildSettings.
/// </summary>
/// <param name="settings"></param>
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;
}

/// <summary>
/// Register a platform that can be built.
/// </summary>
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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++)
Expand All @@ -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++)
Expand All @@ -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++)
Expand All @@ -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++)
Expand Down
6 changes: 3 additions & 3 deletions Editor/Build/Platform/BuildPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,23 +107,23 @@ public string buildPath
{
get
{
return BuildProject.settings.binPath + Path.DirectorySeparatorChar + name + Path.DirectorySeparatorChar;
return BuildSettings.binPath + Path.DirectorySeparatorChar + name + Path.DirectorySeparatorChar;
}
}

public string dataDirectory
{
get
{
return buildPath + string.Format(dataDirNameFormat, BuildProject.settings.binName) + Path.DirectorySeparatorChar;
return buildPath + string.Format(dataDirNameFormat, BuildSettings.binName) + Path.DirectorySeparatorChar;
}
}

public string exeName
{
get
{
return string.Format(binaryNameFormat, BuildProject.settings.binName);
return string.Format(binaryNameFormat, BuildSettings.binName);
}
}

Expand Down
34 changes: 34 additions & 0 deletions Editor/Build/Settings/BaseSettings.cs
Original file line number Diff line number Diff line change
@@ -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<T>(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<T>();
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;
}
}

}

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

Loading

0 comments on commit eaebb4a

Please sign in to comment.