Question by
PersianKiller · Aug 16, 2019 at 03:33 PM ·
button
call a function depends on the mouse position on a button?
ok let's say i have 2 functions,
public void a()
{
Debug.Log("left");
}
public void b()
{
Debug.Log("right");
}
how can i do this ?
//if (cursor position is on the left side of the button when you click ){
//a();
//}
//else if (cursor position is on the right side of the button when you click ){
//b();
//}
any idea?????
untitled.png
(4.1 kB)
Comment
Best Answer
Answer by sacredgeometry · Aug 16, 2019 at 03:36 PM
Method A
So here is a simple way to get it to work on a single button based on the inputs position.
public Button MyButton;
private RectTransform _buttonRect;
void Start()
{
_buttonRect = MyButton.GetComponent<RectTransform>();
MyButton.onClick.AddListener(() => {
if(_buttonRect.sizeDelta.x * 0.5 > Input.mousePosition.x)
{
Debug.Log("Left Side");
}
else {
Debug.Log("Right");
}
});
}
thanks but can you show me an example? :) (i'm working on the links )
Yes sure thing. As soon as I am near a computer I will do.
thanks dude :) ,i have to do some changes but its fine,
Your answer
Follow this Question
Related Questions
Sprite change doesn't work in built game. 0 Answers
Hold touch button Unity Javascript 1 Answer
UI Buttons Trigger displaced after build. Help Please 0 Answers
Make button move character? 1 Answer
Button Click 1 Answer