How to get the gameobject- which triggered the UI.Button click function - unity3d

in Unity 5- I am trying to disable a ui.button- once it is clicked. There is a list of buttons- among which- the clicked one will be disabled. But I am not sure, if it is possible to get the event triggering gameobject.
Unity Editor-
Code:
// called from ui.button clicks
public void callThisMethod(string param) {
// how to get the clicked button gameobject here
}

Just Add another event to return the button
public Button ButtonPressed(Button ThisButton)
{
return ThisButton;
}

You can add something like this to your code:
public Button yourButton;
public void yourButtonClicked(){
yourButton.interactable = false;
}
Assign your button to the Button object in inspector. Then where it says "On Click()" in the image you have above, select the script the above code is added to, and select the "yourButtonClicked()" function. That will disable the button once it is clicked.
To get your clicked button game object, you can use: EventSystem.current.currentSelectedGameObject

Pass the gameObject of the button as a parameter while your are adding event listener to the UI button.
Hope that helps:
GameObject myButtonGameObject = myButton.gameObject;
myButton.onClick.AddListener(() => {LogName(myButtonGameObject); });
public void LogName(GameObject buttonGameObject = null){
Debug.Log(myButtonGameObject);
}
Note: The given approach is useful, when you need to dynamically show the clicked buttons name, but if you know the exact button, you can make a public field for the myButton and get the name without passing any parameters.

Related

How to get text from specific child(button) when there are many

I have 6 buttons. Each has a different text on it. Pushing any button calls the one same method which changes the text and changes button positions between them. I can't figure out how to get the text from a specific button when it pushed. I can only return either random button text or all of them. Somewhat like this:
public void ReadFromButton()
{
Button button;
button = GetComponent<Button>();
var koba = button.GetComponentInChildren<Text>().text;
Debug.Log(koba);
}
So I need any advice. Thanks.
buttons
You can add an int to your button click function like ReadFromButton(int i) and then in unity give your buttons numbers in onclickevent, according to your public Text[] buttontexts
Like:
public Text[] buttontexts;
public void ReadFromButton(int i)
{
var koba = buttontexts[i].text;
Debug.Log(koba);
}
But i believe there is more ways to do that.

How to call method of another class from differnet class in Xamarin

I am new to xamarin.forms.
On main page I have button "Select Photos". When user click popup open with number of images. When user tap and image popup closes.
I want to do is when popup close; I want to display the selected image on main page so user know what image they selected.
So I have method on the popup page for image click. In the method I save the image name as variable. And I try to call another method which is on main page. The Method on the main page would get the variable and display the Image.
This is code when user tap image
public void Idpty1(object sender, EventArgs args)
{
Signtype = "1";
//save the image name as variable
SelectedTypeImage = "idpty1.png";
//On the Newphoto page; call close popup function.
new NewPhotoPage().ClosePopover();
}
This is function on main page and I am trying to call this function with above function.
public void ClosePopover()
{
//Close the popover
PopupNavigation.Instance.PopAsync();
//Get the variable which was set on the popover page (image name)
SelectedTypeImage = MyPopupPage.SelectedTypeImage;
// Source the image from variable.
SelectedType.Source = SelectedTypeImage ;
//DisplayAlert("Alert2", SelectedTypeImage, "ok");
System.Diagnostics.Debug.WriteLine("test");
}
This is image code in the main page
<Image x:Name="SelectedType" Resources=""></Image>
In the above code; image part does not work, image source does not work also display alert does not work. BUT SYSTEM.DEBUG WORKS.
What I don't understand is function does get call but even display alert does not work.
I use something like
public void Idpty1(object sender, EventArgs args)
{
Signtype = "1";
//save the image name as variable
SelectedTypeImage = "idpty1.png";
//On the Newphoto page; call close popup function.
// new NewPhotoPage().ClosePopover();
Xamarin.Forms.MessagingCenter.Send<App> ((App)Xamarin.Forms.Application.Current, "CallMethod");
}
in the constructor of Main Page.
MessagingCenter.Subscribe(this, "CallMethod", (sender) => {
// do something
ClosePopover(); // <-- run u' method.
});
On your Newphoto page, just call ClosePopover method as:
YOURCLASSNAME.ClosePopover();
assuming they are in the same namespace.
Else, use using to add the namespace of your class.
Let me know if you need more clarification.

How to access the currently selected item within Wicket Palette

