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));
}
}
}
推荐阅读
- 热闹中的孤独
- Docker应用:容器间通信与Mariadb数据库主从复制
- 一个人的碎碎念
- JS中的各种宽高度定义及其应用
- 我从来不做坏事
- 我眼中的佛系经纪人
- 《魔法科高中的劣等生》第26卷(Invasion篇)发售
- Android中的AES加密-下
- 从蓦然回首到花开在眼前,都是为了更好的明天。
- 放下心中的偶像包袱吧