New to coding here, setting up a UI in unity and having a visual issue where the back button appears until I hit options and back on the start up page, any reccomendations for a fix?
Image showing back button behind the quit button
Settings of the image on the right
You can use Gameobject.setactive(true/false) and try to manage the panel by making empty game object inside UI Canvas and name it as a panel so the whole button became a child and you can easily manage it
for example like this one
public Gameobject startPanel;
public Gameobject optionPanel;
public void Startpnl()
{
//deadActive optionPanel and Activate startPanel
startPanel.setActive(true);
optionPanel.setActive(false);
}
public void optionpnl()
{
//deadActive starPanel and Activate OptionPanel
startPanel.setActive(false);
optionPanel.setActive(true);
}
Related
I'm developing a game and I've reached a stage in which I need to dynamically create a list of buttons.
My approach was to create a scroll view and then add the buttons through scripting.
I've already created the button prefab. This button purpose is to open a specific link outside the application, so I created the button prefab with two text components: One saying "Show Path" which is what I want the buttons to show, and the other text component is the link I need to open, which isn't visible to the user.
I'm running into several problems:
I need to create the buttons as I am going through a list of user data. I display the data right beside the button, in another scroll view, correctly. However, only one button is created.
I'm unable to access the prefab link text component to change it to the link I need to open.
Thank you for your time!
What I'd recommend you do is create your own class to handle this, so it's easier to visualise in the inspector.
[System.Serializable]
public class RelevantClassName
{
public string link = "http://defaultbackuplink";
public string text = "Default content";
[Space]
public Transform button;
public Transform contentHolder;
}
And in a seperate class, you use this information to generate your scroll views and buttons:
public class UIController : MonoBehaviour
{
public List<RelevantClassName> contentToDisplay;
[Header("References"]
public Transform buttonParent;
public Transform contentParent;
public GameObject buttonPrefab;
public GameObject contentPrefab;
public void DisplayUI()
{
foreach(RelevantClassName content in contentToDisplay)
{
content.contentHolder = Instantiate(contentPrefab, contentParent);
content.button = Instantiate(buttonPrefab, buttonParent);
// And here you can run any custom changes, for example finding a text
// component on the content holder and changing the description:
content.contentHolder.GetComponent<Text>().text = content.text;
}
}
Naturally I heavily advise you use a pooling system, and if you're planning to access a component, cache it in that class.
Hope this helps!
So I'm using the AttachToController script to attach a window floated on top of the controller - which works great. In the script that calls up the window, I figure out which hand pressed the controller's menu button and set the Handedness field appropriately (Left or Right). The problem I'm trying to solve is this: Let's say the user clicks on the right controller's menu button and then later on, the left menu button is clicked. The problem I'm experiencing is that even though I've altered the Handedness field, the window still appears attached to the right controller.
private void InteractionManager_InteractionSourcePressed(InteractionSourcePressedEventArgs args)
{
hand = args.state.source.handedness;
...
}
private void SetHandednessAndActivate(GameObject go)
{
AttachToController script = go.GetComponentInChildren<AttachToController>();
if (script != null)
{
script.Handedness = hand;
}
go.SetActive(true);
}
Just to be clear, if the user clicks the left controller menu button first, the window is always on the left and the same holds true for the right controller. What I want is for the window to move to whichever controller is used.
Instead of
script.Handedness = hand;
Use
script.ChangeHandedness(hand);
All the other bits are handled by the script.
I have three buttons, when you tap one button, a panel with a text appear.
The problem is when I attached the script, nothing happens.
The "click" is registered but the the panel never appears.
My script is attached to each of the button and is something like this:
public GameObject panel; //i use to put the panel in unity
bool selected = false;
void Start () {
panel.SetActive(false);
}
void OnSelect() {
selected = !selected;
panel.SetActive(true);
}
I probably have to do something else with the panel but I can't figure it out.
Do it like this:
(1) Add the canvas to your project
(2) BIG TIP - be sure to select Scale with screen size.
That's the only one you ever use. Unity accidentally set the wrong default there, they have not fixed it yet.
(3) In your Canvas, add a BUTTON Make it say perhaps "Test"
(3) In your Canvas, add another BUTTON Make it say perhaps "Another Test"
(4) Make a script something like this...
public class MainScreen:MonoBehaviour
{
public void UserClickedTest()
{
Debug.Log("test..");
}
public void UserClickedAnotherTest()
{
Debug.Log("another test..");
}
}
(5) put ONE copy of that script on ANY object you like. You can put it on your camera, on the canvas, or anywhere else that makes sense
For now let's say you put it on your CAMERA object, for example.
(6) Click on the button "Test" .....
And do this ...
click the PLUS button under OnClick
you see the slot that says "_main" in this example. DRAG your CAMERA item from HEIRARCHY, to that slot
Using the drop down menu:
select the "UserClickedTest()" function ...
good eh?
Now for the other button, do the same but select the "UserClickedAnotherTest()" function.
You're done! Run and test!
You can't use the OnSelect system unless you use ISelectHandler and more stuff: it is difficult for beginners. I strongly recommend the OP masters the simpler technique I explain here. Enjoy!
Maybe you attached the script to the panel. If so, your scripts can not be executed as long as your GameObject is SetActive(false).
I hope I was able to help you.
void OnMouseDown() {
SceneManager.LoadScene ("Scene2");
}
I have tried every conceivable method. The method posted has worked for me using GameObjects with colliders. Instead, this time I am using a button on a 2D canvas. It does not work in this context.
How do I load a new scene using a button in a canvas? I have tried so many different things. This should be simple.
Thanks for any advice.
Here (link: Unity page) you can find a video tutorial how to use Button on canvas in UnityGUI. It's for Unity 4.6 but its really simillar to newest (5.3.1).
It's quite simple. U can make a script with public method e.g
public void LoadScene2()
{
SceneManager.LoadScene ("Scene2");
}
Attach this script to some GameObject e.g Controller. And add event in Button inspector.
In my opinion there is a better solution for the one shown by #Paweł Marecki
I use this in my projects.
OK so you will simply create a script called ButtonManager and inside it you can make a method like this
public void ChangeToScene(string sceneName)
{
Application.LoadLevel(sceneName);
OR
SceneManager.LoadScene(sceneName);
}
Now you have your canvas button, you will select it and look for "Event Trigger"
(i got this image from google to help) add a new mouse down event.
Create an empty GameObject on your Scene, name it "ButtonManager" and drag it onto the event box.
Now you need to click that dropDown list and find your "ChangeToScene" method.
You will see that an editor field appears below, type your desired scene name and hit play :P
This way you will always use this script when you want to change scenes.
You can add other methods and add functionality, but the beautiful part is that you dont need to create a method each time the name of the scene changes.
This has been bothering me for quite some time so here we go...
I'm making a 2D game using XNA framework and part of the game needs the usage of mouse click funtions in UI. By using these textures as buttons it makes another window to pop up with more functions. Now the problem is when you create a button and use it, it will open another window with more buttons in it. Because these 2 buttons on different windows are at the same location, that one mouse click operates both buttons and makes the action of the last button instantly rather than having to click twice.
The real question is how do I use sprites as buttons rather than checking the position and checking if that possition is clicked?
Thanks in advance for any comments.
I created a basic UI framework that has Windows with Controls on them. Windows are stacked by zOrder as are the controls on each window. There is a ScreenManager class that updates every tick that tracks the mouse. When it sees a button click it walks the window stack from top to bottom looking for the first window that contains the click coordinates. It then calls the OnClick sub of that window instance, which does the same thing for the controls on the window. Each method stops walking the control stack as soon as it finds the first control that contains the click coords.
Each unique window in my game is a subclass of that basic window class that creates child control instances and lays them out in the specific way for it's layout. Controls like buttons raise a clicked event that the window subclass handles to do the logic needed for that button.
You could use a state manager in which the proper UI live.
In your case, the first state would have your first buttons, lets call it FirstGameState. When you click on them, the state machine gets you to a another state, lets call it MenuState. You can implement your state manager in a way that allows you to decide who handle events and who doesn't. The only state that handles your click events would be the one flagged to do so. The rest would only draw and update the visual content but handle no user events.
Here is an example for you:
namespace States
{
class StateManager
{
List<GameState> _states = new List<GameState>();
List<GameState> _statesCopy = new List<GameState>();
public Game Game { get; private set; }
public SpriteBatch SpriteBatch { get; private set; }
public StateManager(Game game, SpriteBatch sb)
{
Game = game;
SpriteBatch = sb;
}
public void AddState(GameState state)
{
_states.Add(state);
}
public void RemoveState(GameState state)
{
_states.Remove(state);
}
public void Update(GameTime gameTime)
{
_statesCopy.Clear();
foreach (GameState state in _states)
if (state.RequireUpdate)
_statesCopy.Add(state);
foreach (GameState state in _statesCopy)
if (state.RequireUpdate)
state.Update(gameTime);
}
public void Draw()
{
foreach (GameState state in _states)
state.Draw();
}
}
}