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

Unity编辑器脚本修改材质不实时生效,仅悬停视图后更新

编辑模式下材质参数实时更新延迟问题

我编写了一个可在编辑模式下调整材质颜色与参数的脚本,核心逻辑在OnGUI方法中,代码结构如下:

private void OnGUI()
{
    if (basicTextilePreset.Count != 5)
    {
        LoadBasic();
    }

    if (material != null)
    {
        if (material != _nowMaterial)
        {
            SaveOriginalValues();
            _nowMaterial = material;
            _nowTextileType = TextileType;

            Select();
        }
        if (material == _nowMaterial)
        {
            if (TextileType != _nowTextileType)
            {
                _nowTextileType = TextileType;
                Select();
            }
            else
            {
                ChangeMaterialDynamically();
            }
        }
    }
    if (material == null && _nowMaterial != null)
    {
        _nowMaterial = null;
        originalTextilePreset = null;
    }
}

组件Inspector界面截图

该脚本通过OnGUI调用ChangeMaterialDynamically方法调整材质参数,预期调整Inspector参数后材质能实时更新,但实际只有将鼠标悬停到Scene视图或Game视图(双视图同时显示时)才会触发更新;单视图显示时,更新有时延迟数秒甚至完全不生效。

脚本已开启Execute In Edit Mode,我尝试添加以下强制刷新代码到OnInspectorGUI方法中,但问题依旧,更新延迟或需悬停视图才生效的情况没有改善:

public override void OnInspectorGUI()
{
    base.OnInspectorGUI();

    if (GUI.changed)
    {
        UnityEditorInternal.InternalEditorUtility.RepaintAllViews();
        EditorApplication.QueuePlayerLoopUpdate();
        EditorApplication.update();
        EditorApplication.RepaintProjectWindow();
        EditorApplication.RepaintHierarchyWindow();
        EditorApplication.RepaintAnimationWindow();
        SceneView.RepaintAll();
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 14:47:07