LoginSignup
10
11

More than 5 years have passed since last update.

Easy WiFi Controllerであそんでみた

Posted at

ちょっとあこがれていたwifiでスマホをコントローラーにするプラグインが安かったので買ってしまった。

インポートしたところ

ClientとServerに入っているシーンが対になっています。
Clientフォルダに入っているのがコントローラーで、これをそれぞれビルドして端末などに入れます。
あとはServerの方をPCで起動するかスマホに入れて起動するだけでうごきます。

デモをいろいろいろいろな端末で動かしてみた

Androidをサーバーに、PCとIpodtouch2つをクライアント(コントローラー)にして動かしていたところ

ばっちり動きました。これで1人で楽しく遊べるね!

簡単にジャンケンでもつくってみる

とりあえずクライアント側を作る

GameObject->Easy WiFi Manager->Add EasyWiFiManagerでオブジェクトを作成

するとオブジェクトが作成されるのでそのインスペクタのPeer TypeをClientに
サーバーの場合はこれをServerにすればOK

コントローラーは用意されているので好きなものを配置する
PCでも動作するし、スマホでも動作するScriptがすでについております。
uGUIです

タッチパッドまであります。

とりあえずクライアント側はこんな感じで作成しました。
Connection Widgetは任意にWifiをつなげたりできるものです。
クライアント側はまったくノーコーディングでした。らくちんらくちん!

各ボタンにアタッチされているScriptのControl Nameだけ変更する必要があります。
これがサーバーがわで設定する名前になります

サーバー側を作る

サーバーがわはClient側でもつくったEasyWiFiManagerをServerで作成します。
その後各ボタンの判定のScriptをどこかにアタッチします。
数が多い場合は独自に作成しても良さそうですが、めんどくさいので今回は用意されている
Cusstom Button Server Controller.csを使いました。

こんな感じでClientでControl Nameに指定した値をControlに設定し、Notify Methodに呼び出すメソッド名を記載します。
SendMessageでその関数が呼ばれるので処理するScriptを同じ所にアタッチする必要があります。
あとは処理を書くだけ
ざっと書いたものですが


using UnityEngine;
using System.Collections;
using EasyWiFi.Core;
using UnityEngine.UI;

public class GameManager : MonoBehaviour {

    public Text label;
    public Text resultLabel;
    public Image player1Img;
    public Image player2Img;
    public Sprite[] handTexture;

    private Hand player1Hand;
    private Hand player2Hand;

    private enum Hand
    {
        NONE = 0,
        GU,
        CHOKI,
        PA,
    }

    void Awake()
    {
        Init();
    }

    private void Init()
    {
        label.text = "ジャンケン";
        resultLabel.text = "";
        player1Hand = Hand.NONE;
        player2Hand = Hand.NONE;
    }

    private void Result()
    {
        //if(player1Hand == Hand.NONE || player2Hand == Hand.NONE) return;
        if(player1Hand == Hand.NONE)
        {
            resultLabel.text = "Player1を待っています・・・";
            return;
        }
        else if(player2Hand == Hand.NONE)
        {
            resultLabel.text = "Player2を待っています・・・";
            return;
        }

        switch(player1Hand)
        {
        case Hand.GU:
            player1Img.sprite = handTexture[0];
            break;
        case Hand.CHOKI:
            player1Img.sprite = handTexture[1];
            break;
        case Hand.PA:
            player1Img.sprite = handTexture[2];
            break;
        }

        switch(player2Hand)
        {
        case Hand.GU:
            player2Img.sprite = handTexture[0];
            break;
        case Hand.CHOKI:
            player2Img.sprite = handTexture[1];
            break;
        case Hand.PA:
            player2Img.sprite = handTexture[2];
            break;
        }
        label.text = "ポイ!";

        if(player1Hand == player2Hand)
        {
            resultLabel.text = "引き分け";
        }
        else if((int)player1Hand - (int)player2Hand == -1 || (int)player1Hand - (int)player2Hand == 2)
        {
            resultLabel.text = "Player1の勝ち";
        }
        else
        {
            resultLabel.text = "Player2の勝ち";
        }   
        Invoke ("Init", 3f);
    }


    public void GuClick(ButtonControllerType button)
    {
        bool isPressed = button.BUTTON_STATE_IS_PRESSED;

        if (isPressed && player1Hand == Hand.NONE)
        {
            player1Hand = Hand.GU;
            Result();
        }
    }

    public void ChokiClick(ButtonControllerType button)
    {
        bool isPressed = button.BUTTON_STATE_IS_PRESSED;

        if (isPressed && player1Hand == Hand.NONE)
        {
            player1Hand = Hand.CHOKI;
            Result();
        }
    }

    public void PaClick(ButtonControllerType button)
    {
        bool isPressed = button.BUTTON_STATE_IS_PRESSED;

        if (isPressed && player1Hand == Hand.NONE)
        {
            player1Hand = Hand.PA;
            Result();
        }
    }

    public void GuClick2(ButtonControllerType button)
    {
        bool isPressed = button.BUTTON_STATE_IS_PRESSED;

        if (isPressed && player2Hand == Hand.NONE)
        {
            player2Hand = Hand.GU;
            Result();
        }
    }

    public void ChokiClick2(ButtonControllerType button)
    {

        bool isPressed = button.BUTTON_STATE_IS_PRESSED;

        if (isPressed && player2Hand == Hand.NONE)
        {
            player2Hand = Hand.CHOKI;
            Result();
        }
    }

    public void PaClick2(ButtonControllerType button)
    {
        bool isPressed = button.BUTTON_STATE_IS_PRESSED;

        if (isPressed && player2Hand == Hand.NONE)
        {
            player2Hand = Hand.PA;
            Result();
        }
    }
}

サーバー側の状態

クライアントをIpodTouch2台にいれて一人むなしく遊んでいる様子。

10
11
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
10
11