FC2ブログ
    02 «1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.» 04

    ハルシオンシステムの気ままBlog

    株式会社ハルシオンシステムのメンバーが送る、UnityやらJavaやらの技術的話題から、自社開発のアプリの宣伝とかとかのブログです。ほんと気ままにいきたいと思います。更新日は毎週 月 木でっす!

     

    【Unity】NGUIを使ったストーリー会話ウィンドウの作成!? 

    こんにちは!月曜の坂内です。

    みなさん元気にお過ごしでしょうか!?
    金曜の雪もすごかったっすね!
    帰れない人とかいたらしいっす。

    また今週水曜とかに雪降りそうとか言われてますが、是非積もらない方向でお願いしたい!
    交通機関麻痺がやばいっすからね!


    はいっ!今日はよくある会話ウィンドウ?的なものをUnity+NGUIで作ってみましたので、
    といっても、NGUIのサンプルにあったやつをちゃちゃっとパクってアレンジしたくらいですが!

    ちなみにストーリーシナリオ書いてくれるライターさん募集です。
    1,2kbくらいのショートなものなんですけどね!

    あ、本題に入ります。

    まずは~ 会話を流す場所を確保!

    それと、しゃべってるキャラのSpriteと、ページ送りが分かりやすいように
    矢印もつけときましょう。
    Scriptはこのあと書きます。



    で、次のプログラムをパクってきて・・・
    using UnityEngine;
    using System.Collections;

    public class TypewriterEffect : MonoBehaviour {
    public int charsPerSecond = 40;
    UILabel mLabel;
    string mText;
    int mOffset = 0;
    float mNextChar = 0f;

    public void ResetText(){
    mOffset = 0;
    mNextChar = 0f;
    }

    void Update (){
    if (mLabel == null){
    mLabel = GetComponent();
    mLabel.supportEncoding = false;
    mLabel.symbolStyle = NGUIText.SymbolStyle.None;
    mText = mLabel.processedText;
    }
    if (mOffset < mText.Length){
    if (mNextChar <= RealTime.time){
    charsPerSecond = Mathf.Max(1, charsPerSecond);
    float delay = 1f / charsPerSecond;
    if(mLabel.alpha == 0f){
    mLabel.alpha = 1f;
    }
    char c = mText[mOffset];
    if (c == '.' || c == '\n' || c == '!' || c == '?') delay *= 4f;
    mNextChar = RealTime.time + delay;
    mLabel.text = mText.Substring(0, ++mOffset);
    }
    }else{
    GameObject.Find("Story").GetComponent().ViewArrow();
    Destroy(this);
    }
    }
    }

    こんなクラスを作ります。
    んで、こいつをラベルにくっつけときます。



    で、一番あたまの「Story」に次ぎのクラスをつけときます。

    using UnityEngine;
    using System.Collections;

    public class StoryController : MonoBehaviour {
    public UISprite StoryCharaImage;
    public UILabel StoryText;
    public GameObject StorySet;
    public UISprite Arrow;

    int activeNo;
    ArrayList activeStory;
    bool pushArrowFlg;

    void Start(){
    StoryStart(1);
    }

    public void StoryStart(int storyNo){
    switch(storyNo){
    case 1:
    StoryPart_1();
    break;
    }
    iTween.ValueTo(gameObject,iTween.Hash("from",0,"to",1,"looptype",
    iTween.LoopType.pingPong,"time",1f,"onupdate","BlinkArrow"));
    }

    void BlinkArrow(float value){
    Arrow.alpha = value;
    }

    void StoryPart_1(){
    activeStory = new ArrayList();
    StoryCharaImage.spriteName = "chara";
    MakeTestStory();
    ProcStory();
    }

    void MakeTestStory(){
    string testStory = "あいうえお かきくけこ \r\nわがはいはなんである?";
    activeStory.Add(testStory);
    testStory = "いやいや、テストストーリーである。
    \r\n\r\nちゃんとうごいていますか?\r\nはい。";
    activeStory.Add(testStory);
    }

    void ProcStory(){
    // はじめは矢印消す.
    Arrow.enabled = false;
    activeNo = 0;
    StoryText.text = (string)activeStory[activeNo];
    StoryText.gameObject.GetComponent().ResetText();
    activeNo++;
    }

    public void CloseStory(){
    Destroy(StorySet);
    }

    public void ViewArrow(){
    Arrow.enabled = true;
    }

    void Update(){
    if(pushArrowFlg){
    if(StoryText.gameObject.GetComponent() == null){
    StoryText.alpha = 0f;
    StoryText.gameObject.AddComponent();
    Arrow.enabled = false;
    StoryText.text = (string)activeStory[activeNo];
    }
    activeNo++;
    pushArrowFlg = false;
    }
    }

    public void TapStoryWindow(){
    if(Arrow.enabled){
    // 会話が終わっているか判定.
    if(activeNo >= activeStory.Count){
    CloseStory();
    }
    // 次の会話に進む.
    pushArrowFlg = true;
    }
    }
    }

    んで、ラベルには次のように、Colliderとページ送り用のButton Messageをつけておきます。
    MessageにはStoryControllerのTapStoryWindowメソッドを指定します。



    で、StoryにつけたStoryControllerの各Publicの場所に、それぞれのオブジェクトをアサインしときます。



    以上!

    一応これで、それっぽい動きをしてくれます!
    戻るとかは無いんですけどね・・・・

    で、大事なのでもう一度言っておきます。
    ストーリーシナリオ書いてくれるライターさん募集です。
    1,2kbくらいのショートなものなんですけどね!

    ストーリー自体はArrayListで管理してますが、実際はテキストファイルから
    ストーリーを取り出して、ArrayListにぶっこむ感じでしょうか?
    そんな感じでできました!

    ということで、本日の開発ブログは終わり!

    Category: 開発日記(Unity)

    tb 0 : cm 0   

    コメント

    コメントの投稿

    Secret

    トラックバック

    トラックバックURL
    →http://halcyonsystemblog.jp/tb.php/76-b4773450
    この記事にトラックバックする(FC2ブログユーザー)