###assetStoreのUIControllerをキーによって実行した
C#を学び始めて1か月程度のUnity超初心者です。
他の言語も知らずUnityをするためC#を勉強し始めました。
AssetStoreの「UIController」を使用してUIの表示非表示を行っており、UIのボタンによる表示非表示は成功しております。
(チュートリアル通りに行っているだけなのですが・・・)
そこでキー入力によって表示非表示ができないか模索しているところなのですが、なかなかうまくいきません。
UIController(スクリプト)内の関数を実行することができれば、成功するような気がするのですが、別のオブジェクトにアタッチしている関数の使い方がうまく理解できていないので、成功しません。
どうすればいいでしょうか。
UIControllerのShow関数を実行することでUIを出しているのではないかと思ってスクリプトを書いています。UIControllerのスクリプト内容も簡単に教えてもらえると助かります。
###発生している問題・エラーメッセージ
エラーメッセージは特にありませんが、実行しても何も起こりません。
エラーメッセージ特になし
###該当のソースコード
UIControllerをキー入力によって表示しようとしたスクリプト
using System.Collections; using System.Collections.Generic; using UnityEngine; public class BottonUIControl : MonoBehaviour { GameObject UICanvas; // Use this for initialization void Start () { UICanvas = GameObject.Find("ProjectBrowser"); } // Update is called once per frame void Update () { if (Input.GetKeyDown(KeyCode.M)) { UIController d1 = UICanvas.GetComponent<UIController>(); d1.Show(); } } }
UIControllerのスクリプト
using UnityEngine; using UnityEngine.Events; using System.Collections; [RequireComponent(typeof(Animator))] public class UIController : MonoBehaviour { public enum OnHideAction { None, Disable, Destory, } public OnHideAction onHideAction = OnHideAction.Disable; [SerializeField] private UnityEvent m_OnShow = new UnityEvent(); [SerializeField] private UnityEvent m_OnHide = new UnityEvent(); private UnityEvent m_DisposableOnShow = new UnityEvent(); private UnityEvent m_DisposableOnHide = new UnityEvent(); private Animator m_Animator; private Vector3 m_TempSaveScale; public UnityEvent onShow { get { return this.m_OnShow; } private set { this.m_OnShow = value; } } public UnityEvent onHide { get { return this.m_OnHide; } private set { this.m_OnHide = value; } } public bool isShow { get { if (this.animator.runtimeAnimatorController == null) { return this.gameObject.activeSelf; } return this.animator.isInitialized && this.animator.GetBool("isShow"); } private set { if (this.animator.runtimeAnimatorController == null) { this.gameObject.SetActive(value); if (value) { this.OnShow(); } else { this.OnHide(); } return; } if (this.animator.isInitialized) { this.animator.SetBool("isShow", value); } } } public bool isPlaying { get { var currentState = this.animator.GetCurrentAnimatorStateInfo(0); return this.animator.isInitialized && (currentState.IsName("Show") || currentState.IsName("Hide")) && currentState.normalizedTime < 1; } } public Animator animator { get { if (this.m_Animator == null) { this.m_Animator = this.GetComponent<Animator>(); } return this.m_Animator; } } // Show/Hide must fast by Show(UnityAction)Hide(UnityAction), make SendMessage("Show/Hide") working in Inspector public virtual void Show() { bool active = this.gameObject.activeSelf; this.gameObject.SetActive(true); this.isShow = true; if (!active && this.gameObject.activeInHierarchy) { this.TrySaveScale(); StartCoroutine(this.TryRevertScale()); } } public virtual void Hide() { this.isShow = false; } public void Show(UnityAction onShow) { this.ShowHideWithAction(true, onShow, this.m_DisposableOnShow, this.Show); } public void Hide(UnityAction onHide) { this.ShowHideWithAction(false, onHide, this.m_DisposableOnHide, this.Hide); } protected virtual void OnShow() { this.onShow.Invoke(); this.m_DisposableOnShow.Invoke(); this.m_DisposableOnShow.RemoveAllListeners(); } protected virtual void OnHide() { if (!this.animator.GetBool("isShow")) { switch (this.onHideAction) { case UIController.OnHideAction.None: break; case UIController.OnHideAction.Disable: this.gameObject.SetActive(false); break; case UIController.OnHideAction.Destory: Destroy(this.gameObject); break; } } this.onHide.Invoke(); this.m_DisposableOnHide.Invoke(); this.m_DisposableOnHide.RemoveAllListeners(); } private void TrySaveScale() { if (this.transform.localScale != Vector3.zero) { this.m_TempSaveScale = this.transform.localScale; this.transform.localScale = Vector3.zero; } } private IEnumerator TryRevertScale() { yield return new WaitForEndOfFrame(); if (this.m_TempSaveScale != Vector3.zero) { this.transform.localScale = this.m_TempSaveScale; this.m_TempSaveScale = Vector3.zero; } } private void ShowHideWithAction(bool isShowAction, UnityAction onAction, UnityEvent onActionEvent, System.Action action) { if (isShowAction == this.isShow && !this.isPlaying) { if (onAction != null) { onAction(); } } else { if (onAction != null) { onActionEvent.AddListener(onAction); } action(); } } }
###試したこと
C#自体の理解が進んでいないためどの関数を使うことでUIが表示されるのか理解できていませんが、ShowまたはOnShow関数を使えばできるのではと思ったのですが、ダメでした。
###補足情報(言語/FW/ツール等のバージョンなど)
使用言語:C#
実行ソフト:Unity5.6.2
開発ソフト:Visual Studio 2017
回答1件
あなたの回答
tips
プレビュー