I am a beginner in Unity and I am currently making a simple game. I have a problem where I need to select an object automatically by script so I can click outside of it without clicking on it in the screen.
What I want to do is when I click the object another object or panel will show and it will be automatically be selected by script so I can click outside the panel and it will close or will setActive to false. I tried the Select() and it is not working for me. The two objects are originally an Image but I added a Button component so it can be selectable.
This is the script for the object that will show the Panel:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class ShowPhone : MonoBehaviour, ISelectHandler
{
[SerializeField] Button phonePanel;
public void OnSelect(BaseEventData eventData)
{
phonePanel.gameObject.SetActive(true);
}
}
This is the script for the panel where I want to click outside to close it:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class HidePhoen : MonoBehaviour, IDeselectHandler
{
public Button phone;
private void OnEnable()
{
phone.Select();
}
public void OnDeselect(BaseEventData data)
{
Debug.Log("The Object will be setActive false when clicked outside the object");
}
}
You would use EventSystem.SetSelectedGameObject
EventSystem.current.SetSelectedGameObject(yourTargetGameObject);
Note though: Your panel would automatically be closed even if clicking any UI element within it (like e.g. InputField etc)
You can checkout the TMP_Dropdown source code and the way they implemented a "UI Blocker" which basically is an overlay over the entire screen except the drop-down popup and closes the pop-up when clicked on.
you could either replicate this or have a simpler version with a fully transparent background button behind your panel with onClick for closing the panel.
As alternative you could check RectTransformUtility.RectangleContainsScreenPoint
I create a set of prefabs at runtime via script. They are held in an array called newObj. Each one has some text UIs and some buttons, which I retrieve with GetComponentsInChildren. When a user clicks the first button in the prefab, I want to run a function that changes the text of the button and highlights that button.
Everything is working except the button doesn't highlight.
public void SelectPlayer(int rowSelected)
{
var buttons = newObj[rowSelected].GetComponentsInChildren<Button>();
var texts = newObj[rowSelected].GetComponentsInChildren<Text>();
texts[0].text = "1";
buttons[0].Select();
buttons[0].OnDeselect(null);
}
Oops. I'm still new to Unity and forgot that Unity sets the default highlight color to white (for some reason). Once I changed it for my prefab using the editor, all was well.
I need to change the items placed in UI on basis of items clicked on.
User have to click on Name of the game then it will unable the existing item and enable the chapters item to show chapters.
Note: I don't need to change scenes, I know how to change scene with buttons.
I have attached the screenshot of the main menu.
Just create a reference of the gameObjects you want to Activate/Deactivate.
Create a button and use GameObject.SetActive to activate/deactivated the objects you want when the user press it.
You can make the button invisible so the user thinks he's clicking the title but actually he clicks a button.
I Hope this helps. :D
I suggest creating a button for every menu interaction in general.
To handle your buttons OnClickEvents you need to create an empty gameObject on your scene and attach to it a script that your buttoms will use to do whatever you want when you click them.
For example:
//You can name your method however you like.
public void ButtomClicked(){
//Hide the UI on the screen expect the Back button.
//Show chapters to the player
}
Create a button and from the inspector select the Empty Gameobject this script is attached to and then select the ButtomClicked method. When you press the buttom the code in the method will run.
To avoid activating/deactivating all this buttons one by one, you can attach them to a panel(UI element) and activate/deactivate the panel istead. So, lets say you have 3 panels.
The Main menu panel, the Chapters panel and the Options panel.
When the player wants to see the chapters, you diactivate the main menu panel and activate the chapters panel. To make this feel really polished you can add transition animations later on.
This is how i handle my UI without never changing scenes. It gives a really smooth and polished feel to the user.
If you have more quastions about UI, plz watch this turtorial, it helped me a lot to understand the basics.
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.
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.