【Unity】選択したテキストを反映する

2019/03/06更新

ScrollViewで入力ボタンを設置する所まで出来たので、
入力ボタンを押せばテキストが反映されるようにして行こうと思います。

まず、各ScrollView呼び出しのButtonTextに選択したテキストを反映してみます。


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

public class InputFoamManager : MonoBehaviour {

public GameObject ImageFome; //入力フォーム取得
public GameObject CARDView; //対戦カードView取得
public GameObject ChView; //放送ChView取得
public GameObject TimeView; //放送時間View取得

public GameObject TextCARD; //CARDViewボタンのテキスト取得
public GameObject TextCh; //ChViewボタンのテキスト取得
public GameObject TextTime; //TimeViewボタンのテキスト取得

private int ViewBt; //ビューボタンの番号変数

private string CARDText,ChText,TimeText,DayText; //各テキスト


// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

}

//各ScrollView呼び出し
public void ScrollViewCall(int Bt)
{
//いずれかのViewが開いてる時は他のViewは開かない
if (CARDView.activeSelf || ChView.activeSelf || TimeView.activeSelf)
{
return;
}

ViewBt = Bt; //クラス内でボタンナンバーを共有

switch (Bt)
{
case 1:
CARDView.SetActive(true);
break;

case 2:
ChView.SetActive(true);
break;

case 3:
TimeView.SetActive(true);
break;
}

}



//入力フォームを閉じる
public void Pushclose()
{
//いずれかのViewが開いている時はフォームを閉じない
if (CARDView.activeSelf || ChView.activeSelf || TimeView.activeSelf)
{
return;
}
ImageFome.SetActive(false);
}


//各Viewボタンのテキストに反映する
public void ViewTextRef(string Tx)
{
switch (ViewBt)
{
case 1:
TextCARD.GetComponent<Text>().text = Tx;
CARDView.SetActive(false);
break;

case 2:
TextCh.GetComponent<Text>().text = Tx;
ChView.SetActive(false);
break;

case 3:
TextTime.GetComponent<Text>().text = Tx;
TimeView.SetActive(false);
break;
}
}
}

前回のスクリプトに加筆修正した部分は、
・名前空間にUnityEngine.UIを追加
・各ButtonTextを取得する変数を追加
・ScrollView呼び出しボタンのナンバーを共有する変数追加
・呼び出しボタンのテキストを書き替えるメソッド追加

ここからはエディターで、それぞれの変数やボタンのOnClickをアタッチして、
プレイしてみます。



無事テキストがボタンに反映されるようになりました。
続いて、カレンダー上のテキストに反映されるようにします。

カレンダーのテキストを書き替えるためには、
日ごとに反映させる必要があるので、どの日付か分かるようにします。
ついでに日枠を押すと入力フォームが開いて、テキストが反映されると
入力フォームが閉じるようにします。


上記スクリプトに

public GameObject[] CalendarText; //カレンダー上のテキストを取得
private int FrameNo; //カレンダーの枠番変数
private string CARDText,ChText,TimeText,DayText; //各テキスト



//各Viewボタンのテキストに反映する
public void ViewTextRef(string Tx)
{
switch (ViewBt)
{
case 1:
TextCARD.GetComponent<Text>().text = Tx;
CARDText = Tx;
CARDView.SetActive(false);
break;

case 2:
TextCh.GetComponent<Text>().text = Tx;
ChText = Tx;
ChView.SetActive(false);
break;

case 3:
TextTime.GetComponent<Text>().text = Tx;
TimeText = Tx;
TimeView.SetActive(false);
break;
}
}


//カレンダーの日毎のボタンを押した処理
public void PushDayButton(int frame)
{
if (ImageFome.activeSelf)
{
return;
}

//入力フォームを開く
ImageFome.SetActive(true);
FrameNo = frame;
}


//カレンダー上にテキストを反映させる
public void CalendarRef()
{
DayText = CARDText + "\n" + ChText + "\n" + TimeText;
CalendarText[FrameNo].GetComponent<Text>().text = DayText;

//反映後入力フォームを閉じる
ImageFome.SetActive(false);
}

少し説明すると、
日枠のボタンを押した時にint frameで枠番号を取得します。
クラス内の共有変数FrameNoに代入して、カレンダーのテキストに反映する
オブジェクト番号を指定しました。
CalendarRefメソッドのCalendarText[FrameNo]の部分です。
枠番0=CalendarText[0]の感じです。


