通过脚本设置UnityNavigation里面Bake的相关参数

6 篇文章 0 订阅

很无语的是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;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值