通过脚本设置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;

猜你喜欢

转载自blog.csdn.net/alexhu2010q/article/details/107858457