「Basic Csharp Mod」修訂間的差異
小 (已保护“Basic Csharp Mod”([编辑=仅允许管理员](无限期)[移动=仅允许管理员](无限期))) |
小 (→编程) |
||
(未顯示同一使用者於中間所作的 1 次修訂) | |||
行 66: | 行 66: | ||
using TaleWorlds.MountAndBlade; | using TaleWorlds.MountAndBlade; | ||
3. 继承 MBSubModuleBase 类。<br> | 3. 继承 MBSubModuleBase 类。<br> | ||
− | 4. | + | 4. 复写 OnSubModuleLoad() 继承方法。<br> |
− | 5. 添加如下代码到你的 | + | 5. 添加如下代码到你的 复写 方法中:[https://docs.bannerlordmodding.com/_tutorials/basic-csharp-mod#programming 源链接]<br> |
+ | <nowiki> | ||
+ | Module.CurrentModule.AddInitialStateOption(new InitialStateOption("Message", | ||
+ | new TextObject("Message", null), | ||
+ | 9990, | ||
+ | () => { InformationManager.DisplayMessage(new InformationMessage("Hello World!")); }, | ||
+ | false)); | ||
+ | </nowiki> | ||
6. 编译你的项目并确定文件被输出到了 Modules\ExampleMod\bin\Win64_Shipping_Client。<br> | 6. 编译你的项目并确定文件被输出到了 Modules\ExampleMod\bin\Win64_Shipping_Client。<br> | ||
7. 打开游戏启动器转到 Singleplayer > Mods 确定你的模组被勾选了并启动。<br> | 7. 打开游戏启动器转到 Singleplayer > Mods 确定你的模组被勾选了并启动。<br> |
於 2020年4月20日 (一) 13:28 的最新修訂
介紹
這個教程接下來將一步一步教你創建一個基於 C# 的模組,這個模組將添加一個叫 Message 的按鈕到單人遊戲主界面,在點擊這個按鈕後將輸出 Hello World。
準備工作
本教程中,我們將這個項目命名為 ExampleMod。
創建模組(SubModule.xml)
1. 前往遊戲文件下的 Modules 目錄。
2. 創建一個新的文件夾並命名為 ExampleMod(必須跟你第4步使用的id相同)。
3. 創建一個新的文件夾並命名為 bin 然後在其中新建一個子文件夾並命名為 Win64_Shipping_Client。
4. 在你第2步創建的目錄下新建一個文件 SubModule.xml(必須是這個文件名)並粘貼該代碼:源連結
<Module> <Name value="Example Mod"/> <Id value="ExampleMod"/> <Version value="v1.0.0"/> <SingleplayerModule value="true"/> <MultiplayerModule value="false"/> <DependedModules> <DependedModule Id="Native"/> <DependedModule Id="SandBoxCore"/> <DependedModule Id="Sandbox"/> <DependedModule Id="CustomBattle"/> <DependedModule Id="StoryMode" /> </DependedModules> <SubModules> <SubModule> <Name value="ExampleMod"/> <DLLName value="ExampleMod.dll"/> <SubModuleClassType value="ExampleMod.MySubModule"/> <Tags> <Tag key="DedicatedServerType" value="none" /> <Tag key="IsNoRenderModeElement" value="false" /> </Tags> </SubModule> </SubModules> <Xmls/> </Module>
注意:MySubModule 是在編程部分會使用的類的名字。
5. 如果你使用了不同的名字,請修改以上文件並匹配你的 Module/SubModule 的值。
6. 打開遊戲啟動器並確認你的模組名字出現在了 Singleplayer > Mods 中。
查看更多有關目錄的信息,請訪問目錄結構。
創建項目
在創建項目之前,要告知你這裏對於基礎模組(如物品/場景/文化等)而言是不需要的。
- 啟動 Microsoft Visual Studio 並點擊 新建項目(Create New Project)。
- 選擇 Class Library (.NET Framework)。
- 將你的項目命名為 ExampleMod(如果你用了其它名字請確保命名空間和程序集名稱是正確的)並使用.NET Framework 4.7.2 作為 Framework。如果沒有這個選項,在這裏下載(開發包)。
- 建好項目後,設置你的編譯路徑為遊戲目錄下的 Modules/ExampleMod/bin/Win64_Shipping_Client。
- 引用 在你遊戲文件目錄(不是你的模組目錄)下 bin\Win64_Shipping_Client 中的 TaleWorlds.*(DLLs),並且引用每個官方模組目錄 Modules\ModuleName\bin\Win64_Shipping_Client 下的 TaleWorlds.* DLLs。
調試項目(可選)
- 打開項目屬性並轉至 Debug 標籤頁。
- 選擇 Start external program 選項並瀏覽至遊戲文件(不是你的模組)下的目錄 bin\Win64_Shipping_Client 中的 Bannerlord.exe。
- 設置工作目錄為遊戲文件(不是你的模組)下的目錄 bin\Win64_Shipping_Client。
- 添加以下命令行參數(確定把 ExampleMod 換成了你的模組的名字)
- /singleplayer _MODULES_*Native*SandBoxCore*CustomBattle*SandBox*StoryMode*ExampleMod*_MODULES_
編程
1. 在你的 VS 項目中新建一個類並命名為 MySubModule,然後打開它。
2. 將以下指令添加到你的類中:
using TaleWorlds.Core; using TaleWorlds.Localization; using TaleWorlds.MountAndBlade;
3. 繼承 MBSubModuleBase 類。
4. 複寫 OnSubModuleLoad() 繼承方法。
5. 添加如下代碼到你的複寫方法中:源連結
Module.CurrentModule.AddInitialStateOption(new InitialStateOption("Message", new TextObject("Message", null), 9990, () => { InformationManager.DisplayMessage(new InformationMessage("Hello World!")); }, false));
6. 編譯你的項目並確定文件被輸出到了 Modules\ExampleMod\bin\Win64_Shipping_Client。
7. 打開遊戲啟動器轉到 Singleplayer > Mods 確定你的模組被勾選了並啟動。
8. 在主界面中,你應該可以看到一個 Message 按鈕,單擊後你會在左下角聊天框看見輸出了 Hello World。
9. 你成功地創建了你的第一個遊戲模組!