In developing a mod for Cities: Skylines, I have run across a problem.
Most of my code, as far as I can tell, works fine - but so far, I haven't got to test it. This is because it should all get called in sequence when a button I have added to the UI gets clicked. The click event on this button is not calling the handler I've assigned to it.
This is where I create the button:
public class LoadingExtension : LoadingExtensionBase
{
public override void OnLevelLoaded(LoadMode mode)
{
Debug.LogDebugMessage("Adding 'Generate City Report' button to UI");
// Get the UIView object. This seems to be the top-level object for most
// of the UI.
var uiView = UIView.GetAView();
// Add a new button to the view.
var button = (UIButton)uiView.AddUIComponent(typeof(UIButton));
// Set the text to show on the button.
button.text = "Generate City Report";
// Set the button dimensions.
button.width = 250;
button.height = 30;
// Style the button to look like a menu button.
button.normalBgSprite = "ButtonMenu";
button.disabledBgSprite = "ButtonMenuDisabled";
button.hoveredBgSprite = "ButtonMenuHovered";
button.focusedBgSprite = "ButtonMenuFocused";
button.pressedBgSprite = "ButtonMenuPressed";
button.textColor = new Color32(255, 255, 255, 255);
button.disabledTextColor = new Color32(7, 7, 7, 255);
button.hoveredTextColor = new Color32(7, 132, 255, 255);
button.focusedTextColor = new Color32(255, 255, 255, 255);
button.pressedTextColor = new Color32(30, 30, 44, 255);
// Enable button sounds.
button.playAudioEvents = true;
// Place the button.
button.transformPosition = new Vector3(-1.0f, 0.97f);
// Respond to button click.
// NOT GETTING CALLED
button.eventClick += ButtonClick;
}
public void ButtonClick(UIComponent component, UIMouseEventParameter eventParam)
{
Debug.LogWarningMessage("HIGH LOGIC: Beginning report generation.");
string now = DateTime.Now.ToFileTime().ToString();
string filepath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string filename = filepath + "\\CityReport-" + now + ".xlsx";
Output fileCreator = new Output(filename);
fileCreator.GenerateReport();
}
}
According to the documentation I've been following, using
button.eventClick += ButtonClick;
should add ButtonClick
as the click handler for the button. However, clicking on the button does nothing. The debug message at the start of the handler (the "HIGH LOGIC" message) doesn't get displayed (note: the earlier debug message about adding the button to the UI does). No error messages are displayed either in the game's debug panel or in VS.
I have also tried using new MouseEventHandler(ButtonClick)
, since the VS inline documentation tells me that the type of eventClick
is MouseEventHandler
. This doesn't show any errors in VS or the game, but doesn't work either.
(Note: there is official documentation, but it's next to useless.)
Does anyone here have experience with the C:S API? Why is this event not getting called?
You might try taking a small step back to check if a different UI element works; for example, by following the example for adding a UILabel. Registering a click event handler to this one possibly might work, since there's an actual example to follow; although, the documentation says to place this code inside the Start method. I'm not sure, but maybe the UIButton in your code should be placed in a Start method as well.
As an aside, I stumbled upon this link for debugging. Skylines seems to have its own messaging system for debugging.
Something that I did notice in your code is that the first Debug statement uses a different method than the second Debug statement:
It might not make any difference, but I would test out that Debug.LogWarningMessage call by moving it where the Debug.LogDebugMessage is to see if it actually works.
According to the extremely terse documentation there's a log file called output_log.txt, perhaps there might be some info contained within this file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With