【Unity】どうぶつの森 自作 #3 〜装備切り替えUIの作成〜

2020年4月9日木曜日

Unity どうぶつの森自作

t f B! P L

前回はPlayerの移動を実装した。

装備切り替えUIの作成

このUIの作成にはアークタンジェントを用いてマウスの位置ベクトルからマウスが初期位置から動いた角度を求めた

アークタンジェントとは

・三角関数タンジェントの逆関数
アークタンジェントのグラフ

逆関数はプログラム的に言うと入力値と出力値を逆転させた関数である

本来タンジェントは
入力値:角度(ラジアン)
出力値:座標(位置ベクトル)
であるが今回は入力値(角度)の方を知りたいので、位置ベクトルを入れれば、角度か得られる関数
つまり
入力値:座標(位置ベクトル)
出力値:角度(ラジアン)
である関数がアークタンジェントである。

逆関数について、軽く


関数y=f(x)
において、yの値がさだまれはxの値がただ1つに定まる(関数f(x)が単射である)すなわちxがyの関数 
x=g(x) 
と表せる時、x=g(x)をものと関数y=f(x)の逆関数といい
y=f-1(x)
f-1(x) はfインバースX と読む
と表す
また、y=f(x)の逆関数y=f-1(x)y=x について対象である


詳しくは「高校数学の美しい物語」へ


実装


構成
装備インスペクター

Weapon List (weaponList.cs)

・マネージャー
ユーザからのマウスの角度を入手
→対応するiconのメソッドを実行させる

icon (waponList_icon.cs)

マネージャーより、メソッドが実行される。
Selected Image をアクティブな状態にする
Weapon Imageのspritの名前を返す

Selected Image

色 #00BFFF
iconに色をつけて、選択されていることをわかりやすくする。

Weapon Image

「装備の画像」をもつ。
iconにより、どの「装備の画像」をもつか決められている。

次(未作成)

C#スクリプト

weaponList.cs
using System.Collections;
using System.Linq;
using UnityEngine;
using static UnityEngine.Mathf;

public class WeaponList : MonoBehaviour {
    public WeaponList_icon[] icon_script = new WeaponList_icon[8];
    Vector2 startMousePos, currentMousePos;

    void Start () {
        //nullチェック
        if (icon_script.Count (x => x == null) != 0)
            Debug.LogError ("Not be set Script in button of Weapon List.");
    }
    public void Active () { //このスクリプトのSetActiveをtrueにするとき一緒に呼ぶ
        startMousePos = Input.mousePosition;
        StartCoroutine (inputMouse ());
    }
    public void Sleep () { //このスクリプトのSetActiveをfalseにするとき一緒に呼ぶ
        foreach (var i in icon_script) i.Sleep ();
    }

    int oldIndex;
    IEnumerator inputMouse () {
        oldIndex = 1; //初期値指定
        while (true) {
            currentMousePos = ((Vector2) Input.mousePosition) - startMousePos;

            //どのUIが選択されているかを取得
            int index = getIndexNum (currentMousePos);

            if (oldIndex != index) { //違うものが選択されたら
                icon_script[index].Active();
                icon_script[oldIndex].Sleep();

                Debug.Log(icon_script[index].WeaponName);

                oldIndex = index;
            }
            yield return null;
        }
    }
    //座標から、マウスがさすUIのインデックスを返す
    private int getIndexNum (Vector2 Pos) {

        var value = 8 * Atan2 (Pos.y, Pos.x);

        if (Abs (value) > 7 * PI) return 2;
        if (value < -5 * PI) return 3;
        if (value < -3 * PI) return 4;
        if (value < -2 * PI) return 5;
        if (value < PI) return 6;
        if (value < 3 * PI) return 7;
        if (value < 5 * PI) return 0;
        return 1;
    }
}
waponList_icon.cs
using UnityEngine;
using UnityEngine.UI;

public class WeaponList_icon : MonoBehaviour
{
    public Image selectedImage;

    public Image weaponImage;
    public string WeaponName {
        get { return weaponImage.sprite.name; }
    }
    private void Start() {
        if(selectedImage == null)   //nullチェック
            Debug.LogError("Not be set Selected Image in Button of Weapon List.");
    }
    public void Active() {  //このスクリプトのSetActiveをtrueにするとき一緒に呼ぶ
        selectedImage.gameObject.SetActive(true);
    }
    public void Sleep() {   //このスクリプトのSetActiveをfalseにするとき一緒に呼ぶ
        selectedImage.gameObject.SetActive(false);
    }
}

自己紹介

自分の写真
県立高校理数科2年
Unity2年目
Twitter
Note

人気の投稿

[Vim] coc.nvim + coc-clangdの構文チェックでboostのエラーが発生する

C++の拡張ライブラリ、Boost。 AtCoderでも使用が認められているライブラリです。 様々な便利機能が搭載されています。 競技プログラミングで使えるBoostライブラリ 初級編 から一部抜粋すると、 boost...

QooQ