Example #1
0
    static void NodeLerp()
    {
        if (lerpLength <= 0)
        {
            Open();
        }
        if (Selection.gameObjects.Length == 2)
        {
            Transform start = null;
            Transform end   = null;
            {
                NodeLink[] links = Selection.gameObjects[0].GetComponents <NodeLink>();
                for (int i = 0; i < links.Length; i++)
                {
                    if (links[i].End == Selection.gameObjects[1].transform)
                    {
                        start = Selection.gameObjects[0].transform;
                        end   = Selection.gameObjects[1].transform;
                        GameObject.DestroyImmediate(links[i]);
                    }
                }
            }
            {
                NodeLink[] links = Selection.gameObjects[1].GetComponents <NodeLink>();
                for (int i = 0; i < links.Length; i++)
                {
                    if (links[i].End == Selection.gameObjects[0].transform)
                    {
                        start = Selection.gameObjects[1].transform;
                        end   = Selection.gameObjects[0].transform;
                        GameObject.DestroyImmediate(links[i]);
                    }
                }
            }
            float length = Vector3.Distance(Selection.gameObjects[0].transform.position, Selection.gameObjects[1].transform.position);
            if (length > lerpLength)
            {
                float floatCount = length / lerpLength;
                int   count      = Mathf.FloorToInt(length / lerpLength);
                //有小数的话数量加一
                if (floatCount > count)
                {
                    count += 1;
                }
                ////起始点结束点已经有,要差值的点数量-1
                //count -= 1;

                if (count > 0)
                {
                    if (start == null || end == null)
                    {
                        start = Selection.gameObjects[0].transform;
                        end   = Selection.gameObjects[1].transform;
                    }

                    Delete(start, end);

                    NodeCell nodeCell = AddNodeCell(start.gameObject, end.gameObject);

                    Transform pre = start;
                    for (int i = 1; i < count; i++)
                    {
                        pre = AddLerpObj(start.transform, pre, Vector3.Lerp(start.position, end.position, (float)i / (float)count));
                        nodeCell.lst.Add(pre);
                    }

                    AddLink(end.gameObject, pre);
                }
            }
            AStarHelpEditor.NodeAttachFloor();
            UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene());
        }
    }