// 获取对象当前所站立的层值,1、2、4、8、16 public static int GetAgentLayer(NavMeshAgent agent) { NavMeshHit hit; bool reach = NavMesh.SamplePosition(agent.transform.position, out hit, agent.radius, agent.walkableMask); return hit.mask; } // Nav层名字-->层索引(第几位),0、1、2、3、4 public static int AgentLayerNameToIndex(string name) { return NavMesh.GetNavMeshLayerFromName(name); } // Nav层名字-->层的值,1、2、4、8、16 public static int AgentLayerNameToValue(string name) { int idx = NavMesh.GetNavMeshLayerFromName(name); return (int)Mathf.Pow(2, idx); } // 检查对象某个层是否为开启 public static bool IsNavMeshLayerOpen(NavMeshAgent agent, string layerName) { int layerValue = NavMesh.GetNavMeshLayerFromName(layerName); if (layerValue == -1) return true; int ret = agent.walkableMask & (0x1 << layerValue); return ret > 0 ? true : false; } // 开启某导航层 public static void EnableNavMeshLayer(NavMeshAgent agent, string layerName) { if (agent == null) return; int layerValue = NavMesh.GetNavMeshLayerFromName(layerName); if (layerValue == -1) return; int mask = agent.walkableMask | 0x1 << layerValue; agent.walkableMask = mask;; } // 关闭某导航层 public static void DisableNavMeshLayer(NavMeshAgent agent, string layerName) { if (agent == null) return; int layerValue = NavMesh.GetNavMeshLayerFromName(layerName); if (layerValue == -1) return; int mask = agent.walkableMask & ~(0x1 << layerValue); agent.walkableMask = mask;; }
时间: 2024-10-20 18:29:56