1 . 我是一名程序新手, 第一次写博客,写的不好,大神们嘴下留情,加油
思路:
1.用一个Image进行拉伸从而形成一根线,改变线的长度与 起始连线对象到鼠标位置的距离。
2. 用mathf.Atan2 求出起点和终点的 弧度,最终在转化成角度乘以mathy.Rad2Deg.
3. 废话不多说 直接贴代码,写的不好的地方 请多提建议!!!!!!!!!!!!!!!!!!!!!!!!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System;
public class UI_Thread : MonoBehaviour, IPointerDownHandler, IDragHandler, IPointerEnterHandler, IEndDragHandler
{
private RectTransform rect;
private Vector3 vec3;
private Vector2 screen;
private Vector3 mousePos;
private GameObject game;
private Image m_Thread;
private float angle;
private Vector2 sizedata;
public Color ThreadColor = Color.black;
private Transform target;
private Action endAction;
public void SetThread(Transform target, Action endAction)
{
this.endAction = endAction;
this.target = target;
}
public void OnPointerEnter(PointerEventData eventData)
{
InIt();
}
public void OnDrag(PointerEventData eventData)
{
SetThreadData(Input.mousePosition);
}
public void OnPointerDown(PointerEventData eventData)
{
RemodThreadEvent(false);
Creat_Thread();
SetThreadData(Input.mousePosition);
}
public void OnEndDrag(PointerEventData eventData)
{
if (eventData.pointerEnter == null)
{
RemodThreadEvent(true);
DestroyImmediate(game);
}
else
{
SetThreadData(eventData.pointerEnter.transform.localPosition, true);
if (eventData.pointerEnter.name == target.name)
{
RemodThreadEvent(false);
//检测正确 线 颜色变成绿色
SetThreadColor(Color.green);
if (endAction != null) endAction();
}
else
{
// 线变成红色
SetThreadColor(Color.red);
RemodThreadEvent(false);
}
}
}
void SetThreadColor(Color color)
{
m_Thread.color = color;
}
void RemodThreadEvent(bool isOpenEvent)
{
GetComponent<Image>().raycastTarget = isOpenEvent;
}
void Creat_Thread()
{
if (game != null) DestroyImmediate(game.gameObject);
game = new GameObject("xian");
game.transform.SetParent(transform.parent);
game.transform.SetAsFirstSibling();
m_Thread = game.AddComponent<Image>();
m_Thread.color = ThreadColor;
m_Thread.raycastTarget = false;
rect = m_Thread.GetComponent<RectTransform>();
rect.pivot = new Vector2(0, 0.5f);
rect.anchoredPosition = transform.localPosition; ;
}
void SetThreadData(Vector3 endPos, bool isDragEnd = false)
{
if (!isDragEnd)
{
mousePos = endPos;
vec3 = new Vector3(mousePos.x - screen.x, mousePos.y - screen.y, 0) - transform.localPosition;
}
else
{
vec3 = endPos - transform.localPosition;
}
sizedata = new Vector2(vec3.magnitude, 5f);
rect.sizeDelta = sizedata;
angle = Mathf.Atan2(vec3.y, vec3.x);
rect.localRotation = Quaternion.Euler(0, 0, angle * Mathf.Rad2Deg);
}
void InIt()
{
screen = new Vector2(Screen.width / 2, Screen.height / 2);
}
}
这是在其他脚本调用的代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class Add_UI_Thread : MonoBehaviour
{
public Transform taget;
// Use this for initialization
void Start()
{
AddThread(transform, taget, delegate
{
Debug.Log(“检测正确”);
});
}
/// <summary>
///
/// </summary>
/// <param name="currUI">起始UI</param>
/// <param name="targetUI">目标UI</param>
/// <param name="endAction">检测结束后要的执行方法</param>
void AddThread(Transform currUI, Transform targetUI, Action endAction)
{
UI_Thread uI_Thread = currUI.gameObject.AddComponent<UI_Thread>();
uI_Thread.SetThread(targetUI, endAction);
}
}
f