通过脚本设置UnityNavigation里面Bake的相关参数
很无语的是Unity连这个基本的API都没提供,下面代码是通过UnityEditor.AI里的NavMeshBuilder的navMeshSettingsObject来通过SerializedObject实现设置参数的,代码如下:
using UnityEditor;
using UnityObject = UnityEngine.Object;
namespace Impossible.Ete
{
public sealed class NavMeshSettings
{
private SerializedObject serializedObject;
private SerializedProperty _agentRadius;
private SerializedProperty _agentHeight;
private SerializedProperty _agentSlope;
private SerializedProperty _ledgeDropHeight;
private SerializedProperty _agentClimb;
private SerializedProperty _maxJumpAcrossDistance;
private SerializedProperty _minRegionArea;
private SerializedProperty _widthInaccuracy;
private SerializedProperty _heightInaccuracy;
private SerializedProperty _accuratePlacement;
public NavMeshSettings(UnityObject navMeshSettingsObject)
{
EnsureArg.IsNotNull(navMeshSettingsObject);
serializedObject = new SerializedObject(navMeshSettingsObject);
_agentRadius = serializedObject.FindProperty("m_BuildSettings.agentRadius");
_agentHeight = serializedObject.FindProperty("m_BuildSettings.agentHeight");
_agentSlope = serializedObject.FindProperty("m_BuildSettings.agentSlope");
_ledgeDropHeight = serializedObject.FindProperty("m_BuildSettings.ledgeDropHeight");
_agentClimb = serializedObject.FindProperty("m_BuildSettings.agentClimb");
_maxJumpAcrossDistance = serializedObject.FindProperty("m_BuildSettings.maxJumpAcrossDistance");
_minRegionArea = serializedObject.FindProperty("m_BuildSettings.minRegionArea");
_widthInaccuracy = serializedObject.FindProperty("m_BuildSettings.widthInaccuracy");
_heightInaccuracy = serializedObject.FindProperty("m_BuildSettings.heightInaccuracy");
_accuratePlacement = serializedObject.FindProperty("m_BuildSettings.accuratePlacement");
}
public float agentRadius
{
get => _agentRadius.floatValue;
set
{
_agentRadius.floatValue = value;
serializedObject.ApplyModifiedPropertiesWithoutUndo();
}
}
public float agentHeight
{
get => _agentHeight.floatValue;
set
{
_agentHeight.floatValue = value;
serializedObject.ApplyModifiedPropertiesWithoutUndo();
}
}
public float agentSlope
{
get => _agentSlope.floatValue;
set
{
_agentSlope.floatValue = value;
serializedObject.ApplyModifiedPropertiesWithoutUndo();
}
}
// "Drop Height"
public float ledgeDropHeight
{
get => _ledgeDropHeight.floatValue;
set
{
_ledgeDropHeight.floatValue = value;
serializedObject.ApplyModifiedPropertiesWithoutUndo();
}
}
// "Step Height"
public float agentClimb
{
get => _agentClimb.floatValue;
set
{
_agentClimb.floatValue = value;
serializedObject.ApplyModifiedPropertiesWithoutUndo();
}
}
public float maxJumpAcrossDistance
{
get => _maxJumpAcrossDistance.floatValue;
set
{
_maxJumpAcrossDistance.floatValue = value;
serializedObject.ApplyModifiedPropertiesWithoutUndo();
}
}
public float minRegionArea
{
get => _minRegionArea.floatValue;
set
{
_minRegionArea.floatValue = value;
serializedObject.ApplyModifiedPropertiesWithoutUndo();
}
}
public float widthInaccuracy
{
get => _widthInaccuracy.floatValue;
set
{
_widthInaccuracy.floatValue = value;
serializedObject.ApplyModifiedPropertiesWithoutUndo();
}
}
public float heightInaccuracy
{
get => _heightInaccuracy.floatValue;
set
{
_heightInaccuracy.floatValue = value;
serializedObject.ApplyModifiedPropertiesWithoutUndo();
}
}
// "Height Mesh"
public bool accuratePlacement
{
get => _accuratePlacement.boolValue;
set
{
_accuratePlacement.boolValue = value;
serializedObject.ApplyModifiedPropertiesWithoutUndo();
}
}
}
}
实际使用的时候这么写:
var settings = new NavMeshSettings(NavMeshBuilder.navMeshSettingsObject);
settings.agentRadius = 0.15f;
settings.agentHeight = 1.8f;
settings.agentSlope = 60f;
settings.agentClimb = 0.4f;
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本