unity中UI连线

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

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值