本文主要是介绍unity UGUI画线(Image方式),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
/// <summary>/// 画线/// 注意:startPoint和endPoint,须和lineTransform在同一路径层级下的位置关系!!!/// </summary>/// <param name="self"></param>/// <param name="lineTransform">LineImg RectTransform <param>/// <param name="startPoint"></param>/// <param name="endPoint"></param>/// <param name="lineColor"></param>/// <param name="lineWidth"></param>private void DrawLine(RectTransform lineTransform, Vector2 startPoint, Vector2 endPoint, Color lineColor, float lineWidth = 5f){Log.Debug($"startPoint.x:{startPoint.x}; startPoint.y:{startPoint.y}; endPoint.x:{endPoint.x}; endPoint.y:{endPoint.y}");// 计算线条的方向和长度Vector2 direction = endPoint - startPoint;float length = direction.magnitude;// 如果长度为0,则不绘制线条if (length == 0) return;// 标准化方向向量Vector2 normalizedDirection = direction.normalized;// 设置线条的RectTransform属性float angle = Mathf.Atan2(normalizedDirection.y, normalizedDirection.x) * Mathf.Rad2Deg;lineTransform.localRotation = Quaternion.AngleAxis(angle, Vector3.forward);lineTransform.sizeDelta = new Vector2(length, lineWidth);lineTransform.anchoredPosition = startPoint + normalizedDirection * length * 0.5f; // 将线条中心放在起始点// 设置线条颜色lineTransform.gameObject.GetComponent<Image>().color = lineColor;}
这篇关于unity UGUI画线(Image方式)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!