前提
C#scriptでescapeキーを押したらオブジェクトを表示、非表示するのにともなって、カーソルも表示、非表示するシステムを作っています。
実現したいこと
- オブジェクトが表示から非表示に切り替わるとき、エスケープキーを押したら瞬時にカーソルを非表示にしたいです。
発生している問題
ゲーム画面をクリックしないとカーソルを非表示にできません。
該当のソースコード
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.UI; 5 6public class GameManager : MonoBehaviour 7{ 8 public GameObject settingpanel; 9 bool currentactivety; 10 // Start is called before the first frame update 11 void Start() 12 { 13 settingpanel.SetActive(false); 14 } 15 16 // Update is called once per framew 17 void Update() 18 { 19 currentactivety = settingpanel.activeSelf; 20 keytoggle(settingpanel, "escape", currentactivety); 21 22 Cursor.visible = currentactivety; 23 Debug.Log(currentactivety); 24 } 25 public void keytoggle(GameObject onofobject,string keycodest,bool currentActive) { 26 27 if (Input.GetKeyDown(keycodest)) { 28 onofobject.SetActive(!currentActive); 29 } 30 } 31 32}
試したこと
デバッグして本当にcurrentactivety変数が機能しているか試しましたが、ちゃんと機能していました。
補足情報
何も押さなくても最初だけカーソルが非表示になっています。
unity 2019.4.36f1
Unity の話なら Unity のタグをつけてください。