Skip to content

Instantly share code, notes, and snippets.

@HassakuTb
Last active November 25, 2017 09:50
Show Gist options
  • Save HassakuTb/44ce6aedd37d905d50620bdc262d4392 to your computer and use it in GitHub Desktop.
Save HassakuTb/44ce6aedd37d905d50620bdc262d4392 to your computer and use it in GitHub Desktop.
Create asset contains Git commit id that was commited at Build Unity project
NYSL Version 0.9982 (en) (Unofficial)
----------------------------------------
A. This software is "Everyone'sWare". It means:
Anybody who has this software can use it as if he/she is
the author.
A-1. Freeware. No fee is required.
A-2. You can freely redistribute this software.
A-3. You can freely modify this software. And the source
may be used in any software with no limitation.
A-4. When you release a modified version to public, you
must publish it with your name.
B. The author is not responsible for any kind of damages or loss
while using or misusing this software, which is distributed
"AS IS". No warranty of any kind is expressed or implied.
You use AT YOUR OWN RISK.
C. Copyrighted to Hassaku
D. Above three clauses are applied both to source and binary
form of this software.
using UnityEngine;
namespace Git {
public class Commit : ScriptableObject{
public string id;
public static Commit Create(string commitId) {
var instance = CreateInstance<Commit>();
instance.id = commitId;
return instance;
}
}
}
using UnityEngine;
using UnityEngine.UI;
// sample of using commit id
[RequireComponent(typeof(Text))]
public class CommitIdText : MonoBehaviour{
void Start() {
Text text = GetComponent<Text>();
Git.Commit commit = Resources.Load<Git.Commit>("GitHead");
text.text = commit.id.Substring(0, 7);
}
}
using System.IO;
using UnityEngine;
using UnityEditor;
using UnityEditor.Build;
using Git;
namespace Editor.Git {
// put in Editor folder
// requires Unity 5.6 or lator
public class Preprocessor : IPreprocessBuild {
public int callbackOrder {
get { return 0; }
}
public void OnPreprocessBuild(BuildTarget target, string path) {
var gitRoot = Path.Combine(Directory.GetCurrentDirectory(), ".git");
if (!Directory.Exists(gitRoot)) {
Debug.LogError("not fount .git directory");
return;
}
var headPath = Path.Combine(gitRoot, "HEAD");
var head = File.ReadAllText(headPath);
string commitId;
if (IsHeadDetached(head)) {
commitId = head.Trim();
}
else {
var headRefPath = Path.Combine(gitRoot, head.Remove(0, 4).Trim());
commitId = File.ReadAllText(headRefPath);
}
Commit commit = Commit.Create(commitId);
AssetDatabase.CreateAsset(commit, "Assets/Resources/GitHead.asset"); // change asset path.
}
private static bool IsHeadDetached(string head) {
return !head.StartsWith("ref:");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment