LoginSignup
1
2

More than 5 years have passed since last update.

unity > Dictionaryをチェックしてあれば表示、なければweb検索処理 > Dictionary登録もできる

Last updated at Posted at 2015-08-25
動作確認
Unity 5.1.2-f1 on MacOS X 10.8.5

やろうとしていること。
1. 電話番号を入力
2. 検索ボタンを押した時、辞書に登録済みであれば、辞書から登録名を表示
3. 辞書になければwebから検索する。
4. 検索結果(または手入力)を辞書に登録もできる。

以下がそのコード

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Linq; // for Where
using System.Collections.Generic;

public class CheckButtonControl : MonoBehaviour {

    public const string kTitle = "<title>";
    public Text resText; // to show check result
    public InputField IFtelno; // for input telephone number
    public InputField IFinfo; // to show/input hospital name

    static private Dictionary<string,string> telbook = new Dictionary<string, string>();

    void Start() {
        IFtelno.text = "0729883121"; // TODO: remove // for test
    }

    string getHospitalName(string txt, string telno) {
        string removed = txt.Substring (kTitle.Length, 30);

        // check with first 2 characters
        string lhs = removed.Substring (0, 2);
        string rhs = telno.Substring (0, 2);
        if (lhs.Equals (rhs)) {
            return "not registered";
        }
        return removed;
    }

    string removeHyphen(string src) {
        var dst = new string (src.Where (c => !"-".Contains (c)).ToArray ());
        return dst;
    }

    bool hasObtainedTelNo(string src) {
        if (src.ToLower ().Contains ("not")) {
            return false;
        }
        return true;
    }

    IEnumerator checkHospitalTelephoneNumber() {
        string telno = IFtelno.text;

        // remove "-" from telno
        telno = removeHyphen (telno);

        if (telbook.ContainsKey (telno)) {
            IFinfo.text = "dic:" + telbook[telno];
            yield break;
        }

        string url = "http://www.jpcaller.com/phone/";
        WWW web = new WWW(url + telno);
        yield return web;

        string res = web.text;
        int pos = res.IndexOf (kTitle);
        resText.text = "not found";
        IFinfo.text = "";
        if (pos > 0) {
            res = getHospitalName(web.text.Substring(pos, 40), telno);
            resText.text = res;
            if (hasObtainedTelNo(res)) {
                IFinfo.text = resText.text;
            }
        }
    }

    void addDictionary(string telno, string name) {
        if (telbook.ContainsKey (telno) == false) {
            telbook.Add (telno, name);
            Debug.Log("added");
        }
    }

    public void CheckButtonOnClick() {
        StartCoroutine("checkHospitalTelephoneNumber");
    }

    public void AddButtonOnClick() {
        string telno = removeHyphen (IFtelno.text);
        addDictionary (telno, IFinfo.text);
    }
}

以下の結果は、まずはwebで検索したもの。
Scene_unity_-_150825-TELChecker_-_PC__Mac___Linux_Standalone__Personal_.jpg

この検索結果を[Add TEL no]ボタンで辞書登録できる。

一度辞書登録した後は、webがつながってなくても辞書から検索できる。
以下がその例。"dic:"という表示は辞書から検索したことを示す。

Scene_unity_-_150825-TELChecker_-_PC__Mac___Linux_Standalone__Personal_.jpg

これにあとはファイルの入出力機能を加えれば、電話番号から対応する名前があるかチェックするアプリのとりあえずの機能実装が完了する。

知人の電話番号の場合はweb検索できないが、以下で辞書登録できる。
- 上のInputFieldにその人の電話番号を入力
- 下のInputFieldにその人の名前を入力
- [Add TEL no]として辞書登録
これでいたずら電話やセールス電話を完全無視できる環境ができればいいなと思う。

機械の苦手な人が使えるように、ボタン数は極力減らしているつもり。
電話番号にはハイフンをつけても認識する。

DONE: 辞書をファイル出力する。アプリ起動時にそのファイルを読み込む。

上記を基にとりあえず必要そうな機能を追加したアプリをgithubに公開した。

Scene_unity_-_150825-TELChecker_-_PC__Mac___Linux_Standalone__Personal_.jpg

1
2
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
1
2