# ランタイム中に Build Automation のビルドマニフェストを使用

> Access build manifest data at runtime for analytics and bug reporting in Unity Build Automation.

ゲームのランタイムコードが、ビルド自体に関する主要な情報を把握していると、多くの場合に役立ちます。ビルドの名前や番号などの情報は、バグの報告や分析情報の追跡に非常に役立ちます。これを支援するために、Build Automation ではビルド時にゲームにマニフェストを挿入し、後のランタイム時にこの主要データにアクセスできるようにします。

Build Automation は、マニフェストを JSON 形式の [`TextAsset`](https://docs.unity3d.com/Manual/class-TextAsset.html) として提供します。Build Automation は、`Resources.Load()` からアクセスできるゲームリソースとしてマニフェストを保存します。ビルドマニフェストには以下の値が含まれます。

| **値**                  | **プロパティ**                                                      |
| ---------------------- | -------------------------------------------------------------- |
| `scmCommitId`          | ビルドされたコミットまたは変更リスト。                                            |
| `scmBranch`            | ビルドされたブランチの名前。                                                 |
| `buildNumber`          | このビルドに対応する Build Automation ビルド番号。                             |
| `buildStartTime`       | ビルドプロセスが開始されたときの UTC タイムスタンプ。                                  |
| `projectId`            | Unity プロジェクト ID。                                               |
| `bundleId`             | Build Automation で構成された `bundleIdentifier` (iOS と Android のみ)。 |
| `unityVersion`         | Build Automation がビルドの作成に使用した Unity のバージョン。                    |
| `xcodeVersion`         | プロジェクトのビルドに使用された XCode のバージョン (iOS のみ)。                        |
| `cloudBuildTargetName` | ビルドされたビルドターゲットの名前。                                             |

**UnityCloudBuildManifest.json** というマニフェスト TextAsset が `Assets/UnityCloud/Resources` フォルダーに書き込まれます。

## ローカルテスト用##for-local-testing

ビルドマニフェスト機能をローカルでテストするには、ファイルに `UnityCloudBuildManifest.json.txt` という名前を付けます。コードリポジトリ内のプロジェクトの **アセット/UnityCloud/リソース** フォルダーにこのファイルをコミットしないでください。Build Automation マニフェストファイルの妨げになる可能性があるためです。

## マニフェストの使用##use-the-manifest

ランタイムにマニフェストにアクセスするには、以下を使用します。

* [JSON 形式のマニフェスト](#build-manifest-as-json)。
* [マニフェストの ScriptableObject](#build-manifest-as-scriptableobject)。

### JSON 形式のビルドマニフェスト##build-manifest-as-json

Build Automation マニフェストにはランタイムに JSON 形式としてアクセスできます。このアセットには、カスタム解析ロジックまたはサードパーティ製の JSON パーサーを使用する必要があります。

以下の C# コード例は、[GitHub Gist](https://gist.github.com/darktable/1411710) で MiniJSON パーサーを使用してビルドマニフェストをロードし、解析する方法を示します。

```cs
using UnityEngine;
using System.Collections.Generic;
using MiniJSON;

public class MyGameObject: MonoBehaviour
{
    void Start()
    {
        var manifest = (TextAsset) Resources.Load("UnityCloudBuildManifest.json");
        if (manifest != null)
        {
            var manifestDict = Json.Deserialize(manifest.text) as Dictionary<string,object>;
            foreach (var kvp in manifestDict)
            {
                // Be sure to check for null values!
                var value = (kvp.Value != null) ? kvp.Value.ToString() : "";
                Debug.Log(string.Format("Key: {0}, Value: {1}", kvp.Key, value));
            }
        }
    }
}
```

### ビルドマニフェストを `ScriptableObject` とする##build-manifest-as-scriptableobject

`BuildManifestObject` は、`UnityCloudBuildManifest.json` TextAsset を手動でロードせずに、スクリプトからビルドマニフェストの値にアクセスするために使用する [`ScriptableObject`](https://docs.unity3d.com/ScriptReference/ScriptableObject.html) です。

`BuildManifestObject` は、`UnityCloudBuildManifest.json` TextAsset が書き込まれていない場合に Build Automation によって呼び出されるエクスポート前の任意のパラメーターです。詳細は、 [JSON 形式のマニフェスト](#build-manifest-as-json) を参照してください。

以下の C# コード例は、エクスポート前のメソッドを示しています。これは、マニフェストで指定された `buildNumber` に基づいて `PlayerSettings` の `bundleVersion` を更新します。エクスポート前メソッドの詳細については、[エクスポート前メソッドとエクスポート後メソッド](../advanced-build-configuration/run-custom-scripts-during-the-build-process#pre-export-and-post-export-methods) を参照してください。

```cs
using UnityEngine;
using UnityEditor;
using System;

public class CloudBuildHelper : MonoBehaviour
{
    #if UNITY_CLOUD_BUILD
        public static void PreExport(UnityEngine.CloudBuild.BuildManifestObject manifest)
        {
            PlayerSettings.bundleVersion = string.Format("1.0.{0}", manifest.GetValue<int>("buildNumber"));
        }
    #endif
}
```

以下は、`BuildManifestObject` クラスの public インターフェースです。

```cs
namespace UnityEngine.CloudBuild
{
    public class BuildManifestObject : ScriptableObject
    {
        // Try to get a manifest value - returns true if key was found and could be cast to type T, otherwise returns false.
        public bool TryGetValue<T>(string key, out T result);
        // Retrieve a manifest value or throw an exception if the given key isn't found.
        public T GetValue<T>(string key);
        // Set the value for a given key.
        public void SetValue(string key, object value);
        // Copy values from a dictionary. ToString() will be called on dictionary values before being stored.
        public void SetValues(Dictionary<string, object> sourceDict);
        // Remove all key/value pairs.
        public void ClearValues();
        // Return a dictionary that represents the current BuildManifestObject.
        public Dictionary<string, object> ToDictionary();
        // Return a JSON formatted string that represents the current BuildManifestObject
        public string ToJson();
        // Return an INI formatted string that represents the current BuildManifestObject
        public override string ToString();
    }
}
```
