Art*_*ode 5 c# unity-game-engine cities-skylines-api
在开发城市模式:Skylines时,我遇到了一个问题.
据我所知,我的大多数代码都可以正常工作 - 但到目前为止,我还没有对它进行测试.这是因为当我添加到UI的按钮被点击时,它应该按顺序被调用.此按钮上的单击事件不会调用我已分配给它的处理程序.
这是我创建按钮的地方:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
根据我一直关注的文档,使用
button.eventClick += ButtonClick;
Run Code Online (Sandbox Code Playgroud)
应添加ButtonClick为按钮的单击处理程序.但是,单击按钮不会执行任何操作.处理程序开头的调试消息("HIGH LOGIC"消息)不会显示(注意:有关将按钮添加到UI的早期调试消息确实如此).游戏的调试面板或VS中不会显示任何错误消息.
我也尝试过使用new MouseEventHandler(ButtonClick),因为VS内联文档告诉我类型eventClick是MouseEventHandler.这不会在VS或游戏中显示任何错误,但也不起作用.
(注:还有就是 官方文档,但它的旁边没有用的.)
这里有没有人有C:S API的经验?为什么这个事件没有被调用?
| 归档时间: |
|
| 查看次数: |
304 次 |
| 最近记录: |