I needed to create UIButton attached to a gameobject.
I'm using a board based game i.e., when ever clicking a button at that time place one object to that particular button.
I am using c# script in unity3d .
void UIBtn(GameObject BName)
{
//here to write Button click event.
}
I'm assuming you mean GUI.Button .
Reading your first sentence I understood that you wanted to create a button where a GameObject is, but reading your second sentence it seems that you want a GameObject to appear when clicking a button. Since I'm not sure, I'll answer both.
To make a GUI button appear at the place the mouse is use something like:
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour {
void OnGUI() {
Vector2 screenPos = Event.current.mousePosition;
GUI.Button ( new Rect(screenPos.x,screenPos.y,100,100),"Hello");
}
}
Attaching a button to a GameObject requires first identifying the GameObject via Physics.Raycast and then getting the GameObject from the HitCollider and then, on the game object's OnGUI loop. constantly translate its world coordinates to screen coordinates to be able to show a button, via GUI.Button.
Related
We are working on our school project and we are all beginners in Unity and C#.
There is a toggle group with three toggles on our UI panel and three cubes are on the screen. We are having trouble with writing scripts for these cubes. What we want is when the corresponding toggle is on, the color of the cube can be changed by clicking.
For example, when toggle 1 is on, we click cube1 and its color change.
For now, we know how to use the toggle to change color by scripts, but we are not sure how to write a cube script by checking whether the toggle is on or not to change the cube color using mouse clicking.
You can check if a toggle is on or off by using .isOn
using UnityEngine;
using UnityEngine.UI;
public class CheckScript : MonoBehaviour
{
public Toggle myToggle;
void Start()
{
if(myToggle.isOn)
{
Debug.Log("Toggle is on");
}
else
{
Debug.Log("Toggle is off");
}
}
}
But I think what you actually need is this answer https://stackoverflow.com/a/57341924/19611934
I know this is a really simple question but I can't figure out how to archive this:
I have a UI button in my scene and I want Vuforia to instantiate one AR Model ONLY when I press a button.
Following the tutorial on the net I was able to instantiate a model on the screen when I touch it but I need to know how to set up Vuforia for archive the same result only when I press a button.
I have to disable the "Anchor Input Listener Behaviour"?
And then?
I want to call for the PositionContentAtPlaneAnchor but I can't figure out how to call it in the right way in the OnClick field of the button. I need to make a custom script for this?
Thanks for any answer.
Ok, sorry for the delay.
I deduce that you are working with the ground plane, if you have the Ground Plane Stage and the Plane Finder in the scene and works, we're at a good point.
Now, you must only add a button to the scene and into the script add something like this:
public PlaneFinderBehaviour plane;
void Start()
{
...
buttonOnTheScene.onClick.AddListener(TaskOnClick);
...
}
void TaskOnClick()
{
Vector2 aPosition = new Vector2(0,0);
...
plane.PerformHitTest(aPosition);
}
What does it mean?
First of all, you must move the Plane Finder from Hierarchy to the script variable, so we have a reference to the plane into the script.
Then when you click (or tap) on the button you simulate the click (or tap) on the display with the PerformHitTest.
If you're wondering why my question in the comment, it's because the Plane Finder Behaviour Script has two modes type: Interactive and Automatic. The Interactive intercept the tap on the display and shows the object (on the ground plane) in the exact position of the tap, the automatic shows the object in the center of the plane.
So if you want the object in an exact position you can pass a Vector2 position in the PerformHitTest and if you want to show an object programmatically or do something when it shows the object you can call a custom method OnInteractiveHitTest.
That's all.
This question already has answers here:
How to detect click/touch events on UI and GameObjects
(4 answers)
Closed 4 years ago.
I am trying to create a very simple button in Unity, using the information provided in this question but after a multitude of attempts it won't seem to work.
I have a "button" object, with a SpriteRenderer and BoxCollider2D component, a Physics2DRaycaster attached to my main camera, and the following code attached to the object:
using UnityEngine;
using UnityEngine.EventSystems;
using System.Collections;
public class ButtonController : MonoBehaviour, IPointerClickHandler
{
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void OnPointerClick(PointerEventData eventData)
{
Debug.Log("Clicked" + eventData.pointerCurrentRaycast.gameObject.name);
}
}
This is the scene, there is clearly nothing obstructing the button:
There is only the button and the camera in the scene:
And these are the components attached to both the camera and the button:
I'm aware this isn't strictly the kind of question to ask on here but it's been several days now and this should have been something very simple but can not for the life of me see what's gone wrong.
Do you have an Image component added to your object? For the event to trigger you need to have a component that can be a RaycastTarget, ie, it won't trigger from an empty gameObject, it also needs to be child of Canvas.
If you start from fresh scene and create any UI element from the GameObject menu, Unity will create an Event System, Canvas and InputModule components, which is a great start - if you can click the button, you should get your debug if you click on an image with your script - your code is correct.
For a sprite working with the event system, you'll need a physics raycaster and collider. A 3D Box collider works with Physics Raycaster, and 2D Box Collider works with Physics2D Raycaster, but if no collider is present your sprite won't intercept raycasts, they need colliders to work
I have a plane that contains around 200 tiles, and I'd like to be able to detect which tile has been clicked by the player. Each tile has a box collider attached. I've also created an empty game object in the scene to which I attached EventSystem and the below script:
public class PlaneBehaviour : MonoBehaviour, IPointerDownHandler {
public GameObject ClickSymbol;
public void Start() {
var physicsRaycaster = FindObjectOfType<PhysicsRaycaster>();
if (physicsRaycaster == null) {
Camera.main.gameObject.AddComponent<PhysicsRaycaster>();
}
}
public void OnPointerDown(PointerEventData eventData) {
var o = eventData.pointerCurrentRaycast.gameObject;
}
}
Now, when user clicks anywhere, nothing happens - I've put a breakpoint inside the OnPointerDown method, but it's not getting hit (and the code inside the Start method is run - I've also verified that by putting a breakpoint in there).
There are many things that could cause the callback function not being called. Before trying these, put Debug.Log inside the OnPointerDown function to make sure that it's being called:
1.Go to GameObject-->UI--->EventSystem.
A GameObject named "EventSystem" will be created and will proper scripts such as EventSystem, StandAloneInputModule attached to it.
2.The PlaneBehaviour script must be attached to the GameObject with the Collider not to an empty GameObject. It detects click on the Objects it is attached to.
3.If you are using a collider that ends with "2D" in its name then Physics2DRaycaster must be used instead of PhysicsRaycaster.
4.Are you using more than two cameras? If so manually attach PhysicsRaycaster to the correct Camera that is currently showing that Object.
5.Re-size the GameObjects you want to detect clicks on to be bigger. I've seen doing this solve the problems sometimes. You then have to move the camera back if this works.
I'm having a hell of time getting a 2D polygon collider to register a mouse click. I've attached images and code to show where I'm at. I cannot get the click to work.
Ultimately, the thing I'm trying to achieve is to just set the area defined by the collider to be clickable rather than the entire image sprite.
What am I doing wrong here? Need help!
using UnityEngine;
using System.Collections;
public class MouseClick : MonoBehaviour
{
void OnMouseDown()
{
Debug.Log ("Clicked the Collider!");
}
}
I've defined the collider:
I've setup the components for my image:
Set your Canvas "Render mode" to "Screen Space Camera" and attach your camera,
Your using the new uGUI system, you dont even need colliders!
Make your script look like this and you should be able to do it with any object that has Image component. so you can get rid of the collider!
using UnityEngine.UI;
using UnityEngine.Events;
using UnityEngine.EventSystems;
public class MouseClick : MonoBehaviour, IPointerClickHandler
{
void OnPointerClick(PointerEventData data)
{
Debug.Log ("Clicked the Collider!");
}
}
I want to suggest to you a different approach seeing as this uses UGUI.
Attach an EventTrigger script. Then add a new trigger of type PointerClick. Now you can drag an object into that and call any function on that object. This is a very easy and reusable way.
With this in place you can safely remove you rigidbody and collider unless those are needed for something other than mouse interaction.