[英]Why does this added UI button's event not get called?
在開發城市模式: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();
}
}
根據我一直關注的文檔 ,使用
button.eventClick += ButtonClick;
應該添加ButtonClick
作為按鈕的單擊處理程序。 但是,單擊按鈕不會執行任何操作。 處理程序開頭的調試消息(“HIGH LOGIC”消息)不會顯示(注意:有關將按鈕添加到UI的早期調試消息確實如此)。 游戲的調試面板或VS中不會顯示任何錯誤消息。
我也嘗試使用new MouseEventHandler(ButtonClick)
,因為VS內聯文檔告訴我eventClick
的類型是MouseEventHandler
。 這不會在VS或游戲中顯示任何錯誤,但也不起作用。
(注: 有 正式文件 ,但它的旁邊沒有用的。)
這里有沒有人有C:S API的經驗? 為什么這個事件沒有被調用?
您可以嘗試向前退一步,檢查是否有不同的UI元素; 例如,通過遵循添加UILabel的示例。 將click事件處理程序注冊到這個可能可能有效,因為有一個實際的例子可以遵循; 雖然,文檔說要將此代碼放在Start方法中。 我不確定,但也許代碼中的UIButton也應放在Start方法中。
順便說一句,我偶然發現了這個鏈接進行調試 。 Skylines似乎有自己的消息傳遞系統進行調試。
我在代碼中注意到的一點是,第一個Debug語句使用的方法與第二個Debug語句不同:
它可能沒有任何區別,但我會測試Debug.LogWarningMessage調用,將它移動到Debug.LogDebugMessage的位置以查看它是否真的有效。
根據非常簡潔的文檔,有一個名為output_log.txt的日志文件,也許這個文件中可能包含一些信息。
你有沒有嘗試過:
//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();
};
還試過這個嗎?:
//var button = (UIButton)uiView.AddUIComponent(typeof(UIButton));
var button = uiView.AddUIComponent(typeof(UIButton)) as UIButton
(對不起,我還不能服用)
看起來你的點擊處理程序就好了。 將ButtonClick方法更改為:
public void ButtonClick(UIComponent component, UIMouseEventParameter eventParam)
{
Debug.LogWarningMessage("HIGH LOGIC: Beginning report generation.");
}
然后檢查調試輸出。 如果在該行之后有錯誤,則調試消息可能不可見。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.