MagicOnion環境構築(macOS+Rider)パターンB (非推奨)

環境

  • macOS Catalina 10.15.3

  • Unity 2019.3.4f1

  • Jet Brains Rider 2019.3.4

環境構築

はじめに

全体のプロジェクトを入れるためのフォルダを作成する。

ここではMagicOnionSampleとする

Unityプロジェクト作成

MagicOnionSampleフォルダの中にUnityのプロジェクトを作成する。

プロジェクト名をここではSample.Unityとする。

他のフォルダ作成

次のようなパスに、Sample.ServerSample.SharedGeneratorToolsという3つのフォルダを作成する。

Sampleの部分はそれぞれ自分のプロジェクト名に置き換えて下さい。

  • MagicOnionSample/GeneratorTools
  • MagicOnionSample/Sample.Unity/Assets/Scripts
  • MagicOnionSample/Sample.Unity/Assets/Scripts/Generated
  • MagicOnionSample/Sample.Unity/Assets/Scripts/ServerShared
  • MagicOnionSample/Sample.Unity/Assets/Scripts/ServerShared/Hubs
  • MagicOnionSample/Sample.Unity/Assets/Scripts/ServerShared/MessagePackObjects
  • MagicOnionSample/Sample.Unity/Assets/Scripts/ServerShared/Services

PlayerSettings

  • Api Compatibility Level.NET4.x に設定

  • Scripting Define SymbolENABLE_UNSAFE_MSGPACKと入力する。(全てのプラットフォームに同様に入力する)

  • Allow 'unsafe' Codeにチェックを入れる。

MagicOnionをインストール

Releases · Cysharp/MagicOnion · GitHub

から

  • MagicOnion.Client.Unity.unitypackage
  • moc.zip

の2つをダウンロード。

MagicOnion.Client.Unity.unitypackage をインポート。

moc.zipを解凍し、GeneratorToolsフォルダの中に入れる。

MessagePack for C#

Releases · neuecc/MessagePack-CSharp · GitHub

から

  • MessagePack.Unity.unitypackage
  • mpc.zip

の2つをダウンロード

MessagePack.Unity.unitypackage をインポート

mpcをGeneratorToolsフォルダの中に入れる。

gRPCのダウンロード

https://packages.grpc.io

ここから、最新のビルドをクリックし、C#のカテゴリにある、grpc_unity_packageと名前が付いているものをダウンロードし、

解凍したPluginsフォルダの中から、

  • Grpc.Core
  • Grpc.Core.meta
  • Grpc.Core.Api
  • Grpc.Core.Api.meta

の4つをMagicOnionSample/Sample.Unity/Assets/Plugins/ の中に入れる

サーバーへ接続するための Script の用意

MagicOnionSample/Sample.Unity/Assets/Scene/ フォルダに次のようにSampleController.csを作成する。

using Grpc.Core;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SampleController : MonoBehaviour
{
    private Channel channel;

    void Start()
    {
        this.channel = new Channel("localhost:12345", ChannelCredentials.Insecure);
    }

    async void OnDestroy()
    {
        await this.channel.ShutdownAsync();
    }
}

Start() でサーバーへ接続し、OnDestroy() で切断するようになっている。

SampleController という空のゲームオブジェクトを作成し、SampleControllerをアタッチする。

Unity ⇔ サーバー間のコード共有の動作確認用の class の用意

MagicOnionSample/Sample.Unity/Assets/Scripts/ServerShared以下に置いてあるスクリプトを全て共有する設定を行う。

MagicOnionSample/Sample.Unity/Assets/Scripts/ServerShared/MessagePackObjects/ に次のようにPlayer.csを作成する。

using MessagePack;
using UnityEngine;

namespace Sample.Shared.MessagePackObjects
{
    [MessagePackObject]
    public class Player
    {
        [Key(0)]
        public string Name { get; set; }
        [Key(1)]
        public Vector3 Position { get; set; }
        [Key(2)]
        public Quaternion Rotation { get; set; }
    }
}

サーバー側の設定

サーバーのソリューションを作成

Riderで File -> New -> .NET Core -> Console Application

Solution nameをSample.Serverに設定

Project DirectoryはMagicOnionSample/ 直下になるようにする。

Put solution and project in the same directoryにチェック

Sharedプロジェクトを作成

RiderのExplorerで、Sample.Serverを右クリック -> Add -> Add New Project -> .NET Core -> Class Library

Project NameをSample.Sharedとする。

Createした後に、Class1.csを削除

Sample.Sharedに必要なパッケージを追加

RiderでSample.Sharedを右クリック -> Manage NuGet Packages

  • MagicOnion.Hosting
  • MessagePack.UnityShims

を追加

Sample.Shared に対してUnity側のコードを読み込む設定を行う

RiderでSample.Shared.csprojを右クリック -> Edit -> Edit 'Sample.Shared.csproj'

Sample.Shared.csprojに次の文を追加する

<ItemGroup>
    <Compile Include="..\..\Sample.Unity\Assets\Scripts\ServerShared\**\*.cs" LinkBase="LinkFromUnity" />
</ItemGroup>

Sample.Serverの設定

Sample.Serverに必要なパッケージを追加

RiderでSample.Serverを右クリック -> Manage NuGet Packages

  • MagicOnion.Hosting
  • MessagePack.UnityShims

を追加

Sample.Sharedへの参照を追加

RiderでSample.Server.csprojを右クリック -> Edit -> Edit 'Sample.Server.csproj'

Sample.Server.csprojに次の文を追加

<ItemGroup>
    <ProjectReference Include=".\Sample.Shared\Sample.Shared.csproj" />
</ItemGroup>

動作確認

Program.csの編集

Sample.ServerのProgram.csを次のように上書きする。

using Grpc.Core;
using MagicOnion.Hosting;
using MagicOnion.Server;
using Microsoft.Extensions.Hosting;
using System.Threading.Tasks;

namespace Sample.Server
{
    class Program
    {
        static async Task Main(string[] args)
        {
            GrpcEnvironment.SetLogger(new Grpc.Core.Logging.ConsoleLogger());

            await MagicOnionHost.CreateDefaultBuilder()
                .UseMagicOnion(
                    new MagicOnionOptions(isReturnExceptionStackTraceInErrorDetail: true),
                    new ServerPort("0.0.0.0", 12345, ServerCredentials.Insecure))
                .RunConsoleAsync();
        }
    }
}

参考