1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

unityroomで得点をツイートする実装例【初心者向け】

Last updated at Posted at 2022-11-02

はじめに

基本的には↓ここを読むだけで出来る筈です。
naichilab/unityroom-tweet: WebGLからツイートするサンプル

初心者向けに実装例を書いておきます。
※あくまで1例に過ぎず、幾らでも実装方法があります

サンプルゲーム

ブラウザ上で遊べます↓
https://unityroom.com/games/missile_interceptor

実装例

  • 「GameOverObjects」という空のオブジェクトがゲームオーバー時にSetActiveで 「非アクティブ」→「アクティブ」になる。
    image.png

  • それにより子供のゲームオーバーテキストやリトライボタン、ツイートボタンが表示される
    image.png

  • 適当なオブジェクトにこのスクリプトをアタッチする(ツイートボタン自身でもよい)

ResultTwitter.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ResultTwitter : MonoBehaviour
{
    // ※ここの初期値ではなく、インスペクタ上の値を変えてください
    public string gameID = "unityroom_game_id"; // unityroom上で投稿したゲームのID
    public string tweetText1 = "あなたの得点は";
    public string tweetText2 = "点でした。";
    public string hashTags = "#unityroom #ゲームタイトル"; //#unity1week";

    // ※ スコアマネージャーというものが存在すると仮定します
    // 貴方の作成しているゲームの事情に合わせてください
    // インスペクタ上でスコアマネージャーコンポーネントを持つゲームオブジェクトをセット
    public ScoreManager scoreManager;

    // ツイートボタンから呼び出す公開メソッド
    public void Tweet()
    {
        int score = scoreManager.GetScore(); // ※ 何らかの方法でスコアの値を取得(貴方のゲーム事情に合わせてください)
        naichilab.UnityRoomTweet.Tweet(gameID, tweetText1 + score + tweetText2 + hashTags);
    }
}
  • インスペクタの設定例
    image.png
    Score Manager の欄は ( ScoreManagerというコンポーネントがあると仮定 ) ScoreManagerコンポーネントの付いているオブジェクトをドラッグ&ドロップでセット

  • ボタンの設定
    On Click 欄の「+ボタン」を押して、赤い丸のところに ResultTwitter コンポーネントをアタッチしているオブジェクトをドラッグ&ドロップでセット(ここではボタン自身に付けているのでボタン自身をセット)、青い丸のところで呼び出す公開関数を選びます。
    これにより、ボタンを押されたときにこの関数が呼び出されツイートされます。
    image.png

これで、ゲームオーバー時にツイートボタンを押せばスコアの値をツイートできます。
image.png

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?