Took 3+ hours, but I have GUI buttons partly working.
The clicking on the buttons is working.
I need now to detect in the C# script when and which button is being clicked.
How would I detect which GUI button was pressed and perform some action in a C# script?
I shall again pimp my Datasacks package! Apologies to all who are getting tired of me pointing this one out, but Datasacks is actually engineered to do PRECISELY this, without ever hooking anything up between your scene and your controller script.
For the above, you would make sure each GameObject containing a button is named uniquely, then you would put an instance of DSButtonSetUIIntent.cs on each button.
Over in your controller script for this scene, you would have this sort of a construct (in a Monobehavior on any GameObject):
// Here is an example data subscription pattern for your Monobehavior:
// It falls back to DSM.UserIntent because that is the default sack.
public Datasack dataSack; // populate this in the Unity editor, or NOT (see below in OnEnable()...)
void OnUserIntent( Datasack ds)
{
// Here is where you service the notification
// that the contents of this Datasack changed
// You can inspect the name of the Datasack if
// you expect more than one Datasack to call
// this function.
switch( ds.Value)
{
case "ButtonAchievements":
// start achievements scene
break;
case "ButtonPlay":
// start playing the game
break;
}
}
void OnEnable()
{
if (!dataSack) dataSack = DSM.UserIntent;
dataSack.OnChanged += OnUserIntent;
}
void OnDisable()
{
dataSack.OnChanged -= OnUserIntent;
}
Sample code above is from the README.md file in the repository, and there are other examples in the Unity scene.
The cool part is you don’t need to make any fragile drag-and-link stuff between your scene and the controller script.
Thanks for the reply, but we really don’t want to add another dependency to our project.
If someone could quickly point us to a solution we would be happy!