Position a UI button on bottom left of the viewport/screen? - unity3d

How do I place a UI Button (the new UI) so that it is always at the bottom-left on the screen/viewport? My idea was to do it in code like this:
void Awake () {
fireButton.transform.position = new Vector3(Screen.width - 50, Screen.height - 50, 0);
}
It is not working at all. How can I best do this?

Instead of doing it in code, click on the UIButton in the scene or hierarchy then go into the inspector and in the top left of the transform component you will see a square, click it, hold shift and alt and then click the 'Bottom Left' square to latch it to that side of the screen.

Related

move object and it's children out of camera in unity2d

I have made a gameobject together with some children gameobject to represent the information to show up when specific circumstances occurred.
I have already ajusted the position of the information gameobject(together with its' children) in the cameraarea. The thing is that I want to move the gameobject(together with its' children) out of the camera, maybe on top or maybe on left. Following is the scratch to demonstrate the position I want to put it:
So that I could move the information gameobject and its' children (Red box) with some movement effect when needed, I have no problem with moving it back but could find an elegant way to move it out of the camera when the game started.
Mostly because I don't know how to calculate the position out of the camera.
Maybe find the upper border of the camera and the size of the gameobject and its children?
I know I could do this by maybe add a marker gameobject to represent the downer border of the information gameobject, and move it until it's not visible, but is there a more elegant way?
Any ideas?
For this one, I would use the following trick: use any method (animation, coroutine, Update method...) to move your item out of screen the way you desire. Then you can use the OnBecameInvisible event which is called when the item does not need to be rendered on any camera anymore. The event will there be used to detect that the parent object moved out of screen, and that you want to stop the current movement. You then just need to define in this event that you want to stop the current moving behavior, and you will be done.
void OnBecameInvisible() {
// Stop moving coroutine, moving in Update or current animation.
}
There are probably more elegant ways of doing it as you said, but I think this method should be enough for what you want to achieve.
It took me time, but I found this way for you, attach this script to your gameObject:
public Renderer rend;
//drag the camera to the script in the inspector
public Camera camera1;
Vector3 bottomleft;
Vector3 topright;
void Start()
{
rend = GetComponent<Renderer>();
//the top-right point of the camera bounds
topright= camera1.ViewportToWorldPoint(new Vector3(0, 0, transform.position.z));
//the bottom-left point of the camera bounds
bottomleft = camera1.ViewportToWorldPoint(new Vector3(1, 1, transform.position.z));
StartCoroutine(MoveUp());
}
IEnumerator MoveUp()
{
//while the position and the height are lower that the Y of top right
while (transform.position.y + rend.bounds.size.y < topright.y)
{
//move the object to the new position (move it up)
transform.position = new Vector3(transform.position.x, transform.position.y + .01f, transform.position.z);
//and wait for 1/100 of a second
yield return new WaitForSecondsRealtime(.001f);
}
}
you can play with the WaitForSecondsRealtime value to change the velocity of moving.

gtkmm Gtk::Layout negative child position not setting its absolute position

The description of Gtk::Layout says
Infinite scrollable area containing child widgets and/or custom drawing
I put a Gtk::Layout inside a Gtk::ScrolledWindow, gave the Gtk::Layout a size of 400x400 and then placed a Gtk::Button at (450, 450), but the scrollbars only appeared until the scrolled window was resized to 400x400, not (450+button-width x 450+button-height). So I had to move the bottom-right corner of the layout by connecting a handler to button's signal_size_allocate() signal and manually call Gtk::Layout::set_size() function to resize it up to button's boundary. Now the scrolled window shows scrollbars to reach the button. Up to this everything was OK.
class LayoutWindowTest : public ApplicationWindow {
ScrolledWindow sw;
Layout l;
Button b;
public:
LayoutWindowTest()
: l(), b("test") {
set_title("layout test");
set_size_request(400, 400);
sw.set_hadjustment(l.get_hadjustment());
sw.set_vadjustment(l.get_vadjustment());
l.set_size(400, 400);
sw.add(l);
add(sw);
// b.get_allocation() won't work because button has not been allocated a
// size yet
b.signal_size_allocate().connect(
[this](Allocation& btn_sz) {
l.set_size(btn_sz.get_x() + btn_sz.get_width(),
btn_sz.get_y() + btn_sz.get_height());
});
l.put(b, 450, 450);
show_all_children();
}
virtual ~LayoutWindowTest() {}
};
Initial window showing scrollbars and then scrolled to bottom-right corner to show button
Now I need to do the same thing for the top-left corner, i.e. put the button out of visible boundary of scrolled window and then use the scrollbars to reach the button. I tried putting the button at (-10, -10), but unlike the previous case I can't set layout's top-left corner with code by resizing it, so can't move the layout's scrollbars. When I resize the scrolled window manually by its top-left corner the button moves towards top-left with it instead of staying there. How to remedy this situation?
Initial window with button at (-10, -10) and no scrollbars, tried resizing the window by top-left handle but button moves with it
In short, how can I scroll in both top-left and bottom-right directions infinitely?

