码迷,mamicode.com
首页 > 其他好文 > 详细

NavMesh名字、层索引、层值之间的转换

时间:2015-04-03 22:18:40      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:

    // 获取对象当前所站立的层值,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;;
    }

 

NavMesh名字、层索引、层值之间的转换

标签:

原文地址:http://www.cnblogs.com/sifenkesi/p/4391074.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!