You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Terraria模组开发:UI居中与游戏交互禁用问题求助

Terraria模组UI开发问题

我正在开发Terraria模组,需要实现一个主UI,要求启用该UI时禁用游戏其余所有键鼠交互(仅允许与该UI交互),且UI保持居中显示(边距20f)。但目前遇到两个问题:

  • 启用UI后仍可移动角色、操作物品栏,物品栏快捷栏未禁用
  • 使用Main.screenWidth和Main.screenHeight设置边距时,右侧和底部边距偏大,无法真正居中

当前UI效果:
实际UI效果

现有代码

QuestUI.cs

namespace TMWQuests.UI;

public class QuestUI : UIState
{
    private UIPanel _panel;
    private const float Margin = 20f;
    
    public override void OnInitialize()
    {
        // UI size
        var width = Main.screenWidth - Margin * 2f;
        var height = Main.screenHeight  - Margin * 2f;
        
        _panel = new UIPanel();
        _panel.SetPadding(0f);
        
        // Margin position
        _panel.Left.Set((Main.screenWidth - width) / 2f, 0f);
        _panel.Top.Set((Main.screenHeight - height) / 2f, 0f);
        
        _panel.Width.Set(width, 0f);
        _panel.Height.Set(height, 0f);
        
        _panel.BackgroundColor = new Color(127 / 255f, 127 / 255f, 127 / 255f, 127 / 255f);
        
        Append(_panel);

        _panel.OnMouseOver += (e, element) =>
        {
            Main.player[Main.myPlayer].mouseInterface = true;
        };
    }

    public override void Update(GameTime gameTime)
    {
        Main.player[Main.myPlayer].mouseInterface = true;
        base.Update(gameTime);
    }

    public override void OnDeactivate()
    {
        Main.player[Main.myPlayer].mouseInterface = false;
        base.OnDeactivate();
    }
}

TMWQuests.cs

namespace TMWQuests
{
    public class TMWQuests : Mod
    {
        public override void Load()
        {
            if (!Main.dedServ)
            {
                ModContent.GetInstance<TMWQuestsSystem>();
                ModContent.GetInstance<QuestsHoyKey>();
            }
        }

        public override void Unload()
        {
            TMWQuestsSystem._ui = null;
        }
    }
}

解决方案

1. 修复键鼠交互未完全禁用问题

仅设置mouseInterface只能处理鼠标交互,要彻底禁用游戏其余操作,需在UI激活时拦截所有输入并隐藏游戏原生UI:

修改QuestUI的Update和OnDeactivate方法:

public override void Update(GameTime gameTime)
{
    Main.LocalPlayer.mouseInterface = true;
    // 禁用玩家移动操作
    Main.LocalPlayer.controlLeft = Main.LocalPlayer.controlRight = false;
    Main.LocalPlayer.controlUp = Main.LocalPlayer.controlDown = false;
    // 禁用物品/方块使用操作
    Main.LocalPlayer.controlUseItem = Main.LocalPlayer.controlUseTile = false;
    // 隐藏物品栏与快捷栏
    Main.hidePlayerCraftingMenu = true;
    Main.HidePlayerHotbar();
    // 全局阻止所有游戏输入
    Main.blockInput = true;
    
    base.Update(gameTime);
}

public override void OnDeactivate()
{
    Main.LocalPlayer.mouseInterface = false;
    // 恢复所有输入状态
    Main.blockInput = false;
    Main.hidePlayerCraftingMenu = false;
    Main.ShowPlayerHotbar();
    
    base.OnDeactivate();
}

2. 修复UI居中异常问题

Main.screenWidth/Height是屏幕原始分辨率,未考虑Terraria的UI缩放设置。正确做法是使用Main.UIWidth/Height,这两个值已适配缩放后的实际UI显示区域:

修改QuestUI的OnInitialize方法:

public override void OnInitialize()
{
    // 使用适配缩放的UI尺寸计算
    var width = Main.UIWidth - Margin * 2f;
    var height = Main.UIHeight - Margin * 2f;
    
    _panel = new UIPanel();
    _panel.SetPadding(0f);
    
    // 基于缩放后尺寸居中定位
    _panel.Left.Set((Main.UIWidth - width) / 2f, 0f);
    _panel.Top.Set((Main.UIHeight - height) / 2f, 0f);
    
    _panel.Width.Set(width, 0f);
    _panel.Height.Set(height, 0f);
    
    _panel.BackgroundColor = new Color(127 / 255f, 127 / 255f, 127 / 255f, 127 / 255f);
    
    Append(_panel);

    _panel.OnMouseOver += (e, element) =>
    {
        Main.LocalPlayer.mouseInterface = true;
    };
}

内容的提问来源于stack exchange,提问作者GreenLass

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 03:44:54