MRが楽しい

MRやVRについて学習したことを書き残す

公式チュートリアル「MR and Azure 305 8章」を試してみる

本日はチュートリアルの実施枠です。
Academyの「MR and Azure 305: Functions and storage」の実施内容をまとめます。
docs.microsoft.com
前回記事の続きです。
bluebirdofoz.hatenablog.com
今回は「Chapter 8」です。

Chapter 8:Create the ShapeFactory class

作成する次のスクリプトは、ShapeFactoryクラスです。
このクラスの役割は、要求されたときに新しいシェイプを作成し、履歴リストで作成されたシェイプの履歴を保持することです。
シェイプが作成されるたびに、履歴リストが AzureService クラスで更新され、Azure ストレージに保存されます。
アプリケーションが起動すると、Azure Storage に保存されている履歴リストを取得します。
履歴リストが見つかると、生成されたシェイプがストレージのものか新しいものかを3D Textオブジェクトが通知します。

1.Script フォルダを開きます。
2.フォルダ内で右クリックして、Creapte -> C# Script を選択します。
Script の名称は ShapeFactory に設定します。
f:id:bluebirdofoz:20181009040423j:plain

3.新しいスクリプトをダブルクリックしてVisual Studioで開きます。
4-6.以下の通り、スクリプトを編集します。
・ShapeFactory.cs

// 名前空間の追加
using System;
using System.Collections.Generic;
using UnityEngine;

public class ShapeFactory : MonoBehaviour {
    // メンバ変数の追加
    /// <summary>
    /// Provide this class Singleton-like behaviour
    /// このクラスをシングルトンと同じように動作させます
    /// </summary>
    [HideInInspector]
    public static ShapeFactory instance;

    /// <summary>
    /// Provides an Inspector exposed reference to ShapeSpawnPoint
    /// InspectorにShapeSpawnPointへの参照を提供する
    /// </summary>
    [SerializeField]
    public Transform spawnPoint;

    /// <summary>
    /// Shape History Index
    /// シェイプ履歴のインデックス
    /// </summary>
    [HideInInspector]
    public List<int> shapeHistoryList;

    /// <summary>
    /// Shapes Enum for selecting required shape
    /// 必要な形状を選択するための図形列挙型
    /// </summary>
    private enum Shapes { Cube, Sphere, Cylinder }


    /// <summary>
    /// Called on initialization
    /// 初期化処理
    /// </summary>
    private void Awake()
    {
        instance = this;
    }

    /// <summary>
    /// Runs at initialization right after Awake method
    /// StartメソッドはAwakeメソッドの直後の初期化時に実行されます
    /// </summary>
    private void Start()
    {
        shapeHistoryList = new List<int>();
    }

    /// <summary>
    /// Use the Shape Enum to spawn a new Primitive object in the scene
    /// シェイプ列挙型を使用してシーン内に新しいプリミティブオブジェクトを生成する
    /// </summary>
    /// <param name="shape">Enumerator Number for Shape(図形の列挙子番号)</param>
    /// <param name="storageShape">Provides whether this is new or old(新しいものか古いものか)</param>
    internal void CreateShape(int shape, bool storageSpace)
    {
        Shapes primitive = (Shapes)shape;
        GameObject newObject = null;
        string shapeText = storageSpace == true ? "Storage: " : "New: ";

        AzureServices.instance.azureStatusText.text = string.Format("{0}{1}", shapeText, primitive.ToString());

        switch (primitive)
        {
            case Shapes.Cube:
                newObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
                break;

            case Shapes.Sphere:
                newObject = GameObject.CreatePrimitive(PrimitiveType.Sphere);
                break;

            case Shapes.Cylinder:
                newObject = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
                break;
        }

        if (newObject != null)
        {
            newObject.transform.position = spawnPoint.position;

            newObject.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);

            newObject.AddComponent<Rigidbody>().useGravity = true;

            newObject.GetComponent<Renderer>().material.color = UnityEngine.Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f);
        }
    }
}

f:id:bluebirdofoz:20181009040436j:plain

7.Visual Studio で変更を保存して Unity に戻ります。
f:id:bluebirdofoz:20181009040452j:plain

8.Hierarchy パネルの MixedRealityCameraParent を開くと、直下に MixedRealityCamera オブジェクトがあります。
ShapeFactory スクリプトをこの MixedRealityCamera オブジェクトにドラッグして追加します。
f:id:bluebirdofoz:20181009040500j:plain

9.Hierarchy パネルの GazeButton を開きます。
直下にある ShapeSpawnPoint オブジェクトを ShapeFactory スクリプトの SpawnPoint 変数に設定します。
f:id:bluebirdofoz:20181009040513j:plain

Chapter 8 はここまでです。
次回は Chapter 9 を実施します。
bluebirdofoz.hatenablog.com