How to add an extra button above the TouchScreenKeyboard in UIInput NGUI? - unity3d

My game uses a registration form scene in order to register a user. I've got several Textboxes(UIInput) on screen.
I would like to have Next/Previous Button over the keyboard which appears when i select a text box for input. this way i will be able to navigate on multiple textboxes in the registration form.
right now i am using HideInput=true so there is nothing over the keyboard

I am sure you have Box Collider "using UIInput",
Simple way would be to add UIButton To your InputBox.
Once you do that attach scene GameObject or PrefabObject, with a public method() to the UIButton OnClick Notify option, Select Method from the drop down.
Test Code:
Private Bool ShwNxtBtn = false;
Public Void MethodName(){
if (ShwNxtBtn != true){
ShwNxtBtn = true;
}else{
ShwNxtBtn = false;
}
Note: you could use Toggle, but I've had some issue with that and input Collider.

Related

Multiple Selected buttons Unity

Is there support for multiple selected buttons in unity ui.
I saw this question before, but no good answers. Thanks
Like a button group that only deselects buttons in the same group.
Have you check Toggle UI option? You can select and control multiple buttons and there's an option to create groups as well.
You can obviously control the UI to make it look similar to regular button.
Toggle: https://docs.unity3d.com/Packages/com.unity.ugui#1.0/manual/script-Toggle.html
Toggle group: https://docs.unity3d.com/Packages/com.unity.ugui#1.0/manual/script-ToggleGroup.html
Toggle or that script:
public class ButtonToggle : MonoBehaviour
{
[SerializedField] private bool m_setOnStart;
static event Action<ButtonToggle> OnPress;
void Awake()
{
OnPress += ActionOnPress;
SetActive(m_setOnStart);
}
// Add this one to the button event listener
public void OnButtonPress()
{
OnPress?.Invoke(this);
}
void ActionOnPress(ButtonToggle button)
{
SetActive(button == this);
}
void SetActive(bool value)
{
// Do what you need for true/false
// For instance, set active/inactive color
}
}
This component goes on each button object, then the public method into the button listener.
When a button is pressed, it informs other buttons with the static event. The caller passes itself so all others can set themselves off while the caller sets itself on.

Create a Megaman like character selection screen with Unity2D

I want to create a character selection screen like the old Megaman games beside that i want to create the menus for the game my question is how do i navigate through game objects using the keyboard or the Dpad and selecting them?
I dont want to use UI elements but box colliders and such can anyone please provide an example how to achieve this?
I followed other Unity tutorials however it uses UI buttons.
Thanks for the help.
1.Name all your NonUIButtons in ascending order [eg: Button1,Button2,etc.,]
2.Enable the first button on Awake()
void Awake(){
currentValue=0;
GameObject goToEnable = GameObject.Find("NonUIButton1");
goToEnable.SetActive(true);
}
3.Get the list of allButtons
public List<GameObject> items = new List<GameObject>();
void Start(){
items.AddRange(GameObject.FindGameObjectWithTag("NonUIButtons");}
4.When ever key is pressed calcualte the movement using simple additions and subtraction, disable all Buttons and Enable the only one you want
void Update(){
if (Input.GetKeyDown("Up"))
{
currentValue -= 3; // Assuming each row has 3 buttons
foreach(GameObject obj in items){
obj.SetActive(false);
}
}// DO similar steps for each type of movement also make sure to add edge cases
}
Enabling the Button you want
GameObject goToEnable = GameObject.Find("NonUIButton1");
goToEnable.SetActive(true);

Unity multiplayer UI button script

I have a multiplayer 2D game made in Unity and I wanted to put some onscreen buttons to be able to control it on the telephone. The problem is that when I put the Event Trigger on the button it uses the function from the script that has to use, but it can't open other functions called in the main one. In my code, I try to open the function TryFireBullet() whenever I am pressing the button and Debug.Log shows me that TryFireBullet() is opened, but the functions inside it are not opened at all. (when I press the button, Debug.Log() shows me "works", "works2" and no "works3"..)
Any ideas why it does not work and how to fix it?
Here is the problem part of the code:
public void TryFireBullet()
{
Debug.Log("works");
//if (ultimul_glont == 0 || Time.time > ultimul_glont + Delay_Bullet)
//{
ultimul_glont = Time.time;
Debug.Log("works2");
CmdFireBullet();
//}
}
[Command]
public void CmdFireBullet()
{
Debug.Log("works3");
GameObject bullet = Instantiate(bulletPrefab, gunTip.position, Quaternion.identity) as GameObject;
NetworkServer.Spawn(bullet);
RpcAdaugaViteza(bullet);
}
Here is the full code:
https://pastebin.com/WaWbQvTw
p.s. I can not use just a main function for my button because I need Server Commands

Unity toggle state change causes StackOverflow [duplicate]

I am a absolute beginner in unity. I have been working on an UI which is a simple login form. In which I have two Toggle for selecting gender i.e male or female. What I have been doing is that calling a method which checks if a male is already selected it will remove the check from male when the other toggle is pressed.
My Unity freezes when I click the female Toogle. here is my code.
Toggle female;
Toggle male;
void Start(){
female = GameObject.Find ("toggle_female").GetComponent<Toggle> ();
male = GameObject.Find ("toggle_male").GetComponent<Toggle> ();
}
public void isMale(){
if (female.isOn)
female.isOn = false;
male.isOn = true;
}
public void isFemale(){
if (male.isOn)
male.isOn = false;
female.isOn = true;
}
You are running into infinite loop with your Toggle.
When the female toggle changes, the onValueChanged event is triggered. I assume that your isFemale() function is linked with the Toggle onValueChanged event, so isFemale() will be called.
When isFemale() is called, it will do male.isOn = false; causing
isMale() function to be called which will then run female.isOn = false; causing isFemale() to be called again. This is everlasting.
The onValueChanged event should never be fired when isOn is set from script. This is the problem.
Solution:
You are looking for the ToggleGroup.
1.Create male toggle GameObject -> UI -> Toggle then name it Male toggle.
2.Create female toggle GameObject -> UI -> Toggle then name it Female toggle. You can change the label text later on.
3.Create an empty GameObject. GameObject -> Create Empty.
Rename it to ToggleGroup.
Select the newly created GameObject, go to Component -> ToggleGroup. Now, you have GameObject called ToggleGroup with ToggleGroup Component attached to it.
4.Select your Male toggle, then drag the ToggleGroup GameObject into Male toggle's Group slot. Select your Female toggle, then drag the ToggleGroup GameObject into Female toggle's Group slot
Select your Female toggle, then unckeck Is On checkbox to make sure that only one(Male toggle) is selected by default. Click play and now, only one toggle can be selected at a time.
I guess the two functions isMale and isFemale are Toggles' event handlers. According to Unity docs:
The Toggle has a single event called On Value Changed that responds
when the user changes the current value
So the problem is that these event handlers respond to value change and not toggle being checked, so your code causes an infinite loop. It should work by reading them first to make sure it's a check and not uncheck:
public void isMale(){
if (!male.isOn) return;
if (female.isOn)
female.isOn = false;
male.isOn = true;
}
public void isFemale(){
if (!female.isOn) return;
if (male.isOn)
male.isOn = false;
female.isOn = true;
}

How to prevent mouse clicks from passing through GUI controls and playing animation in Unity3D [duplicate]

This question already has answers here:
Detect mouse clicked on GUI
(3 answers)
Closed 4 years ago.
Description
Hi Guys need your help I faced problems with mouse clicks which pass through UI panel in Unity, that is, I have created pause menu and when I click Resume button, the game gets unpaused and the player plays Attack animation which is undesirable.What I want is when I click Resume button, Attack animation should not be played. The same problem if I just click on panel not necessarily a button and the more I click on UI panel the more Attack animation is played after I exit pause menu. Moreover, I have searched for solutions to this issue and was suggeted to use event system and event triggers but since my knowledge of Unity is at beginner level I could not properly implement it. Please guys help and sorry for my English if it is not clear)) Here is the code that I use:
The code:
using UnityEngine;
using UnityEngine.EventSystems;
public class PauseMenu : MonoBehaviour {
public static bool IsPaused = false;
public GameObject pauseMenuUI;
public GameObject Player;
private bool state;
private void Update() {
//When Escape button is clicked, the game has to freeze and pause menu has to pop up
if (Input.GetKeyDown(KeyCode.Escape)) {
if (IsPaused) {
Resume();
}
else {
Pause();
}
}
}
//Code for Resume button
public void Resume() {
//I was suggested to use event system but no result Attack animation still plays once I exit pause menu
if (EventSystem.current.IsPointerOverGameObject()) {
Player.GetComponent<Animator>().ResetTrigger("Attack");
}
pauseMenuUI.SetActive(false);
Time.timeScale = 1f;
IsPaused = false;
}
//this method is responsible for freezing the game and showing UI panel
private void Pause() {
pauseMenuUI.SetActive(true);
Time.timeScale = 0f;
IsPaused = true;
}
//The code for Quit button
public void QuitGame() {
Application.Quit();
}
}
Im not sure if i understood your problem, but it sounds like somewhere in your code you start an attack when the player does a left click.
Now your problem is that this code is also executed when the player clicks on a UI element, for example in this case the Resume button?
You tried to fix this problem, by resetting the attack trigger of the animator, i think it would be a better solution to prevent the attack from starting instead of trying to reset it later.
EventSystem.current.IsPointerOverGameObject() returns true if the mouse is over an UI element.
So you can use it to modify your code where you start your attack:
... add this check in your code where you want to start the attack
if(EventSystem.current.IsPointerOverGameObject() == false)
{
// add your code to start your attack
}
...
Now your attack will only start if you are not over a UI element