ドキュメント

​
​

Development

User Acquisition

Monetization

産業

Cloud Code

Open Unity Dashboard

Cloud Code

LiveOps
​
​
Cloud Code
  • Overview
  • Get started
  • Server authority
  • Cloud Code C# modules
  • Cloud Code JavaScript scripts
    • Overview
    • Get started
    • Concepts
    • Tutorials
      • Run scripts
      • Write scripts
      • Interact with cross-player data
      • Integrate with CI/CD
      • Use Access Control
      • Test run a script
      • Integrate with other Unity Services
      • Integrate with external services
      • Use Cases
        • Server time anti-cheat
        • Redeemable coupons
        • Daily Rewards
        • Use Lobby
        • Advanced use cases
    • Reference
  • Logging
  • Use-case samples
  • Privacy and consent
  1. Cloud Code
  2. ユースケース

Lobby の使用

Use Cloud Code and Lobby service together to trigger server-side actions when players join lobbies.
読み終わるまでの所要時間 6 分
最終更新 5ヶ月前

Lobby と Cloud Code を一緒に使用して、ゲーム内に一般的なトリガーを定義できます。ゲームロビーですでにマッチを開始する準備ができているソリューションについて考えます。準備ができたゲームロビーに反応して、以下のスクリプトをトリガーできます。この例は、Remote Config とのインテグレーションも備えています。
この例に従う前に、Unity Dashboard を通じて Lobby と Cloud Code の両方を有効にし、必要な SDK をインストールするステップに従います。サービス認証を使用してプロジェクトに ロビーを作成 し、その ID をメモしておきます。これは、Cloud Code を通じて行うことができます (Unity Lobby の例を確認してください)。
以下の例は、スクリプトに 2 つのパラメーター (ロビーの作成に使用されるサービス ID と、ロビー ID) を付与します。ロビーを取得し、そのメタデータにいくつかの変更を加えて更新します。これらの変更は以下のとおりです。
  • ロビーをロックして、それ以上新規のプレイヤーが参加できないようにします。
  • ロビーのプレイヤー ID を使用して、ゲームのランダムな "プレイヤーの順序" を構成します。
  • Remote Config で定義された
    LOBBY_CURRENT_MAPS
    設定を使用して、ゲームのマップをランダムに選択します。スクリプトは、成功または失敗を示す基本的なブーリアンを返します。
JavaScript
const _ = require("lodash-4.17");const { LobbyApi, DataObjectVisibilityEnum } = require("@unity-services/lobby-1.2");const { SettingsApi } = require("@unity-services/remote-config-1.1");module.exports = async ({params,context,logger}) => { const { serviceId, lobbyId } = params; const { projectId, environmentId, playerId } = context; const lobbyApi = new LobbyApi(context); const remoteConfig = new SettingsApi(context); try { const {data: lobby} = await lobbyApi.getLobby(lobbyId, serviceId); return setUpGame(remoteConfig, projectId, environmentId, serviceId, lobbyApi, logger, lobby); } catch (err) { logger.error(`Failed to set up lobby: ${err.response.data.detail}`); return false; }};// Updates a lobby to set up for the start of a game, including reading a value from Remote Config to randomly assign to the lobby.async function setUpGame(remoteConfig, projectId, environmentId, serviceId, lobbyApi, logger, lobby) { let playerIds = []; for (let i = 0; i < lobby.players.length; i++) { playerIds.push(lobby.players[i].id); } // Generate a turn order for the game by shuffling the player IDs. _.shuffle(playerIds); // Load all of the maps that are currently configured as available in Remote Config and pick a random one for this game. let maps = getCurrentMapsFromRemoteConfig(remoteConfig, projectId, environmentId) if (maps == null) { throw "Maps loaded from Remote Config are null."; } let gameMap = _.sample(maps); try { const updateResponse = await lobbyApi.updateLobby( lobby.id, serviceId, null, { isLocked: true, // Lock the lobby so that new players cannot join. hostId: playerIds[0], // Transfer host ownership from the service to one of the players. data: { "playerOrder": { value: playerIds.toString(), visibility: DataObjectVisibilityEnum.Member // Set the visibility of the player order so that only lobby members can access it. }, "map": { value: gameMap, visibility: DataObjectVisibilityEnum.Public // Set the visibility of the game map so that anyone who views the lobby can access it. } } } ); return true; } catch (err) { throw err; } return lobbyUpdateSuccess;}// Loads the Remote Config JSON value for LOBBY_CURRENT_MAPS and returns the array of maps inside of it.// If the JSON is invalid, the return value is null.async function getCurrentMapsFromRemoteConfig(remoteConfig, projectId, environmentId) { const currentMapsId = 'LOBBY_CURRENT_MAPS'; const remoteConfigResponse = await remoteConfig.assignSettingsGet( projectId, environmentId, 'settings', [currentMapsId] ); let mapsJSON = remoteConfigResponse?.data?.configs?.settings?.[currentMapsId]; if (!mapsJSON || !mapsJSON.maps || mapsJSON.maps.length == 0) { return null; } return mapsJSON.maps;}// Uncomment the code below to enable the inline parameter definition// - Requires Cloud Code JS dev environment setup with NodeJS (https://docs.unity3d.com/Packages/com.unity.services.cloudcode@latest/index.html?subfolder=/manual/Authoring/javascript_project.html)//// module.exports.params = {// serviceId: { type: "String", required: true },// lobbyId: { type: "String", required: true },// };
注
ノート: Unity エディターを使用してスクリプトを管理している場合は、スクリプトの下部にあるコードのコメントを外して、必要な
serviceId
および
lobbyId
パラメーターをスクリプト内で宣言できます。スクリプト内パラメーターの管理方法の詳細については、Unity エディター 内でのスクリプトパラメーターの変更に関するページを参照してください。
ここでの Remote Config JSON は単純な構造を持ちますが、この設定例を拡張して、よりきめ細かくゲームをコントロールできます。同様に、例えば以下のように、ゲームの設定に加えて検討できる、Cloud Code のさまざまなトリガーがあります。
  • メタデータ値を調整し、文字列をサニタイズするためのロビー作成のトリガー。
  • Cloud Save プレイヤーデータを使用した、プレイヤーのクエリ結果のフィルター処理、参加の承認/拒否、およびメタデータの設定。
  • Economy データを使用したプレイヤーまたはロビーのメタデータの設定。

Copyright © 2026 Unity Technologies
法規事項プライバシーポリシークッキーDocumentation Terms of Use私の個人情報を販売または共有しないプライバシーに関する選択 (クッキー設定)

"Unity" の名称、Unity のロゴ、およびその他の Unity の商標は、米国およびその他の国における Unity Technologies またはその関係会社の商標または登録商標です (詳しくはこちら)。その他の名称またはブランドは該当する所有者の商標です。

一部のページは利便性向上のため機械翻訳を使用しており、内容に不正確な表現が含まれる場合があります。内容に齟齬または不一致が生じた場合は、英語版を正本とします。

  • このページ
    • JavaScript


このページの問題を報告する