【Unity】AssetBundleの自動ビルド化をやってみた【Jenkins】
AssetBundleの自動ビルドシステムを作ることになり
実際にやってみてのポイントなどを書いていく
ほとんど自分用メモです
■UnityはBatchModeオプションで起動する
/Applications/Unity/Unity.app/Contents/MacOS/Unity -batchmode -quit -projectPath $プロジェクトのパス/ -executeMethod ExportAssetBundles.AutoExportResource -logFile ./BuildTools/Log/build.log; cat ./BuildTools/Log/build.log
BatchModeオプションをつけることでUnityのエディタ自体を立ち上げないままメソッドを直接呼び出すことが出来る。
ビルドログの出力先もここで指定する。
ちなみにJenkinsからシェルを実行させるには設定の『ビルド手順の追加』から『シェルの実行』を選択してやると記述欄が出てくる
■テクスチャ素材のインポート設定を自動化する
テクスチャ素材によって設定が違ったりしたので、置かれるディレクトリ名で判別して自動設定されるようにした。
public class CustomImportSettings : AssetPostprocessor { void OnPreprocessTexture() { Debug.Log("Importing texture to: " + assetPath); string[] pathSplitArrayData = assetPath.Split('/'); List<Dictionary<string,string>> buildParamList = LoadCsv(AUTOBUILD_SETTING_CSV_PATH); //csv読み込みメソッド string textureType = ""; string textureFormat = ""; foreach(Dictionary<string,string> buildParam in buildParamList){ if(Array.IndexOf(pathSplitArrayData, buildParam["resource_name"]) != -1) { textureType = buildParam ["texture_type"]; textureFormat = buildParam ["texture_format"]; break; } } TextureImporter textureImporter = assetImporter as TextureImporter; textureImporter.textureType = ManageTextureImportType.getTextureImporterType(int.Parse(textureType)); //int型のidからtextureTypeを取得するメソッド textureImporter.textureFormat = ManageTextureImportFormat.getTextureImporterFormat(int.Parse(textureFormat)); //int型のidからtextureFormatを取得するメソッド } }
AssetPostprocessorクラスのOnPreprocessTextureメソッドを実装すると、画像がインポートされる瞬間に呼び出されて任意の処理を行うことが出来る。
assetPathで画像が置かれたパスを取得できるので、そこから設定を判別し、textureTypeとtextureFormatを設定する
■Jenkinsのワークスペースは2つ用意する
これはスマホゲームのようなマルチプラットフォーム対応のプロジェクトに限ったもの
御存知の通りUnityのSwitchPlatformは死ぬほど時間がかかる
そこで一つのJenkinsジョブに同一リポジトリのワークスペースを2つ用意し、交互にビルドを行うことでSwitchPlatformをやらずにマルチプラットフォームのビルドが行える
実際これをやらないと数個のAssetBundleだとしてもAssetが多いプロジェクトならば15分とかはザラだ
もちろんほとんどSwitchPlatformにあてられた時間である
やり方はJenkinsの設定『ソースコード管理』から任意のバージョン管理を選択し、『ロケーションの追加』したあとにそれぞれ『ローカルモジュールディレクトリ』を設定してやるだけでいい
■自動化してみて
うちのプロジェクトでは何年も手動でAssetBundleを作っていたらしい
なんだよそれ、AssetBundle職人かよ
正直自分に引き継がれた時はコイツァは人間様のする仕事じゃねえ!!以外の感想は出てこなかった
そして自動化することを決めた、自分が幸せになり人間としての尊厳を取り戻すために
幸い自動化システムを作っているときはとても楽しかった
皆もガンガン自動化して人間は人間の尊厳を保った人間らしい仕事をしていこう
【Unity】Assetsディレクトリ以下にないファイルを一覧取得する【エディタ拡張】
Resources.Load()とかAssetDatabase.FindAssets()とか
Edita拡張からAssetsディレクトリ以下のデータにアクセスするメソッドはよく見つかるのですが
それより上の階層は無理なのかなーと思ったら普通に出来ました
Unityのメソッドとして探していたのが悪かっただけで普通にC#のメソッドを使えばよかったのだ!!
string[] result = System.IO.Directory.GetFiles(path,"*検索ファイル名とか*");
すごい普通だった!
でもUnityからC#はじめた人なんかはなかなか盲点なのかなーと
C#のFileクラスと組み合わせても色々やれそうですな
File クラス (System.IO)
【Unity】Quaternionをヨー、ピッチ、ロールに変換する【ジャイロ】
ジャイロで受け取ったQuaternionからヨー、ピッチ、ロールへの変換式です
Unityに限らず意外と使うところあると思うんですけど、あんまりネット探しても載ってないんですよね
毎度計算式忘れるので自分への備忘録の意味も込めて
void Start () { //ジャイロフラグをオン if (SystemInfo.supportsGyroscope) { Input.gyro.enabled = true; } } void Update () { Quaternion gyro = Input.gyro.attitude; float yaw = Mathf.Atan2(2 * gyro.x * gyro.y + 2 * gyro.w * gyro.z, gyro.w * gyro.w + gyro.x * gyro.x - gyro.y * gyro.y - gyro.z * gyro.z); float pitch = Mathf.Asin (2 * gyro.w * gyro.y - 2 * gyro.x * gyro.z); float roll = Mathf.Atan2 (2 * gyro.y * gyro.z + 2 * gyro.w * gyro.x, -gyro.w * gyro.w + gyro.x * gyro.x + gyro.y * gyro.y - gyro.z * gyro.z); }
Unityの回転は鬼門だと思う
【Unity】Scene内のComponentから文字列を検索する【エディタ拡張】
だいぶ前回更新から期間が空いてしまった...
というのも、エディタ拡張を作っていました。
Scene内のオブジェクト全て(無効含む)に関連付けられたComponentから文字列を検索するエディタ拡張
題して『SceneScriptFinder』!!ヒュー!英語が怪しいぜ!
まだ改良したいところは山ほどあるのですが、とりあえず形にはなりました。
検索結果を階層構造で表示してなおかつ行数も教えてくれます。
ほんとはエディタの指定行に直接飛べたらよかったんだけどね。
※2015/07/16
一応パッケージ化しました。下記リンクからどうぞ
SceneScriptFinder
以下コードです
良ければコピって使ってやってくださいまし
using UnityEngine; using UnityEditor; using System; using System.Collections.Generic; using System.Text.RegularExpressions; public class SceneScriptFinder : EditorWindow { // 変数 private string searchText = string.Empty; private List<List<List<Dictionary<string,string>>>> objList = null; private List<bool> foldOutFlagList = new List<bool>(); private List<bool> foldOutFlagList2 = new List<bool>(); private Vector2 scrollPosition = Vector2.zero; private static bool IsRegex = true; // method [MenuItem("Window/SceneScriptFinder")] static void Open() { EditorWindow.GetWindow<SceneScriptFinder>( "SceneScriptFinder" ); } void OnGUI() { GUILayout.Label( "SearchText" ); searchText = GUILayout.TextArea( searchText ); IsRegex = EditorGUILayout.Toggle( "大文字小文字チェック", IsRegex ); if( GUILayout.Button("Scan") ) Find(); scrollPosition = GUILayout.BeginScrollView(scrollPosition); DrawResults(); // 結果一覧 GUILayout.EndScrollView(); // スクロールバー終了 } /* 文字列検索 */ private void Find (){ if(searchText != "" && searchText != null){ objList = new List<List<List<Dictionary<string,string>>>>(); //foldOutFlag format foldOutFlagList = new List<bool>(); foldOutFlagList2 = new List<bool>(); foreach (GameObject obj in UnityEngine.Resources.FindObjectsOfTypeAll(typeof(GameObject))) { // シーン上に存在するオブジェクトならば処理 if(obj.activeInHierarchy){ // アセットからパスを取得.シーン上に存在するオブジェクトの場合,シーンファイル(.unity)のパスを取得. string path = AssetDatabase.GetAssetOrScenePath(obj); // シーン上に存在するオブジェクトかどうか文字列で判定. bool isScene = path.Contains(".unity"); // シーン上に存在するオブジェクトならば処理. if (isScene) { List<List<Dictionary<string,string>>> componentList = new List<List<Dictionary<string,string>>> (); // コンポーネント一覧を配列で取得 var components = obj.GetComponents<MonoBehaviour> (); if (components != null) { foreach(var componentData in components){ if(componentData != null){ // コンポーネントのテキストを取得 var monoscript = MonoScript.FromMonoBehaviour (componentData); List<Dictionary<string,string>> lines = new List<Dictionary<string,string>> (); // 行数カウント用 int lineCounter = 0; foreach (var line in monoscript.text.Split( new string[]{ Environment.NewLine }, StringSplitOptions.None )) { Dictionary<string,string> lineParams = new Dictionary<string,string> (); lineCounter++; if (!Match (line, searchText)) continue; // コンポーネント名 lineParams.Add ("compornent",componentData.GetType().ToString()); // オブジェクト名 lineParams.Add ("objectName",componentData.gameObject.ToString()); // 何行目か lineParams.Add ("lineNum",lineCounter.ToString()); // ヒットした行の文字列 lineParams.Add ("line",line); lines.Add (lineParams); } if(lines.Count != 0) componentList.Add (lines); } } } objList.Add(componentList); } } } } } /* 文字列がマッチしたらtrueを返す */ public static bool Match(string input, string search) { if (string.IsNullOrEmpty (input)) return false; if (!IsRegex) { var match = Regex.Match (input, search, RegexOptions.IgnoreCase); return match.Success; } else { var result = input.IndexOf (search); return (result != 0 && result != -1); } } /* 結果一覧を描画 */ private void DrawResults() { int i = 0; int j = 0; if (objList != null) { foreach(List<List<Dictionary<string,string>>> componentList in objList){ if(componentList.Count != 0 && componentList[0].Count != 0){ // インデント初期化 EditorGUI.indentLevel = 0; // オブジェクト名Foldout foldOutFlagList.Add(false); foldOutFlagList[i] = EditorGUILayout.Foldout( foldOutFlagList[i],componentList[0][0]["objectName"]); if (foldOutFlagList[i]){ foreach(List<Dictionary<string,string>> lines in componentList){ foreach(Dictionary<string,string> lineParams in lines){ // インデント追加 EditorGUI.indentLevel = 1; // コンポーネント名+行数 foldOutFlagList2.Add(false); foldOutFlagList2[j] = EditorGUILayout.Foldout(foldOutFlagList2[j], " ↳" + lineParams["compornent"] + " " + lineParams["lineNum"] ); if (foldOutFlagList2[j]){ // インデント追加 EditorGUI.indentLevel = 2; // ヒットした行の文字列 EditorGUILayout.SelectableLabel(lineParams["line"]); } j++; } } } i++; } } } } }
負荷テストなんてやってないので超大規模プロジェクトとかでやったらどうなるかわかりません。
でも最悪Unityが死ぬだけだ。なんとかなる。
【Unity】文字列をファイルに書き出す【Log出力】
ログを出力したい時などに使えるかと
思ったより簡単でした
using System.IO; // 引数でStringを渡してやる public void textSave(string txt){ StreamWriter sw = new StreamWriter("../LogData.txt",false); //true=追記 false=上書き sw.WriteLine(txt); sw.Flush(); sw.Close(); }
出力先のログフォルダなどを予め作っておくとよいかと思います
もちろんエディタ拡張でも同様に使えます
【Unity】ボタンを押下させる間隔を制限する【連打防止】
ボタン押下時にデータをリストで取得するなどの重めの通信を走らせてたりすると、連打制限したいなって時ありますよね 絶対連打する人いるし....
最低限で簡単な実装ですが、今後も使いまわせそうなので載せておきます
アレンジして使っていきましょう
// ボタン押下許可フラグ private bool isPushReloadButton = false; // ボタンが押されてから次にまた押せるまでの時間(秒) private TimeSpan allowTime = new TimeSpan(0, 0, 3); // 前回ボタンが押された時点と現在時間との差分を格納 private TimeSpan pastTime; void Update () { // 3秒後にボタン押下を許可 if (isPushReloadButton) { pastTime = DateTime.Now - this.reloadTime; if(pastTime > allowTime){ isPushReloadButton = false; } } } //ボタンイベント public void OnClickButton () { if ( isPushReloadButton ) return; isPushReloadButton = true; // ここに通信処理などを書く //現在の時間をセット this.reloadTime = DateTime.Now; }
もちろんですが、端末の時間を利用して秒をカウントしているので
動作が重い端末だと時間が変わってしまうのかも
そこは要検証ですが、ボタン連打制限ならアバウトでもきっとだいじょうぶでしょ
Unityエディタ拡張でシーン内のオブジェクトに関連付けられたコンポーネント内の変数を取得する
タイトルめっちゃ長い
シーン内の変数一覧が欲しくなったのでエディタ拡張から取得出来ないか頑張ってみました
本当は値も欲しかったのだけど、今回とれたのは変数名一覧だけでした
using UnityEngine; using UnityEditor; using System; using System.Collections; using System.Collections.Generic; using System.Reflection; public class EditorExWindow : EditorWindow { [MenuItem("Window/EditorEx")] static void Open() { EditorWindow.GetWindow<EditorExWindow>( "EditorEx" ); } string textArea = ""; void OnGUI() { if( GUILayout.Button("Caputure") ) Find(); GUILayout.Label( "TextArea" ); textArea = GUILayout.TextArea( textArea ); } private void Find (){ // TextAreaクリア textArea = ""; List<GameObject> AllObjects = new List<GameObject>(); List<MonoBehaviour> allMonobehaviourList = new List<MonoBehaviour> (); foreach (GameObject obj in UnityEngine.Resources.FindObjectsOfTypeAll(typeof(GameObject))) { // アセットからパスを取得.シーン上に存在するオブジェクトの場合,シーンファイル(.unity)のパスを取得. string path = AssetDatabase.GetAssetOrScenePath(obj); // シーン上に存在するオブジェクトかどうか文字列で判定. bool isScene = path.Contains(".unity"); // シーン上に存在するオブジェクトならば処理. if (isScene) { AllObjects.Add (obj); } } allMonobehaviourList.AddRange (GetComponentsInList<MonoBehaviour> (AllObjects)); foreach(var componentData in allMonobehaviourList){ foreach(MemberInfo mi in componentData.GetType().GetMembers(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)){ textArea += mi.Name + "\n"; } } } public static IEnumerable<T> GetComponentsInList<T> (IEnumerable<GameObject> gameObjects ) where T : Component { var componentList = new List<T> (); foreach (var obj in gameObjects) { var components = obj.GetComponents<T> (); if (components != null) { foreach(var component in components){ componentList.Add (component); } } } return componentList; } }
変数名が取れたんだから中身も取れると思うんだけど...
もうちょっとがんばろう