Move UI panel on and off screen, scripted without animation
I have a menu on the left side of my screen that is currently showing. It is a Canvas with a Panel object, a Text object, and some Button objects. I would like the Panel and all its Buttons to be off screen when the game starts. A portion of the Panel and the Text, which is vertical and says "Menu", will be showing. When the player clicks the "Menu", I would like the panel to move onto the screen, showing all the buttons. Another click on the "Menu" text would move the panel and the buttons off screen again.
I have an Event Trigger on Pointer Click set on the "Menu" text. I have a script called "ToggleMenuUI" as a component on the Panel. The Panel is the Object in the "Menu" text Event Trigger using a public function called Toggle() in the script. What am I missing to make this setup work?
UPDATED: Figured it out! I had to use the anchoredPosition property rather than the position property.
Here is the (working!) script, kept up for helping others: using UnityEngine; using UnityEngine.UI; using System.Collections;
public class ToggleMenuUI : MonoBehaviour {
private RectTransform rt;
private Vector2 open;
private Vector2 closed;
private bool IsOpen;
void Start () {
rt = GetComponent<RectTransform>();
open = new Vector2(-5.0f, -5.0f);
closed = new Vector2(-82.0f, -5.0f);
IsOpen = false;
}
public void Toggle () {
if (IsOpen)
{
rt.anchoredPosition = closed;
IsOpen = false;
}
else
{
rt.anchoredPosition = open;
IsOpen = true;
}
}
}
Hello,
I wish to say thank you, @ledisnomad. This piece of information has helped with my game menu's.
Hello, I was looking for answer to this for some time and yours was the first I actually made some sense from. :) Thank you!
Your answer
Follow this Question
Related Questions
Deleting random objects in started project 0 Answers
Trying to make an object appear in a position, but only if that position isn't occupied. 0 Answers
Canvas Structure for Different UI Panels 0 Answers
How do I keep two UI text unity3d on the scene 2 Answers
How do i change a color of an individual alphabet of a UI.Text 2 Answers