Skip to content

Instantly share code, notes, and snippets.

@Dssdiego
Last active April 28, 2020 08:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dssdiego/5acaef1d9884581b06f69841ac2ed242 to your computer and use it in GitHub Desktop.
Save Dssdiego/5acaef1d9884581b06f69841ac2ed242 to your computer and use it in GitHub Desktop.
Keeps the PlayerSettings info of the Unity Project in a separate data asset
using UnityEngine;
[CreateAssetMenu (menuName = "App Version")]
public sealed class AppVersion : ScriptableObject
{
/// <summary>
/// Game Name
/// </summary>
[Header("Settings")]
[SerializeField] private string gameName;
/// <summary>
/// The major version number. For versions that should replace a prior major version.
/// </summary>
[Header ("Semantic Version")]
[Min (0)] [SerializeField] private int major = 0;
/// <summary>
/// The minor version number. For versions that offer smaller improvements and additions.
/// </summary>
[Min (0)] [SerializeField] private int minor = 0;
/// <summary>
/// The patch version number. For versions that offer some kind of bug fix or incremental improvements.
/// </summary>
[Min (0)] [SerializeField] private int patch = 0;
/// <summary>
/// The type of release the software is intended for.
/// </summary>
[Header ("Release Suffix")]
[SerializeField] private ReleaseType release = ReleaseType.Development;
/// <summary>
/// If an alpha or beta version, append this number to the end.
/// </summary>
[Min (0)] [SerializeField] private int releaseUpdate = 0;
/// <summary>
/// Creates a string in the semver format according to the set values in the inspector.
/// </summary>
/// <returns>The semver string of the current software being developed.</returns>
private string GetVersionNumber ()
{
string releaseSuffix = "";
switch (release)
{
case ReleaseType.Development:
releaseSuffix = "-dev";
break;
case ReleaseType.Alpha:
releaseSuffix = "-a" + releaseUpdate.ToString ();
break;
case ReleaseType.Beta:
releaseSuffix = "-b" + releaseUpdate.ToString ();
break;
default:
break;
}
return string.Format ("{0}.{1}.{2}{3}", major, minor, patch, releaseSuffix);
}
/// <summary>
/// Implicitly cast this <c>AppVersion</c>s into a string for display in the UI.
/// </summary>
/// <param name="version">The object to cast.</param>
public static implicit operator string (AppVersion version)
{
return version.GetVersionNumber ();
}
/// <summary>
/// Treat this object as a read only string of the current semver.
/// </summary>
/// <returns>The semver string.</returns>
public override string ToString ()
{
return GetVersionNumber ();
}
public string GetProductName()
{
return gameName;
}
}
/// <summary>
/// Defines the various types of releases which dictate what sort of suffix is added to the end of the version string.
/// </summary>
public enum ReleaseType
{
Development,
Alpha,
Beta,
Production
}
using UnityEditor;
using UnityEngine;
namespace Editor
{
[CustomEditor (typeof (AppVersion))]
public class AppVersionEditor : UnityEditor.Editor
{
AppVersion instance;
public void OnEnable ()
{
instance = (AppVersion)target;
PlayerSettings.bundleVersion = instance;
PlayerSettings.productName = instance.GetProductName();
}
public override void OnInspectorGUI ()
{
base.OnInspectorGUI ();
if (GUI.changed)
{
PlayerSettings.bundleVersion = instance;
PlayerSettings.productName = instance.GetProductName();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment