废话
如果你的mod有一些全局变量供用户控制,你需要为你的Mod编写一个设置选项面板。
过程非常简单,只需要继承两个类并重写核心方法,分别是Mod和ModSettings。ModSettings负责储存数据,Mod负责绘制UI。
核心内容
1.Mod的UI编写
1.1 Model
MVC中的M层 储存数据相关
using Verse;
namespace test
{
public class TestSetting:ModSettings
{
#region param
public bool testBool = false;
public float testFloat = 1f;
public int testInt = 2500;
public UnityEngine.Vector2 scrollPos = UnityEngine.Vector2.zero;
#endregion
/// <summary>
/// 反序列化
/// </summary>
public override void ExposeData()
{
base.ExposeData();
Scribe_Values.Look(ref testBool, "TestBool", false);
Scribe_Values.Look(ref testFloat, "TestFloat", 1f);
Scribe_Values.Look(ref testInt, "TestInt", 2500);
}
/// <summary>
/// 初始化数据
/// </summary>
public void InitData()
{
testBool = false;
testFloat = 1f;
testInt = 2500;
}
}
}
1.2 Controller+View
MVC中的V+C层 视图以及控制逻辑放在这里编写
using UnityEngine;
using Verse;
namespace test
{
//游戏加载时构造实例
[StaticConstructorOnStartup]
public class TestMain : Mod
{
public TestSetting testSetting;
public static TestMain Instance { get; private set; }
public TestMain(ModContentPack content) : base(content)
{
testSetting = GetSettings<TestSetting>();//读取本地数据 设置setting中的mod关联
Instance = this;
}
/// <summary>
/// mod选项面板中的名字
/// </summary>
/// <returns></returns>
public override string SettingsCategory()
{
return "TestCategory";
}
/// <summary>
/// 绘制方法
/// </summary>
/// <param name="inRect"></param>
public override void DoSettingsWindowContents(Rect inRect)
{
Rect rect = new Rect(0, 0, 0.9f * inRect.width, 0.8f * inRect.height);
Listing_Standard ls = new Listing_Standard();
ls.BeginScrollView(inRect, ref testSetting.scrollPos, ref rect);
if (ls.ButtonText("恢复默认".Translate())) { testSetting.InitData(); }//按钮与监听
ls.GapLine(20f);
ls.Label("布尔测试".Translate());
ls.CheckboxLabeled("布尔测试", ref testSetting.testBool, "This is a test bool");
ls.GapLine(20f);
Text.Font = GameFont.Medium;
ls.Label("浮点测试".Translate());
ls.Label(testSetting.testFloat.ToString());
testSetting.testFloat = ls.Slider(testSetting.testFloat, 100f, 300f);
Text.Font = GameFont.Small;
ls.GapLine(20f);
Text.Font = GameFont.Medium;
ls.Label("整型测试".Translate());
ls.Gap(10f);
Text.Font = GameFont.Small;
TextFieldNumericLabeled(ls, "整型测试",ref testSetting.testInt,0f,30f);
ls.EndScrollView(ref rect);
}
private void TextFieldNumericLabeled<T>(Listing_Standard ls,string label, ref T val, float min, float max) where T : struct {
string s = val.ToString();
ls.TextFieldNumericLabeled(label,ref val,ref s, min, max);
}
}
}
1.3 测试
如果这篇文章对你有帮助,点赞收藏支持一下呗!