前提・実現したいこと
ver2018.3 project:3D
現在ポイントごとで選択問題を解いていく流れをuGUIを使って作っています。
その中で、結果表示画面の時のみ、スクリプトからボタンフォーカスの処理をしているにもかかわらず
フォーカスされないという問題が発生しています。
この問題を解決し、結果画面が表示されたとき、「手順を終了する」のボタンが選択済み状態になるようにしたいです。
GIFを見ていただけるとわかると思いますが、そのほかの画面では問題なくボタンをフォーカスできています。
試したこと
フォーカスのタイミングや、フォーカスの方法をSelect();もしくはEventSystem.current.SetSelectedGameObject();
に変更してみたり、ボタン自体を正常にフォーカスできているものと入れ替えたりしましたが、
どれも上手くいきませんでした。
強引な方法として、別スクリプトでUpdateの中でSelect();を実行し、そのスクリプトをendsにアタッチするという
手段ではボタンをフォーカスする事が出来ましたが、何度も処理を繰り返す必要がないので
この方法は避けたいと思っています。
ボタンを押したときに使用するスクリプト(必要部分のみ抜粋)
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; public class ResultMgr : MonoBehaviour { GameObject batu; public GameObject ttSelect = null; public GameObject ffSelect = null; Button ed = null; public static Text quiz; GameObject Button1; GameObject Button2; GameObject Button3; public static GameObject ResultMenu; GameObject Again; GameObject Ok; public static Text ans1; public static Text ans2; public static Text ans3; Text quizAns; Text ST; public static int count = 0; public static int Score = 0; string answerText; public void Awake() { batu = ButtonChangeAction.ot; Button1 = GameObject.FindWithTag ("Button1"); Button2 = GameObject.FindWithTag ("Button2"); Button3 = GameObject.FindWithTag ("Button3"); ed = GameObject.FindWithTag ("ed").GetComponent<Button>(); ResultMenu = GameObject.FindWithTag ("Result"); Again = ResultMenu.transform.Find("again").gameObject; Ok = ResultMenu.transform.Find("ok").gameObject; quiz = GameObject.FindWithTag ("FlightText").GetComponentInChildren<Text>(); ans1 = GameObject.FindWithTag ("Button1Text").GetComponentInChildren<Text>(); ans2 = GameObject.FindWithTag ("Button2Text").GetComponentInChildren<Text>(); ans3 = GameObject.FindWithTag ("Button3Text").GetComponentInChildren<Text>(); ST = GameObject.FindWithTag ("ScoreText").GetComponentInChildren<Text>(); } public void CheckAnswer() { count ++; if(quizAns.text == answerText) { Score ++; ButtonChangeAction.tt.SetActive(true); EventSystem.current.SetSelectedGameObject (ttSelect);"この処理ではフォーカスできている" }else { ButtonChangeAction.ff.SetActive(true); EventSystem.current.SetSelectedGameObject (ffSelect); } } public void OverRideAnswer() { Debug.Log("count" + count); Debug.Log("Score" + Score); ButtonChangeAction.tt.SetActive(false); ButtonChangeAction.ff.SetActive(false); ButtonChangeAction.flight.SetActive(true); batu.SetActive(true); switch(count) { case 1~ case4は省略 case 5 : ed.Select(); "←本来ならこの処理でフォーカスされているはず" ResultMenu.SetActive (true); ButtonChangeAction.ot.SetActive (false); ButtonChangeAction.flight.SetActive (false); Debug.Log(ed); //EventSystem.current.SetSelectedGameObject (ed); "上記の処理も試したがフォーカスできず" ST.text = "あなたの成績は \n" + Score +"/"+ count +"\n 正解でした"; if(Score == count) { Debug.Log("OK"); Ok.SetActive(true); Again.SetActive(false); }else { Ok.SetActive(false); Again.SetActive(true); } break; default: break; } } }
###別のスクリプトでUpdateを使い無理やりフォーカスさせる
下のHierarchyの画像でendsにアタッチされているEnd Selectの中身です。
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; public class EndSelect : MonoBehaviour { Button End = null; // Start is called before the first frame update void Update() { End = GameObject.FindWithTag ("ed").GetComponent<Button>(); End.Select(); Debug.Log("lol");"処理確認用のデバッグログ" } }
HierarchyとGIFによる実際の挙動
endsがフォーカスさせたいボタンです。
GIF画像ではEnd Selectのスクリプトは非アクティブにしています。
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。