实现
记录鼠标点击位置为线段起点。
posStart = Input.mousePosition;
创建一个Image 作为线段。
line = new GameObject("line");
rtLine = line.AddComponent<RectTransform>();
rtLine.pivot = new Vector2(0, 0.5f);
rtLine.localScale = Vector3.one;
imgLine = line.AddComponent<Image>();
imgLine.color = colorLine;
imgLine.raycastTarget = false;
rtLine.SetParent(canvas.transform);
rtLine.position = posStart;
rtLine.sizeDelta = Vector2.zero;
以鼠标当前位置为线段终点,在Update 中实时计算线段的角度和长度。
posEnd = Input.mousePosition;
posDuration = posEnd - posStart;
rtLine.sizeDelta = new Vector2(posDuration.magnitude, widthLine);
angle = Mathf.Atan2(posDuration.y, posDuration.x) * Mathf.Rad2Deg;
rtLine.localRotation = Quaternion.Euler(0, 0, angle);