I am trying to override certain features of the Wicket Palette. I have attached a picture of what i am trying to accomplish with Palette. Basically in addition to the select-item-clickbutton-moveToRight functionality of Palette, I also want to know which item has been selected before it is moved. When I select an item in either of the panels and click on a View button, I should be able to display an html page related to the currently selected item from the Palette.
Right now, the button is placed out of the Palette code and as long as I can get the ID of the selected element, I will be able to accomplish my objective.
I am stuck at the point where I need to know which item has been selected within the palette.
Here's what I have tried so far:
1. Adding an onclick listener to the choicesComponent using the AjaxFormComponentUpdatingBehavior
final Palette classFormMapping = new Palette("formsPalette", new ListModel(selectedFormsList),
formsList, new CustomObjectChoiceRenderer(), 8 , false ){
#Override
protected void onBeforeRender() {
super.onBeforeRender();
getChoicesComponent().add(new AjaxFormComponentUpdatingBehavior("onclick"){
#Override
protected void onUpdate(AjaxRequestTarget target) {
System.out.println("REACHED HERE"+ getFormComponent());
/*
* The code reaches here for each click but I am unable to know which item was selected */
}
});
}
};
Adding a Recorder component to the Palette with an "onclick" listener.
This listener does not get called at all.
final Palette classFormMapping = new Palette("formsPalette", new ListModel(selectedFormsList),
formsList, new CustomObjectChoiceRenderer(), 8 , false ){
protected Recorder newRecorderComponent() {
Recorder recorder = super.newRecorderComponent();
recorder.add(new AjaxFormComponentUpdatingBehavior("onclick") {
private static final long serialVersionUID = 1L;
#Override
protected void onUpdate(AjaxRequestTarget target) {
System.out.println("reached record on click ");
}
});
return recorder;
}
};
Trying to create this palette with a custom button
Please help. Thanks in advance.
Palette.java javadoc explains how to "Ajax-ify" it: https://github.com/apache/wicket/blob/529db58c413861677f7ff6736f9363edf42ae85a/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/palette/Palette.java#L52-L71
But this won't help you because the selection is done at the client side first and then Wicket is notified:
https://github.com/apache/wicket/blob/529db58c413861677f7ff6736f9363edf42ae85a/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/palette/palette.js#L118-L127
You need either to register your own JS event listener for 'change' event before the Wicket one or monkey-patch palette.js to override Wicket.Palette.updateRecorder() function.

Filter button properties set for right click

I want to give the filter button a right click function that when I right click it, it will clear all the grid criteria.
setFilterOnKeypress(false);
setFilterByCell(true);
setFilterButtonPrompt("Left click to filter, right click to clear all texts.");
Button button = new Button();
button.addClickHandler(new ClickHandler()
{
#Override
public void onClick(ClickEvent event)
{
if (event.isRightButtonDown())
{
SC.warn("right clicked");
clearCriteria();
}
}
});
setFilterButtonProperties(button);
This is not working, any ideas on why it isnt working?
Keep the functionality separate. There is no meaning of mixing two different tasks on the same button. Think from the end user's perspective.
Read more on your another post Pass a handler to filter button property that is some what asked in the same context.

Radio button change handler refreshing the page

I'm using UploadItem, RadioGroupItem and some other widgets. RadioButton is having onChangeHandler which will decide what all other components need to be displayed. I've uploaded some file using UploadItem. Then I changed the radio button selection. On changing the radio button, required widgets are getting displayed properly but whatever file I'd selected using UploadItem is going away. Fresh UploadItem widget is getting displayed. In other words page is getting refreshed.
My requirement is whenever I change radio button option, required widget should displayed along with that whatever file I had selected using UploadItem should remain same.
My Code is something like this:
UploadItem upload = new UploadItem();
RadioGroupItem radioGroup = new RadioGroupItem();
HashMap map = new HashMap();
map.put("option1","option1");
map.put("option2","option2");
radioGroup.setValueMap(map);
TextItem textbox = new TextItem();
radioGroup.addChangeHandler(new ChangeHandler(){
public void onChanged(ChangedEvent event) {
String radioValue =((String)event.getValue());
if(radioValue.equalsIgnoreCase("option2")){
textbox.show();
}else{
textbox.hide();
}
}
});
Add all created widgets to DynamicForm object using dynamicForm.setFields(all created widgets)
Changing the radio button should hide and show the textBox. But while doing that page is getting refreshed and whatever file we had selected using UploadItem is lost.
As per the documentation for hide() and show() of FormItem class, invocation of any of these methods, will cause the DynamicForm to be redrawn.
So it may cause the problem you're getting.
To overcome this issue, I would suggest you to put UploadItem in a separate DynamicForm.
fire an event on radio Selection change as
radioButton.addListener(Events.Change, new Listener<BaseEvent>() {
#Override
public void handleEvent(BaseEvent be) {
if(radioButton.getValue()){
//fire an event here for ur widget
}
}
});