Unity - Raycast hit, wrong collider

Here is my code:
using UnityEngine;
public class InputController : MonoBehaviour {
void Update() {
if (Input.GetMouseButtonUp(0)) {
var hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
if (hit.collider != null) {
var e = hit.collider.gameObject;
Debug.Log(e.transform.position.x + ":" + e.transform.position.y);
}
}
}
}
When I click/touch a cell, sometimes the hit is good and the cell is revealed, sometimes the hit is always false (it's like I touch the wrong cell) and sometimes it depends of the position of my click (see images below).
I click the left part of the cell, the console says I click the cell on the left.
I click the right part of the cell, the console says I click the right cell (and reveals it). In this case it's left / right but it can be top / bottom, a corner...
I don't know if my problem is very well explained sorry, and my English is not perfect... Don't hesitate to ask me more details!
Ok I find the problem.
The left cell had a scale of 2 so it overlaps the right cell.
I Guess Here is problem with collider overlaping. you have to just reset all the collider and check it, is it overlaping on any object or not?
you can check colider on gamemode , turn on "Gizmo". so you will see all the collider and check it again is it overlaping on there or not ?

Change sprite on mouseover

I have a sprite that acts as a button in the main menu of my game. It has a box collider, and I use OnMouseDown() to register clicks of the button.
I want to make the sprite change when my mouse rolls over the button. I know I can do stuff using the function OnMouseOver() but how do I switch between 2 sprites completely?
You don’t need to use two sprites and switch between them. Just use an effect like this one:
void OnMouseOver()
{
transform.GetComponent<SpriteRenderer>().sprite.color = "your new color for clicking effect";
transform.GetComponent<SpriteRenderer>().sprite.localScale -= new Vector3(0.1f, 0.1f, 0.1f);
}
and get back all this proccess in OnMouseExit. Or, if you still want to change sprite, you can change it like so:
Sprite sprite;
Sprite highlightSprite;
void OnMouseOver()
{
transform.GetComponent<SpriteRenderer>().sprite = highlightSprite;
}
void OnMouseExit()
{
transform.GetComponent<SpriteRenderer>().sprite = sprite;
}
and change back in OnMouseExit.
put the mouse functions onto an empty parent GameObject, and parent it to all of the sprites you want, then just enable/disable them via the parent
Other options:
Use worldspace UI button instead (then you can use the built-in spriteswap transition and other button methods : http://docs.unity3d.com/Manual/script-SelectableTransition.html )
Add public Sprite variable and assign mouseover sprite to that, then in OnMouseOver() use that sprite image in your button sprite. (and revert back to original sprite on mouse out)
Could also use Mecanim animation, OnMouseOver() toggle to another animation (which only has 1 frame)

Unity detecting user no longer holding button

I'd like to have a button displayed on screen, and as long as it's held, the user would turn.
This is how I created the button, and how I tried marking when the button is held:
if (GUI.Button (new Rect (OffsetUI, Screen.height - OffsetUI * 7, OffsetUI * 6, OffsetUI * 6), "left")) {
playerScript.LeftButtonHeld = true;
}
else {
playerScript.LeftButtonHeld = false;
}
Unfortunately this doesn't seem to work. I believe the button gets into 'if' statement, whenever the button is clicked (on finger up).
How can I detect when a user is holding the button?
Instead of GUI.Button, you will want to use GUI.RepeatButton as shown here:
http://docs.unity3d.com/ScriptReference/GUI.RepeatButton.html