Skip to content

Instantly share code, notes, and snippets.

@ZimM-LostPolygon
Created September 30, 2015 19:41
Show Gist options
  • Save ZimM-LostPolygon/c0b51a82846e901d0afa to your computer and use it in GitHub Desktop.
Save ZimM-LostPolygon/c0b51a82846e901d0afa to your computer and use it in GitHub Desktop.
Unity version parser
using System;
using System.Text.RegularExpressions;
namespace LostPolygon.Shared {
internal class UnityVersionParser {
private readonly int _versionMajor = -1;
private readonly int _versionMinor = -1;
private readonly int _versionPatch = -1;
private readonly UnityBuildType _versionBuildType = UnityBuildType.Unknown;
private readonly int _versionReleaseNumber = -1;
public UnityVersionParser(string unityVersion) {
Match versionMatch = Regex.Match(unityVersion, @"(\d+)\.(\d+)\.(\d+)([bpf])?(\d+)?");
_versionMajor = Convert.ToInt32(versionMatch.Groups[1].Value);
_versionMinor = Convert.ToInt32(versionMatch.Groups[2].Value);
_versionPatch = Convert.ToInt32(versionMatch.Groups[3].Value);
if (versionMatch.Groups.Count > 4) {
string versionBuildType = versionMatch.Groups[4].Value;
switch (versionBuildType) {
case "f":
_versionBuildType = UnityBuildType.Release;
break;
case "p":
_versionBuildType = UnityBuildType.Patch;
break;
case "b":
_versionBuildType = UnityBuildType.Beta;
break;
default:
_versionBuildType = UnityBuildType.Unknown;
break;
}
_versionReleaseNumber = Convert.ToInt32(versionMatch.Groups[5].Value);
}
}
public int GetUnityVersionNumeric() {
return _versionMajor * 100 + _versionMinor * 10 + _versionPatch;
}
public int VersionMajor {
get {
return _versionMajor;
}
}
public int VersionMinor {
get {
return _versionMinor;
}
}
public int VersionPatch {
get {
return _versionPatch;
}
}
public UnityBuildType VersionBuildType {
get {
return _versionBuildType;
}
}
public int VersionReleaseNumber {
get {
return _versionReleaseNumber;
}
}
public enum UnityBuildType {
Unknown,
Release,
Beta,
Patch
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment