### 【模组详细说明与实操指南】
# LethalConfig <!-- omit in toc -->
LethalConfig 是一个模组配置菜单,允许玩家在游戏内编辑他们的配置。它还为开发人员提供了一个简单的 API 来自定义他们的 mod 和配置条目。
受到 Rune580 的 RiskOfOptions 的启发
### 摘要 <!-- 目录中省略 -->
- 支持的类型
- 用法
- 自动生成
- 设置
- 添加配置项
- ConfigItem CanModifyCallback
- 监听设置更改
- 通用按钮
- 问题和建议
- 学分
## 支持的类型
目前,LethalConfig 允许开发者为 ConfigEntry 添加以下类型的接口:
|组件|值类型 |配置项类型 |
| --------- | ---------- | ---------- |
|整数滑块 | `int` | `IntSliderConfigItem` |
|整数输入字段 | `int` | `IntInputFieldConfigItem` |
|浮动滑块| `浮动` | `FloatSliderConfigItem` |
|浮动步进滑块| `浮动` | `FloatStepSliderConfigItem` |
|浮点输入字段 | `浮动` | `FloatInputFieldConfigItem` |
|文本输入字段 | `字符串` | `TextInputFieldConfigItem` |
|十六进制颜色输入字段 | `字符串` | `HexColorInputFieldConfigItem` |
|枚举下拉菜单 | `枚举` | `EnumDropDownConfigItem<>` |
|布尔复选框 | `枚举` | `BoolCheckBoxConfigItem` |
|通用按钮 | `-` | `通用按钮配置项`
\
*LethalConfig 菜单及其元素类型的示例*
## 用法
### 自动生成
从 1.1.0 开始,LethalConfig 自动生成 mod 条目并检测所述 mod 声明的所有 ConfigEntry,并尽力生成正确的 UI 组件。
它将分配一个默认的 mod 图标,并在 mod 和配置的描述中包含有关自动生成的免责声明,并且它们可能需要重新启动,因为 LethalConfig 无法判断设置是否会立即生效。
除非您是 Mod 开发人员,想要自定义其 Mod 图标和描述、手动设置 UI 组件(例如使用滑块而不是数字文本字段)或将设置标记为不需要重新启动,否则除了安装 Mod 之外,您不需要执行任何其他操作。
某些类型可能还没有为其开发 UI 组件。在这些情况下,LethalConfig 将忽略 ConfigEntry。随着时间的推移,将会涵盖更多类型。
### 设置
要开始在您的 mod 上使用 LethalConfig,请将您项目的引用添加到 LethalConfig 的 dll 文件中。您可以通过在Thunderstore上下载LethalConfig mod来获取dll。
要访问 API,只需使用 `LethalConfig` 命名空间或将其导入到源文件中:
``csharp
使用 LethalConfig;
````
还建议在您的插件中添加一个 `BepInDependency` 属性,以提示 BepInEx 您的 mod 对其具有依赖关系:
```csharp
[BepInPlugin(PluginInfo.Guid, PluginInfo.Name, PluginInfo.Version)]
[BepInDependency("ainavt.lc.lethalconfig")]
public class MyVeryCoolPlugin: BaseUnityPlugin {
...
}
```
### 添加配置项
完成所有设置后,您现在应该可以访问主 `LethalConfigManager` 并能够添加您想要的任何可用组件。
首先,您像平常一样从 BepInEx 创建“ConfigEntry”:
```csharp
var configEntry = Config.Bind("General", "Example", 0, "This is an example component!");
```
有了“ConfigEntry”,您现在可以使用以下方法创建新项目并将其注册到菜单中:
```csharp
var exampleSlider = new IntSliderConfigItem(configEntry, new IntSliderOptions
{
Min = 0,
Max = 100
});
LethalConfigManager.AddConfigItem(exampleSlider);
```
就是这样,您现在已经创建了第一个组件!
!滑块示例
LethalConfig 会自动获取您的一些 mod 信息,并根据提供的 ConfigEntry 的部分自动创建部分,因此您不必担心布局方面的任何额外设置。
### ConfigItem 重启要求
默认情况下,所有项目都将设置为如果更改则需要重新启动。一旦玩家应用设置,就会向他们发出警告。
如果您希望您的项目不被标记为需要重新启动,只需通过“bool”构造函数重载或将其传递到项目的特定选项中即可标记配置项目的构造函数:
```csharp
// Using the slider options object
var exampleSlider = new IntSliderConfigItem(configEntry, new IntSliderOptions
{
RequiresRestart = false,
Min = 0,
Max = 100
});
// 使用 bool 构造函数重载
var exampleSlider = new IntSliderConfigItem(configEntry, requireRestart: false);
````
### 配置项 CanModifyCallback
默认情况下,所有项目都可以在运行时不受限制地修改。这对于某些模组来说可能并不理想,因为它们可能需要有条件地禁止修改。
Mods可以使用配置项构造函数的CanModifyCallback。
```csharp
var exampleCheckbox = new BoolCheckBoxConfigItem(configEntry, new BaseOptions
{
CanModifyCallback = CheckboxCanModifyCallback
});
// 每次配置条目显示在 UI 中时都会调用此函数。
私有静态 CanModifyResult CheckboxCanModifyCallback()
{
// 如果条目可以修改,则返回 true。
// 如果需要修改则返回 false。
// 当返回 false 时,您还应该包含一条说明原因的消息
// 配置条目不能修改。
return (false, "示例原因");
// 你也可以只返回一个bool,或者CanModifyResult.True()/CanModifyResult.False(reason)
}
````
### 禁用/跳过自动生成
您可以跳过每个 ConfigEntry、Config 部分或整个 mod 的自动生成。
```csharp
var configEntry = Config.Bind("General", "Example", 0, "This is an example component!");
var skippedConfigEntry = Config.Bind("Skip This Section", "Example 2", 0, "This is an example component!");
// 跳过 ConfigEntry 的自动生成。
LethalConfigManager.SkipAutoGenFor(configEntry);
// 跳过配置部分“跳过此部分”的自动生成。
LethalConfigManager.SkipAutoGenFor("跳过本节");
// 跳过调用 mod 的自动生成。
LethalConfigManager.SkipAutoGen();
````
### Listening to setting changes
请注意,如果没有提示重新启动,玩家很可能会期望设置立即生效。如果您需要监听配置值的更改,“ConfigEntry”为此提供了一种机制:
```csharp
configEntry.SettingChanged += (obj, args) =>
{
logSource.LogInfo($"Slider value changed to {configEntry.Value}");
};
```
### 通用按钮
如果您想创建一个按钮项以在单击时运行代码,则可以使用“GenericButtonConfigItem”。您可以使用它来打开 mod 的自定义菜单或通过回调运行一些逻辑。
该项目不与任何“ConfigEntry”绑定,因此您需要传递该项目的部分、名称和描述。
```csharp
LethalConfigManager.AddConfigItem(new GenericButtonConfigItem("Section", "ItemName", "Description", "ButtonText", () =>
{
// Code you want to run when the button is clicked goes here.
}));
```
### Overriding display properties of items
有时,您可能希望以不同的名称或不同的描述显示不同类别中的配置,而不更改底层的“ConfigEntry”实例。
LethalConfig 允许您直观地修改这些属性,以便它显示您想要的任何名称、部分和描述。
```csharp
var boolCheckBox = Config.Bind("Example", "Bool Checkbox", false, "This is a bool checkbox.");
var configItem = new BoolCheckBoxConfigItem(boolCheckBox, new BoolCheckBoxOptions
{
Section = "General",
Name = "Enable something",
Description = "Does something interesting"
});
```
In the above example, the item will be displayed under the "General" section, with the "Enable something" name, and mousing over the item will show the new description. These are all visual changes, and the underlying entry is unnafected, making it possible to adjust names without having BepInEx create an entire new config for you.
### Customizing mod's icon and description
LethalConfig 会自动尝试从 Thunderstore 清单中加载 mod 的图标和描述,但也允许您覆盖它们。
To override your mod's icon in its entry, simply call `LethalConfigManager.SetModIcon` passing an instance of `UnityEngine.Sprite`. A sprite can be loaded from an AssetBundle.
```csharp
var aVeryCoolIconAsset = assetBundle.LoadAsset<Sprite>("path/to/asset.png")
LethalConfigManager.SetModIcon(aVeryCoolIconAsset);
```
To override your mod's description, simply call `LethalConfigManager.SetModDescription` passing a string.
```csharp
LethalConfigManager.SetModDescription("Very cool mod description!");
```
## Building LethalConfig
If you want to contribute and/or need to build LethalConfig yourself, just open the project and import Lethal Company using the included ThunderKit. To do so, follow these steps to setup your project:
- Clone the repository and open the project with Unity `2022.3.9f1`.
- If Unity asks if you want to open in Safe Mode at any point, click to ignore.
- On the ThunderKit window that opened, go to `ThunderKit Settings`, set your game path and executable to your Lethal Company folder and exe file.
- If the ThunderKit window didn't automatically open, you can find it on `Tools > ThunderKit > Settings`.
- ThunderKit will prompt you to restart the project a couple of times. After the restarts, the project should be setup!
With the project setup, simply select the `Publish` pipeline and the `LethalConfigManifest` at the top of the project, then click `execute`. After it's done, it should generate a zip file under the `ThunderKit/Staging` folder containing the mod.
## Issues and Suggestions
If you have an issue with LethalConfig, or want to suggest a feature, feel free to open an issue at the GitHub repository.
Alternatively, you can also leave your suggestions and issues on the LethalConfig post under the Mod Releases forum in the Unofficial Lethal Company Discord Server.
## Credits
- Search icon - <a href="https://www.flaticon.com/free-icons/discover" title="discover icons">Discover icons created by Smashicons - Flaticon</a>
### 【联机规则与注意事项】
* **联机兼容性**:【开黑全员需装】(涉及联机机制、地图或实体同步,建议房间内所有开黑好友安装同版本)
* **前置依赖需求**:BepInEx-BepInExPack-5.4.2100
* **当前模组版本**:`1.4.6`
* **适配版本**:针对《致命公司》2026 最新稳定版(v68+)整理适配