![](https://scombu.com/wp-content/uploads/2022/04/827e9b01485a9aa03bda9cd1b73e1970-150x150.png)
かなり久しぶりにUnityで開発記事書きます。
アカウントの作成→Unityのインストール→エディターの設定→簡単なシーン作成→実機での確認という流れで説明しますね。玉転がしアプリをつくってみましょう
玉転がしサンプル 完成イメージ
Androidスマホのジャイロ機能を使って、画面上の赤いボールをコントロールするアプリをサンプルっとして作っていきましょう。
Unity アカウント作成
![](https://cdn.sanity.io/images/fuvbjjlp/production/9a58ef0b0a39424aaf70dd4c30539b477ae54697-1200x630.png)
![](https://cdn.sanity.io/images/fuvbjjlp/production/9a58ef0b0a39424aaf70dd4c30539b477ae54697-1200x630.png)
![](https://scombu.com/wp-content/uploads/2022/02/unityStudy1-1024x592.png)
![](https://scombu.com/wp-content/uploads/2022/02/unityStudy1-1024x592.png)
Unity公式サイトの右上の人型アイコンをクリックしてCreate a Unity IDを選択してアカウントを作成してください。
Unity インストール
![](https://cdn.sanity.io/images/fuvbjjlp/production/febce456ba09dc5312d441a45ed6fc51def643a9-600x337.jpg)
![](https://cdn.sanity.io/images/fuvbjjlp/production/febce456ba09dc5312d441a45ed6fc51def643a9-600x337.jpg)
こちらからUnityをダウンロードしてインストールします。
Visual Studio Codeのインストール
![](https://code.visualstudio.com/opengraphimg/opengraph-home.png)
![](https://code.visualstudio.com/opengraphimg/opengraph-home.png)
VS Codeをダウンロードしてインストールする。
エディター設定
![](https://scombu.com/wp-content/uploads/2022/02/unityStudy6.png)
![](https://scombu.com/wp-content/uploads/2022/02/unityStudy6.png)
VS Codeで画面左部分の拡張機能アイコンをクリックして以下の拡張機能をインストールしていく。
![](https://scombu.com/wp-content/uploads/2022/02/unityStudy2.png)
![](https://scombu.com/wp-content/uploads/2022/02/unityStudy2.png)
![](https://scombu.com/wp-content/uploads/2022/02/unityStudy3.png)
![](https://scombu.com/wp-content/uploads/2022/02/unityStudy3.png)
![](https://scombu.com/wp-content/uploads/2022/02/unityStudy4.png)
![](https://scombu.com/wp-content/uploads/2022/02/unityStudy4.png)
![](https://scombu.com/wp-content/uploads/2022/02/unityStudy5.png)
![](https://scombu.com/wp-content/uploads/2022/02/unityStudy5.png)
- C# for Visual Studio Code (Powered by OmniSharp)
- MonoBehaviour Snippets
- Debugger for Unity
- Unity Code Snippets
![](https://scombu.com/wp-content/uploads/2022/02/unityStudy7-1024x620.png)
![](https://scombu.com/wp-content/uploads/2022/02/unityStudy7-1024x620.png)
![](https://scombu.com/wp-content/uploads/2022/02/unityStudy8-1024x379.png)
![](https://scombu.com/wp-content/uploads/2022/02/unityStudy8-1024x379.png)
時々コード補完が効かなくなる事があります。若干不安定なのですがその時はuse global monoの設定を切り替えてたりしてalwaysに最終的に戻して再起動すると機能することがありました。
シーン作成
![](https://scombu.com/wp-content/uploads/2022/02/unityStudy9-1024x657.png)
![](https://scombu.com/wp-content/uploads/2022/02/unityStudy9-1024x657.png)
Panelを一枚、その上に縁取る様にCubeを四つ、真ん中にSphereを一つ設置します。
マテリアルを二つRedとBlueを作成してそれぞれの色を設定し枠にBlueをボールにRedを設定します。
![](https://scombu.com/wp-content/uploads/2022/02/unityStudy10-481x1024.png)
![](https://scombu.com/wp-content/uploads/2022/02/unityStudy10-481x1024.png)
ボールにはRigebody,Sphere Colliderを付けます。スクリプトは以下のballRunスクリプトをつけましょう。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ballRun : MonoBehaviour
{
private float speed = 10.0f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
var dir = Vector3.zero;
dir.x=Input.acceleration.x;
dir.z=Input.acceleration.y;
dir *= speed;
Rigidbody rb = this.GetComponent<Rigidbody>();
rb.AddForce(dir);
}
}
実機で実行
実機の開発者向け設定 Oppo RenoA 編
「設定」→「端末情報」→「バージョン」→「ビルド番号7回タップ」
![](https://scombu.com/wp-content/uploads/2022/02/unityStudy13-473x1024.png)
![](https://scombu.com/wp-content/uploads/2022/02/unityStudy13-473x1024.png)
「設定」→「その他設定」→「開発者オプション」→「開発者オプションをオンにする」
Build Setting
![](https://scombu.com/wp-content/uploads/2022/02/unityStudy11.png)
![](https://scombu.com/wp-content/uploads/2022/02/unityStudy11.png)
Build Settingsを開きます
![](https://scombu.com/wp-content/uploads/2022/02/unityStudy12-1024x1024.png)
![](https://scombu.com/wp-content/uploads/2022/02/unityStudy12-1024x1024.png)
PlatformでAndroidを選択してSwitch Platformボタンを押してしばらく待ちます。一分位かな
私の場合はMacbookなのでAndroid端末を繋いでBuild And Runボタンを押してしばらく待つと実機にアプリが転送されて実行されますので動作を確認しましょう。
コメント