Unity compare if two input fields have same value - c#

How can I achieve this? A function can take only 1 argument in Unity for an event.
There are two different input fields in the same canvas which I'd like to compare.
I'm assuming this can be done using object as argument but I don't know which object I can pass. I tried dragging the canvas and input field gameObject(I'm guessing that's what it's called) to the object parameter field of my selected function but they don't get accepted.
This is where I want to attach the script:-

InputFields are not GameObject. They are objects of UI.InputField class. So in your script import UI with
#using UnityEngine.UI;
Then create 2 InputField object.
public InputField inp1;
public InputField inp2;
then drag your InputFields to them. then you can compare their text like that:
if( inp1.text == inp2.text) {}
Edit: You shouldnt attach a script to there, because the variables may change dynamically. attach this script I have wroten to an empty game object, add new snippet on below to script, and then attach this empty game object to event field that you showed. Then you should select the function will be called when edit ended. For this, create two different function in script for each input field as:
//Call this on EndEdit for inputField1
public void InpField1EndEdit()
{
//compare input fields here or make what you want
}
//Call this on EndEdit for inputField2
public void InpField2EndEdit()
{
//compare input fields here or make what you want
}

Related

Transferring text of an input field to another input field on Unity

Im working on this Unity AR project and im trying to add a function where I can transfer text of an input field to another input field.
I have tried various scripts and have tried it on the app but the text of input field still cannot be transferred to another input field.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class RemarksTransfer : MonoBehaviour
{
public string theRemarks;
public GameObject inputField;
public GameObject textDisplay;
public GameObject secondInput;
public void StoreRemarks()
{
theRemarks = inputField.GetComponent<Text>().text;
textDisplay.GetComponent<Text>().text = theRemarks;
if (textDisplay.TryGetComponent<InputField>(out var secondInput))
{
secondInput.text = theRemarks;
}
}
}
This is the code that I have used. However, this is for transferring text from an input field to a text box instead of another input field. Therefore, displaying the text only but not being able to edit on it.
Long story short, Im just trying to transfer text from an input field to another input field and the text can be edited if the user wants to edit.
What you’re seeing here is a GameObject that has an InputField. For an InputField to work correctly, it has an associated Text component. Your code is looking for the Text component. Now, if your second object DOES have an InputField component, search for that, and use its text property to get and set the text. You should also bring doing the same thing for your original InputField as well. As the Unity documentation says, the visible text shown might be truncated, or have asterisks if it’s a password. That’s what you’d get, instead of the actual underlying text value.
So, assuming your textDisplay has an InputField component, you could use:
if ( textDisplay.TryGetComponent<InputField>( out var secondInput ) )
{
secondInput.text = theRemarks;
}

How to assign programmatically an Interactable Event in MRTK

I am creating a map in Unity3D to be displayed in Hololens 2. I am using MRTK to code interactable functionality.
Since the map contains a lot of independent countries, whose respond onClick may vary with time, I decided to add the required components programmatically. For example:
public Transform worlmap;
public Microsoft.MixedReality.Toolkit.UI.Theme mTheme;
public Microsoft.MixedReality.Toolkit.UI.States mStates;
List<Microsoft.MixedReality.Toolkit.UI.Theme> themes;
void Start()
{
themes = new List<Microsoft.MixedReality.Toolkit.UI.Theme>();
if (mTheme)
{
themes.Add(mTheme);
}
// Looping through all the countries in worldMap
foreach (Transform country in worldMap)
{
// Adding the 4 components which make a GameObject interactable in Hololens
country.gameObject.AddComponent<Microsoft.MixedReality.Toolkit.UI.Interactable>();
country.gameObject.AddComponent<Microsoft.MixedReality.Toolkit.UI.PressableButton>();
country.gameObject.AddComponent<Microsoft.MixedReality.Toolkit.Input.NearInteractionTouchable>();
country.gameObject.AddComponent<MeshCollider>();
// Assigning State
country.gameObject.GetComponent<Microsoft.MixedReality.Toolkit.UI.Interactable>().States = mStates;
// Assigning the Profiles (Will definine the colors based on the State)
Microsoft.MixedReality.Toolkit.UI.InteractableProfileItem mProfile = new Microsoft.MixedReality.Toolkit.UI.InteractableProfileItem();
mProfile.Themes = themes;
mProfile.Target = country.gameObject;
country.gameObject.GetComponent<Microsoft.MixedReality.Toolkit.UI.Interactable>().Profiles.Add(mProfile);
// Assigning Events
// TODO
}
}
The part below is working, but I am not able to assign Events to the different countries in the same loop.
I can define these events in the Editor, for example:
But since it is a repetitive activity which may slightly change over time, I was trying to achieve the same programmatically inside the loop of the script above.
This is what I tried, but it is not working:
// Creating an Event
Microsoft.MixedReality.Toolkit.UI.InteractableEvent mEvent = new Microsoft.MixedReality.Toolkit.UI.InteractableEvent();
// Assigning the method to be executed during onClick
mEvent.Event.AddListener(country.gameObject.GetComponent<CountrySelected>().printName)
// Assigning the Event to Interactable
country.gameObject.GetComponent<Microsoft.MixedReality.Toolkit.UI.Interactable>().InteractableEvents.Add(mEvent);
What is the correct way of adding programmatically Events to an Interactable in MRTK?
To assign OnClick Event for the Interactable component at the runtime, please try the following code:
this.GetComponent<Interactable>().OnClick.AddListener(MyFun1);
The Interactable component profile in the Unity Editor may not display the new method at the runtime, but it does work in the scene.

Enum Value Set in Editor Not Changing in Script

I am working on a project that requires around 30 buttons in a UI menu. Each of these buttons has a "Loader" script attached to it, and the overall scene has a GameManager object (which persists through scenes).
The GameManager holds an enum with a number of named states equal to the number of buttons.
The idea is I need to populate the next scene with specific content depending on the button pressed, so I thought to assign each of these buttons an enum value, and when clicked the Loader script will change the GameManager's enum to equal the button's value. This way I can avoid making 30+ scenes and instead change the single scene based on the state. When the next scene is loaded, it should determine what the current state is, and does stuff based on that.
The problem is, even though I am able to manually assign the enum value of each individual button in the editor, that doesn't seem to actually do anything. Here are my scripts (abbreviated for only the relevant methods), and sample output when clicking a button.
Here is my GameManager:
public class GameManager : MonoBehaviour {
//Used so MenuContent scene knows what random content to populate with
public enum MenuChosen
{
Tabs, Formal, Casual, Headwear, Accessories, Speakers, ComputerParts,
GameConsoles, MobilePhones, Furniture, Lighting, OfficeSupplies, Gadgets, Toys,
Summer, SchoolSupplies, ArtsCrafts, Checking, Savings, PayPal, OtherAccount, Visa,
MasterCard, AirMiles, OtherCards, Food, Retail, Digital, OtherCoupons,
PayTransaction, ScheduledPayments, LoanPayment, MobileRecharge
};
public MenuChosen chosen;
}
And here is my Loader class. It is attached to every button. In the editor, I selected four buttons, and manually changed their states to "Formal", "Casual", "Headwear", and "Accessories", via the drop-down list in the inspector for each of them.
The Loader class creates a reference to the GameManager object, and attempts to change its "chosen" enum variable to be equal to the value that was set in the inspector window (which is called "MenuOption" in the loader).
public class Loader : MonoBehaviour {
public GameManager gm;
public GameManager.MenuChosen MenuOption;
private void Start()
{
gm = GameObject.FindGameObjectWithTag("GameManager").GetComponent<GameManager>();
Debug.Log("Current Loader State: " + MenuOption);
}
public void LoadMenuContent()
{
Debug.Log("Button's State is: " + MenuOption);
gm.chosen = MenuOption;
SceneManager.LoadScene("MenuContent");
Debug.Log("Current state is: " + gm.chosen);
}
}
When I first run the game, the debug messages show that the buttons' enum values are indeed changed:
Current Loader State: Accessories
Current Loader State: Headwear
Current Loader State: Formal
Current Loader State: Casual
However, the logs printed in the LoadMenuContent() method show something different:
Button's State is: Tabs
Current state is: Tabs
So even though it shows that the buttons' MenuOption is properly changed, once it reaches LoadMenuContent(), which is called when the button is pressed, then somehow the state has changed back to the default state, "Tabs".
Any thoughts on why the enum might be changing?
I discovered what my issue was.
Due to the large quantity of buttons, I had resorted to copying and pasting them.
This meant that each button was identical, and had its own copy of the Loader script as a component. By doing this, I was able to change the individual values of the enum of each individual button in the inspector.
However, this also meant that each and every button, in the OnClick() method, were all still holding a reference to the original button that I had copied and pasted. So these buttons were independent, but they all referenced the same exact script from the same exact button, who's state had never been changed from the default.
In order to fix this, I had to go in and manually drag each button into its own OnClick() method as a reference to itself, so they were properly calling the correct function from their own individual Loader script.

unity3d and lot of buttons - looking for a smooth way to handle it

Im doing gui in Unity (ugui) right now and i want to make lot of buttons and transitions. Is there a smoother way to do it than using transition method with screen ID/few onclick methods with int parameter? I tried to do onClick method base on enums but its not possible to add enum method to ugui button (tried integer but it can be confusing with lot of buttons).
Nika Kasradze: putting method based on string in inspector in not a good idea. You need write same string in onClick method later, and writing two same strings in not a good idea.
I used custom button function. You just add it to button and it calls a function in UI manager called "onButtonClick" and pass button name as parameter. Then you have onButtonClick method where you set up all buttons using button names. You can use multiple buttons with same name to call one function or use unique name to call unique function. onButtonClick method is really easy to read and you dont have to set up every button in inspector!!!
using UnityEngine;
using UnityEngine.UI;
{
[RequireComponent (typeof(Button))]
public class CustomButton : MonoBehaviour
{
void Awake ()
{
Button btn = GetComponent<Button> ();
btn.onClick.AddListener (() => ManagerUI.Instance.onButtonClick (gameObject.name));
}
}
}
and you put something like this in ManagerUI
void onButtonClick(string buttonName){
switch(buttonName){
case "back_btn": GoBack();
case "openshop_btn": OpenShop();
case "closepopup_btn": ClosePopup();
}
}

iGUI for Unity 3D

Well,I am working on my graduation project with unity 3d pro v4.5.0 ..I was using the normal method OnGUI() in my scripts but now i want to make a better interface for my project and i didn't anderstand how can i replace for example these lines in my "Database" Script:
if (GUILayout.Button ("Add to database"))
{
// Insert the data
InsertRow (FirstName,LastName);
// And update the readout of the database
databaseData = ReadFullTable ();
}
With just calling a Button1_click for example from my iGUI Class specially that every thing related to interface and buttons exists in OnGUI() method so how can i change all of that in my "Database" Script..i did anderstand how to make a button in iGUI that LOAD a scene or set an other function in the same iGUI Class but i just want to make buttons and iGUI field texts in iGUI MonoBehaviour Class and call them later whenever i need in my pricipal script "Database" if it is possible.Please am so in need for help. Thank you in advance.
You need do this:
create a public method in any script.
add the script to a GameObject.
Click the button and add the object with the script in the method
OnClick located in the inspector.
Choose your script and the public method where said No function.
Boala!!, when you click, your method run.

Categories

Resources