- Home /
Duplicate Question
KeyCode.H to Ui button ? how
i have script use KeyBoard "H"
if (Input.GetKeyDown(KeyCode.H) && lightsMode == LightsMode.Inactive)
{
lightsMode = LightsMode.Active;
}
else if (Input.GetKeyDown(KeyCode.H) && lightsMode == LightsMode.Active)
{
lightsMode = LightsMode.Inactive;
}
how to convert to ui button the easiest way ?
Answer by KuR5 · Jun 13, 2016 at 06:40 AM
Just create a public function and call it by using OnClick event of UI button.
public void OnButtonClick()
{
if (lightsMode == LightsMode.Inactive)
{
lightsMode = LightsMode.Active;
}
else if (lightsMode == LightsMode.Active)
{
lightsMode = LightsMode.Inactive;
}
}
Assign above function in the inspector of UI Button.
Answer by Chamandana · Jun 13, 2016 at 06:51 AM
As I understood the question, you can follow these steps,
Create a Canvas and a Button inside it.
Modify it as needed
Add this script as "CLICKHOLDER.cs" to the MainCamera.
using UnityEngine; using System.Collections; public class CameraCode : MonoBehaviour { public void CLICK() { if (lightsMode == LightsMode.Inactive) { lightsMode = LightsMode.Active; } else if ( lightsMode == LightsMode.Active) { lightsMode = LightsMode.Inactive; } } }
which makes the Input.GetKeyDown(KeyCode.H) == event
Go to the OnClick event section in the inspector window by selecting your button on the scene.
Click Add button, select MainCamera. and then the CLICKHOLDER in the combobox ahead,and select CLICK() method from the contextual menu that you found.
Hope this code helped you, or if not thanks for reading.
Follow this Question
Related Questions
How Converting GUILayout.TextField with UI InputField ? 2 Answers
UnityEngine.UI.dll 0 Answers
Strobe light when a key is pressed 1 Answer
How do I save and load the value of a text? 1 Answer
UNITY NETWORK SCOREBOARD 0 Answers