Why is this added UI button event not triggered?

When developing a mod for cities: Skylines, I ran into a problem.

Most of my code, as far as I can tell, works great, but so far I don't need to check it. This is due to the fact that all this must be called up sequentially when the button added to the user interface is pressed. The click event on this button does not call the handler that I assigned to it.

Here I create a 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 am tracking using

 button.eventClick += ButtonClick; 

should add ButtonClick as a click handler for the button. However, pressing the button does nothing. A debug message at the beginning of the handler (the message "HIGH LOGIC") is not displayed (note: an earlier debug message about adding a button to the user interface). Error messages are not displayed on the debug panel of the game, nor in VS.

I also tried using new MouseEventHandler(ButtonClick) , since the inline VS documentation tells me that the eventClick type is MouseEventHandler . It does not show errors in VS or game, but also does not work.

(Note: official documentation, but this is next to useless.)

Does anyone have any experience with the C: S API? Why is this event not triggered?

+5
source share
3 answers

You can try to take a small step backwards to check if another user interface element is working; for example, following the example of adding UILabel . Registering a click event handler on this may work, as there is a real example; although the documentation says to place this code inside the Start method. I'm not sure, but maybe the UIButton in your code should also be placed in the Start method.

Aside, I came across this link for debugging . Skylines seems to have its own debugging messaging system.

Something I noticed in your code is that the first Debug statement uses a different method than the second Debug statement:

  • Debug.LogDebugMessage ("Adding" Create a city report "to the user interface");
  • Debug.LogWarningMessage ("HIGH LOGIC: start of report generation.");

This may not make any difference, but I would check that calling Debug.LogWarningMessage by moving it where Debug.LogDebugMessage should check if it really works.

According to extremely accurate documentation, there is a log file called output_log.txt , maybe there may be some information contained in this file.

+2
source

You tried:

 //button.eventClick += ButtonClick; button.eventClick += (c,e) => { 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(); }; 

Also did you try to do this ?:

 //var button = (UIButton)uiView.AddUIComponent(typeof(UIButton)); var button = uiView.AddUIComponent(typeof(UIButton)) as UIButton 

(Sorry, I still can not come up)

+1
source

It looks like your click handler, as such, is fine. Change the ButtonClick method to:

 public void ButtonClick(UIComponent component, UIMouseEventParameter eventParam) { Debug.LogWarningMessage("HIGH LOGIC: Beginning report generation."); } 

and then check the debug output. If you have an error after this line, the debug message may not be displayed.

+1
source

All Articles