- Home /
How to detect if UI button is clicked?
Yeah, I know this question has been asked thousand times on the internet... But I find it very confusing...Because I'm just a beginner...
I'm converting GUI Button Eventto UI Button Event
Here's my code:
public void ShowSave()
{
for (int i = -1; i < saveGames.Count; i++)
{
if (i == selectedSaveGameIndex)
{
string str = SaveInputField.GetComponent<InputField>().text = saveGameName;
//string str = GUILayout.TextField(saveGameName, GUILayout.MinWidth(200));
if (regularExpression.IsMatch(str))
{
if (str.IndexOfAny(newLine) != -1)
{
//New Line detected
if (i >= 0)
{
SaveLoad.DeleteFile(slu.saveGamePath, saveGames[i].savegameName);
}
slu.SaveGame(saveGameName);
selectedSaveGameIndex = -99;
return;
}
else
{
saveGameName = str; //All OK, copy
}
}
else
{
Debug.Log("Irregular expression detected");
}
//This LINE
if (GUILayout.Button("Save", GUILayout.MaxWidth(50)))
{
if (i >= 0)
{
SaveLoad.DeleteFile(slu.saveGamePath, saveGames[i].savegameName);
}
slu.SaveGame(saveGameName);
selectedSaveGameIndex = -99;
saveGames = SaveLoad.GetSaveGames(slu.saveGamePath, slu.usePersistentDataPath);
return;
} //THIS LINE
I want this to happen when the user Click on the button : if (GUILayout.Button("Save", GUILayout.MaxWidth(50))) { if (i >= 0) { SaveLoad.DeleteFile(slu.saveGamePath, saveGames[i].savegameName); } slu.SaveGame(saveGameName); selectedSaveGameIndex = -99; saveGames = SaveLoad.GetSaveGames(slu.saveGamePath, slu.usePersistentDataPath); return;
Thanks!
Well, normally, the way you wrote it works perfectly. Doesn't it?
Well it does... But the problem is, it's written for GUI based interface I wanna convert it into UI based interface
Is this for game/runtime? I'm asking because you are using old I$$anonymous$$GUI, which should mostly be only used for Editor scripting...
Answer by prakyathd801 · Nov 12, 2020 at 12:46 PM
public int ShowSave()
{
for (int i = -1; i < saveGames.Count; i++)
{
if (i == selectedSaveGameIndex)
{
string str = SaveInputField.GetComponent<InputField>().text = saveGameName;
//string str = GUILayout.TextField(saveGameName, GUILayout.MinWidth(200));
if (regularExpression.IsMatch(str))
{
if (str.IndexOfAny(newLine) != -1)
{
//New Line detected
if (i >= 0)
{
SaveLoad.DeleteFile(slu.saveGamePath, saveGames[i].savegameName);
}
slu.SaveGame(saveGameName);
selectedSaveGameIndex = -99;
return;
}
else
{
saveGameName = str; //All OK, copy
}
}
else
{
Debug.Log("Irregular expression detected");
}
return i;
}
//This LINE
//Call this function on onclicking the button
public void newfunction()
{
int i= ShowSave() ;
if (GUILayout.Button("Save", GUILayout.MaxWidth(50)))
{
if (i >= 0)
{
SaveLoad.DeleteFile(slu.saveGamePath, saveGames[i].savegameName);
}
slu.SaveGame(saveGameName);
selectedSaveGameIndex = -99;
saveGames = SaveLoad.GetSaveGames(slu.saveGamePath, slu.usePersistentDataPath);
return;
} } //THIS LINE
thnx bro! it worked! I edited the script further more!
Answer by thevortexbm · Nov 07, 2020 at 01:15 PM
Create a public method and assign it to the On Click method in the Editor. Make sure the script is on a GameObject
The problem is "i" is local variable. How can I declare it beyond the scope? That's the problem. I think you didn't read the script fully.
Answer by sevalk · Nov 07, 2020 at 03:06 PM
Create a public method that contains the actions you want to be done when the button is clicked. Create a GameObject in unity and assign the script containing this method to this GameObject. Then assign this GameObject to the Onclick method of the button you created and select the public method you want it to work with.
i is declared in showsave(), o which it's local var... How can I declare it another method. I know what are you saying, and I know that. Read the method again. Thnx!