Exemple #1
0
    // Update is called once per frame
    void Update()
    {
        Collider[] cList = Physics.OverlapBox(boy.position, boy.GetComponent <BoxCollider>().bounds.extents, Quaternion.identity, LayerMask.GetMask(LayerName.NavMesh));
        //判断是否在navmesh中
        if (cList != null && cList.Length != 0)
        {
            inNavmesh = true;
            mb        = cList[0].transform.parent.GetComponentInChildren <MeshBoard>();
        }
        else
        {
            inNavmesh = false;
            mb        = null;
        }

        if (inNavmesh)
        {
            //寻路
            a = boy.transform.position + Vector3.up * 0.5f;
            b = cat.transform.position + Vector3.up * 0.3f;
            mb.Init();
            pathList = mb.FindPath(a, b);

            //计算方向:上下左右
            direction = pathList[1] - pathList[0];
        }
    }
Exemple #2
0
    void OnSceneGUI()
    {
        int controlID = GUIUtility.GetControlID(FocusType.Passive);

        HandleUtility.AddDefaultControl(controlID);


        Event e = Event.current;

        //if (e.type == EventType.mouseUp && e.button == 0)
        //{
        //    if (e.control)
        //    {
        //        Debug.Log(1);
        //    }
        //}
        //if (e.control && e.type == EventType.keyUp && e.keyCode == KeyCode.Q)
        //{
        //    Debug.Log(2);
        //}

        //if (e.type == EventType.mouseUp && e.shift && e.button == 1)
        //{
        //    Debug.Log("redraw");
        //}

        mb.Init();

        //增加顶点(必须先取到所有顶点)
        if (isEditing && e.type == EventType.mouseDown && e.button == 0)
        {
            Ray     r      = UnityEditor.HandleUtility.GUIPointToWorldRay(e.mousePosition);
            Vector3 posRes = new Vector3(r.origin.x, r.origin.y, 0);

            posRes = GetOverlapPoint(posRes);


            //设置当前点
            if (e.control || cur == null)
            {
                cur = new GameObject().transform;
                cur.transform.parent = mb.transform;
                cur.name             = "MeshChild";
            }
            //新建顶点
            GameObject obj = new GameObject();
            obj.name = "p" + cur.transform.childCount;
            obj.transform.position = posRes;
            obj.transform.parent   = cur;
        }

        //调整位置
        if (isEditing == false && e.control && e.type == EventType.mouseDown && e.button == 0)
        {
            //Debug.Log("选中");
            Ray     r       = HandleUtility.GUIPointToWorldRay(e.mousePosition);
            Vector3 posRes  = new Vector3(r.origin.x, r.origin.y, 0);
            Vector3 overPos = GetOverlapPoint(posRes);
            hd = overPos;
            if (overPos == posRes)
            {
                isHandle      = false;
                Tools.current = Tool.None;
            }
            else
            {
                isHandle = true;
            }
        }
        if (isHandle)
        {
            Vector3           diff    = Handles.PositionHandle(hd, Quaternion.identity) - hd;
            List <GameObject> objList = GetOverlapPoints(hd);
            hd += diff;
            AdjustObjsPosition(objList, diff);
        }

        //绘制网格
        RedrawNavMesh();

        //if (isEditing)
        //{
        //    Tools.current = Tool.None;
        //}

        //取消选中锁定
        //if (!isEditing)
        //{
        //    if (e.type == EventType.mouseUp && e.button == 0)
        //    {
        //        Selection.activeObject = null;
        //    }
        //}

        HandleUtility.AddDefaultControl(-1);
        HandleUtility.Repaint();
    }