UGUI中的从一点拉出直线的效果

UGUI中的从一点拉出直线的效果
文章图片
image.png 【UGUI中的从一点拉出直线的效果】从image2拉出一条直线

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using Vectrosity; public class lineTOpoint : MonoBehaviour {public GameObject point1, point2; public RectTransform line; void Start() { Vector3 d; if (point2.transform.localPosition.x >= point1.transform.localPosition.x) { d = point2.transform.localPosition - point1.transform.localPosition; } else { d = point1.transform.localPosition - point2.transform.localPosition; }Debug.LogError(transform.GetComponentsInChildren().Length); Debug.LogError("Quaternion.Euler=" + Quaternion.Euler(d)); line.transform.localRotation = Quaternion.Euler(0, 0, -getDir(point1.transform.localPosition, point2.transform.localPosition)); line.transform.localPosition = midpoint(point1.transform.localPosition, point2.transform.localPosition); line.sizeDelta=new Vector2(221f, distance(point1.transform.localPosition, point2.transform.localPosition)); }bool isStar = false; public void ClickDown() { isStar = true; }public void ClickUp() { isStar = false; }float getDir(Vector3 p1, Vector3 p2) { float radian = 0; Vector3 vector; if (p2.x >= p1.x) { vector = p2 - p1; float dot = Vector3.Dot(vector.normalized, new Vector3(0, 1, 0).normalized); radian = Mathf.Acos(dot) * Mathf.Rad2Deg; } else { vector = p1 - p2; float dot = Vector3.Dot(vector.normalized, new Vector3(0, 1, 0).normalized); radian = Mathf.Acos(dot) * Mathf.Rad2Deg + 180; } return radian; }Vector3 midpoint(Vector3 p1, Vector3 p2) { return (p1+p2)/2; }float distance(Vector3 p1, Vector3 p2) { Vector3 d; d = p2 - p1; Debug.LogError("d.magnitude" + d.magnitude); return d.magnitude; }// Update is called once per frame void Update () { if (isStar) { var mousePos = transform.InverseTransformPoint(Input.mousePosition); line.transform.localRotation = Quaternion.Euler(0, 0, -getDir(point2.transform.localPosition, mousePos)); line.transform.localPosition = midpoint(point2.transform.localPosition, mousePos); line.sizeDelta = new Vector2(221f, distance(point2.transform.localPosition, mousePos)); } } }

    推荐阅读