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 5 years have passed since last update.

【Unity】Unity Editorかスマホ実機か判別して処理を分ける

Last updated at Posted at 2019-07-19

はじめに

スマホ実機で毎回動作確認するのが面倒だったので、Unity Editorの場合はキーボード操作ができるようにしました。

サンプルスクリプト1

Main CameraにAttachして使用することを想定したスクリプトです。
Unity Editorの場合は、方向キーで視点移動ができるように、
スマホ実機(Unity Editor以外)の場合は、端末を傾けて視点移動ができるようになっています。

GyroController1
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GyroController : MonoBehaviour
{
    // キーボード操作用
    private Vector3 rotate;
    void Start()
    {
        // 動作確認用のログ
        Debug.Log("started");

        // Unityエディタと実機で処理を分ける
        if (Application.isEditor)
        {
            rotate = transform.rotation.eulerAngles;
            Debug.Log("no-smartphone");
        }
        else 
        {
            Input.gyro.enabled =true;
        }
    }

    void Update()
    {
        // キーボードで視点変更
        float speed = Time.deltaTime * 100.0f;

        // PCは矢印キーで視点変更
        if (Application.isEditor)
        {
            if (Input.GetKey(KeyCode.LeftArrow))
            {
                rotate.y -= speed;
            }
            if (Input.GetKey(KeyCode.RightArrow))
            {
                rotate.y += speed;
            }
            if (Input.GetKey(KeyCode.UpArrow))
            {
                rotate.x -= speed;
            }
            if (Input.GetKey(KeyCode.DownArrow))
            {
                rotate.x += speed;
            }
            transform.rotation = Quaternion.Euler(rotate);
        }
        // スマホはジャイロで視点変更
        else
        {
            Quaternion gratitude = Input.gyro.attitude;
            gratitude.x *= -1;
            gratitude.y *= -1;
            transform.localRotation = Quaternion.Euler(90, 0, 0) * gratitude;
            
        }
    }
}

サンプルスクリプト2

コメントにて教えていただいた方法です。
コメントそのまま転載させてもらうと、

長所=デバッグコマンドなどをapkに埋め込まなくて済みます
短所=実機専用コード部分にインテリセンス効かなくなります

書いてみましたが、インデントに自信はありません。

GyroController2
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GyroController : MonoBehaviour
{
    // キーボード操作用
# if UNITY_EDITOR || UNITY_STANDALONE
    private Vector3 rotate;
# endif

    void Start()
    {
        // 動作確認用ログ1
        Debug.Log("started");
    #if UNITY_EDITOR || UNITY_STANDALONE
        rotate = transform.eulerAngles;
        // 動作確認用ログ2
        Debug.Log("non-smartphone");
    #else
        Input.gyro.enabled = true;
    #endif
    }

    void Update()
    {
        // PCの場合はキーボード、スマホはジャイロで視点変更
    #if UNITY_EDITOR || UNITY_STANDALONE
        float speed = Time.deltaTime * 100.0f;
        if (Input.GetKey(KeyCode.LeftArrow))
        {
            rotate.y -= speed;
        }
        if (Input.GetKey(KeyCode.RightArrow))
        {
            rotate.y += speed;
        }
        if (Input.GetKey(KeyCode.UpArrow))
        {
            rotate.x -= speed;
        }
        if (Input.GetKey(KeyCode.DownArrow))
        {
            rotate.x += speed;
        }
        transform.rotation = Quaternion.Euler(rotate);
    #else
        Quaternion gattitude = Input.gyro.attitude;
        gattitude.x *= -1;
        gattitude.y *= -1;
        transform.localRotation = Quaternion.Euler(90, 0, 0) * gattitude;
    #endif
    }
}

最後に

Unity初心者なので、より良い方法や間違いがあったら、是非教えてほしいです。

参考文献

Platform dependent compilation

1
1
4

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?