ロバメモ - 素人のUnity覚書と奮闘記

素人のUnity覚書と奮闘記

スクリプトごと使いまわしできる アルファベットボタン

f:id:nico-taniku:20170618130419p:plain:w500
下記の数字ボタンのアルファベットバージョンになります。
使い方などの詳細は、数字ボタンバージョンにて。
robamemo.hatenablog.com

配置

f:id:nico-taniku:20170618130438p:plain:w500

Dectionaryの作成

Global(シングルトン)に作成。

[HideInInspector]
public Dictionary<string,int[]> AlphabetButtonDictionary = new Dictionary<string,int[]> { };

スクリプト

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class AlphabetButton : MonoBehaviour
{
    [SerializeField][Header ("答え")]string answer;
    [SerializeField][Header ("ボタン識別番号 0〜")]int btnNumber;
    [SerializeField][Header ("長押し判定するまでの秒数")]float longPress = 0.5f;
    [SerializeField][Header ("文字を送る速度")]float speed = 0.2f;
    [SerializeField][Header ("Dictionary Key")]string keyName;

    string alphabet = "ABCDEFGIHJKLMNOPQRSTUVWXYZ";
    Text[] texts;
    bool flag = false;
    float passtime;
    bool next = false;

    void Awake ()
    {
        texts = new Text[3];
        for (int i = 0; i < texts.Length; i++) {
            texts [i] = transform.Find ("mask/Text" + i).GetComponent<Text> ();
        }

        passtime = longPress;
    }

    void Start ()
    {
        //Dictionaryに追加
        if (!Global.instance.AlphabetButtonDictionary.ContainsKey (keyName)) {
            int[] array = new int[answer.Length];
            for (int i = 0; i < array.Length; i++) {
                array [i] = 25;
            }
            Global.instance.AlphabetButtonDictionary.Add (keyName, array);
        }
        int index = Global.instance.AlphabetButtonDictionary [keyName] [btnNumber];
        for (int i = 0; i < texts.Length; i++) {
            texts [i].text = alphabet.Substring (index, 1);
            index++;
            if (index == alphabet.Length) {
                index = 0;
            }

        }
    }

    void Update ()
    {
        if (flag) {
            passtime -= Time.deltaTime;
            if (passtime <= 0.0) {
                passtime = speed;
                SetAlphabet ();
            }
        }
    }

    public void OnDown (bool nextFlag)
    {
        flag = true;
        next = nextFlag;
        SetAlphabet ();

    }

    public void OnUp ()
    {
        flag = false;
        passtime = longPress;
        next = false;
    }

    void SetAlphabet ()
    {
        if (next) {
            int index = alphabet.IndexOf (texts [0].text);                    
            for (int i = 0; i < texts.Length; i++) {
                index++;
                if (index == alphabet.Length) {
                    index = 0;
                }
                texts [i].text = alphabet.Substring (index, 1);
                if (i == 0) {
                    Global.instance.AlphabetButtonDictionary [keyName] [btnNumber] = index;
                    PlayerPrefs.SetInt ("alphabet_input" + btnNumber, index);
                }
            }
        } else {
            int index = alphabet.IndexOf (texts [2].text);    
            for (int i = texts.Length - 1; i >= 0; i--) {
                index--;
                if (index == -1) {
                    index = alphabet.Length - 1;
                }
                texts [i].text = alphabet.Substring (index, 1);
                if (i == 0) {
                    Global.instance.AlphabetButtonDictionary [keyName] [btnNumber] = index;
                    PlayerPrefs.SetInt ("alphabet_input" + btnNumber, index);
                }
            }
        }
    }

    public bool OnEnter ()
    {
        bool ans = false;
        string str = "";

        foreach (int value in Global.instance.AlphabetButtonDictionary[keyName]) {
            int index = value + 1;
            if (index == alphabet.Length) {
                index = 0;
            }
            str += alphabet.Substring (index, 1);
        }
        if (answer == str) {
            //正解
            ans = true;
        }
        return ans;
    }

}

Enterボタンのスクリプト

[SerializeField]AlphabetButton AB;

public void OnEnterButton ()
{
    if (AB.OnEnter ()) {
        Debug.Log ("正解");
    } else {
        Debug.Log ("不正解");
    }
}

Keyを追加

先に作成していたDictionaryにキーを追加する。
このキーが、施錠ごとの識別子になる。

書式は、

{ "キー名", new int[桁数]{ 値は25で、桁数と同じ数をつくる } }

複数使う場合は、カンマで区切る。

 [HideInInspector]public Dictionary<string,int[]> AlphabetButtonDictionary = new Dictionary<string,int[]> {
        { "door_lock",new int[4]{ 25, 25, 25, 25 } },
        { "drawer_lock",new int[4]{ 25, 25, 25, 25 } },
    };

f:id:nico-taniku:20170618130438p:plain:w500
上の画像のように、Key Nameに作成したキーの名前を入れる。