最後に「反映する」ボタンを押すと入力フォームが閉じます。

少しエディターを編集します。
Calendar18.jpg
「反映する」ボタンを追加します。
OnClickにはCalendarRefメソッドを割り当てます。
入力フォームの準備は整ったので、Inspectorのチェックを外して消しておきます。

続けて、
Calendar19.jpg
CalendarText[]にSchedule0~36をアタッチします。

Calendar20.jpg
最後にDayButtonParentのButton0~36のOnClickを追加して、
PushDayButtonメソッドを割り当てます。
各ボタンの変数をButton0なら0のように、0~36の数字を入力します。

準備ができたので、プレイしてみます。

無事カレンダー上に反映できるようになりました。

枠に一つのテキストなので、
改行しないと並んでテキストが表示されてしまいます。

スクリプトからテキストの改行を行う際は、”\n”を挟む必要があります。
なぜかVisualStudioで"\n"を入力すると表示は”¥n”になるので注意が必要でっす。

/ではなくて\です。間違い易いです。(´ー`) ウフフ


最後に完成版のスクリプトを載せておきます。

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

public class InputFoamManager : MonoBehaviour {

public GameObject ImageFome; //入力フォーム取得
public GameObject CARDView; //対戦カードView取得
public GameObject ChView; //放送ChView取得
public GameObject TimeView; //放送時間View取得

public GameObject TextCARD; //CARDViewボタンのテキスト取得
public GameObject TextCh; //ChViewボタンのテキスト取得
public GameObject TextTime; //TimeViewボタンのテキスト取得

public GameObject[] CalendarText; //カレンダー上のテキストを取得

private int ViewBt; //ビューボタンの番号変数
private int FrameNo; //カレンダーの枠番変数


private string CARDText,ChText,TimeText,DayText; //各テキスト


// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

}

//各ScrollView呼び出し
public void ScrollViewCall(int Bt)
{
//いずれかのViewが開いてる時は他のViewは開かない
if (CARDView.activeSelf || ChView.activeSelf || TimeView.activeSelf)
{
return;
}

ViewBt = Bt; //クラス内でボタンナンバーを共有

switch (Bt)
{
case 1:
CARDView.SetActive(true);
break;

case 2:
ChView.SetActive(true);
break;

case 3:
TimeView.SetActive(true);
break;
}

}



//入力フォームを閉じる
public void Pushclose()
{
//いずれかのViewが開いている時はフォームを閉じない
if (CARDView.activeSelf || ChView.activeSelf || TimeView.activeSelf)
{
return;
}
ImageFome.SetActive(false);
}


//各Viewボタンのテキストに反映する
public void ViewTextRef(string Tx)
{
switch (ViewBt)
{
case 1:
TextCARD.GetComponent<Text>().text = Tx;
CARDText = Tx;
CARDView.SetActive(false);
break;

case 2:
TextCh.GetComponent<Text>().text = Tx;
ChText = Tx;
ChView.SetActive(false);
break;

case 3:
TextTime.GetComponent<Text>().text = Tx;
TimeText = Tx;
TimeView.SetActive(false);
break;
}
}


//カレンダーの日毎のボタンを押した処理
public void PushDayButton(int frame)
{
if (ImageFome.activeSelf)
{
return;
}

//入力フォームを開く
ImageFome.SetActive(true);
FrameNo = frame;
}


//カレンダー上にテキストを反映させる
public void CalendarRef()
{
DayText = CARDText + "\n" + ChText + "\n" + TimeText;
CalendarText[FrameNo].GetComponent<Text>().text = DayText;

//反映後入力フォームを閉じる
ImageFome.SetActive(false);
}
}


ダラダラと長くなってしまったのですが、縮小する方法も思いつかないので、
このままで行きます。(;^o^) \(ToT )あんたほんとにそれでいいの

対戦カードが広島メインなのは、嫁がカープファンなもんで…
オバハンなのにカープ女子とか…

私は阪神ファンなんですが、甲子園に行くのも広島戦がメインです。(T^T) ヒック

最近では、甲子園よりマツダスタジアムに行く回数の方が多いような…
広島行くの遠いのに…
嫁には逆らえません(T∇T) ウウウ

この記事へのコメント