Unity 两个UI(坐标)之间的连线(直线)。如连线题

应用场景:

屏幕上随机出现n对图片,使用鼠标点击图片进行匹配。鼠标点击第一个图片时,开始连线,起点为第一个图片的位置,终点为鼠标的位置。点击空白线条消失,第二次点击匹配失败线条消失,匹配成功线条消失。

//连线脚本
public class UIMatching : MonoBehaviour
{
    private Image line; //线条颜色材质。在这因挂载在了Image对象上,故取本身作为材质
    private Vector2 startPoint; //起点    线的起点坐标
    private Vector2 endPoint; //终点     线的终点坐标

    public float scaleSize;				//UI的适配机制,如果写得不对或者又跟好的方法,请指教

//设置起点坐标
    public void SetStartPoint(Vector2 newPoint)
    {
        startPoint = newPoint;
    }
//获取线条拉伸的方向(角度)
	public float GetAngle()
    {
        Vector2 dir = endPoint - startPoint;
        Vector2 dirV2 = new Vector2(dir.x, dir.y);
        float angle = Vector2.SignedAngle(dirV2, Vector2.down);
        return angle;
	 }
//获得适配参数
    public void GetScaleSize()
    {
        if (Screen.width / 1024f > Screen.height / 768f)
        {
            scaleSize = Screen.height / 768f;
        }
        else
        {
            scaleSize = Screen.width / 1024f;
        }

    }


    private void Start()
    {
        line = GetComponent<Image>(); //获得材质,故脚本挂载必须是含有Image组件的对象
        GetScaleSize();							//初始化缩放
    }

    // Update is called once per frame
    void Update()
    {
  
        endPoint = Input.mousePosition;			//设置线条终点一直为鼠标位置

        line.transform.Rotate(0, 0, GetAngle());		
        line.transform.localRotation = Quaternion.AngleAxis(-GetAngle(), Vector3.forward);
        float distance = Vector2.Distance(endPoint, startPoint);    


        line.rectTransform.sizeDelta = new Vector2(1, Mathf.Max(1*scaleSize, distance)); //distance -n    n代表实际线的起点距离startPoint值
        line.GetComponent<RectTransform>().anchoredPosition = Vector2.Lerp(startPoint, endPoint, 0.5f);
    }
    
}`

在控制脚本中对上面的对象进行调用实例
public class Ctrl : BaseMonoBehaviour
{
	public GameObject selected;//第一个被选中的对象
	public UIMatching _matching;
	void Update()
	{
		 if (EventSystem.current.IsPointerOverGameObject())
		 {
		 	if (Input.GetMouseButtonDown(0))
		    {
		    	PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current);
		        eventDataCurrentPosition.position = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
		        List<RaycastResult> results = new List<RaycastResult>();
		        EventSystem.current.RaycastAll(eventDataCurrentPosition, results);
		        bool isNullClick = true;  //是否点击了空(没有点到指定的目标,如点击空白取消连线的功能)
		        //遍历鼠标点击时,处在射线下的所有UI
		        foreach(RaycastResult r in results)
		        {
		        	if (r.gameObject.name == "指定对象名")
		            {
			            isNullClick = false;
			            selected=r.gameObject;
			        }
		        }
		        if (isNullClick)
		        {
		            selected=null;
		       	}
		      }
		  }
		 if (selected != null)
		 {
			_matching.SetStartPoint(Input.mousePosition);  //点击了空对象 所以线的起点和终点都为鼠标的位置
		 }
		 else
		 {
			 _matching.SetStartPoint(selected.transform.position);
		 }
	}
}

对以上代码进行整理 ```csharp
//移动端
            if (Application.platform == RuntimePlatform.Android ||
                        Application.platform == RuntimePlatform.IPhonePlayer)
            {
                if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
                {
                    GameObject gb = GetPointerOverGameObject(Input.GetTouch(0).position, "查找的对象名");
                    if (gb != null)
                    {
                        //如果点击到的是目标ui所做的事情
                    }
                    else
                    {
                        //如果不是所要做的事情
                    }
                    gb = null;
                }
            }
            else
            {
                if (Input.GetMouseButtonDown(0))
                {
                    GameObject gb = GetPointerOverGameObject(Input.mousePosition, "查找的对象名");
                    if (gb != null)
                    {
                        //如果点击到的是目标ui所做的事情
                    }
                    else
                    {
                       //如果不是所要做的事情
                    }
                    gb = null;
                }
            }
private GameObject GetPointerOverGameObject(Vector2 mousePosition,string name)
{
    //创建一个点击事件
    PointerEventData eventData = new PointerEventData(EventSystem.current);
    eventData.position = mousePosition;
    List<RaycastResult> raycastResults = new List<RaycastResult>();
    //向点击位置发射一条射线,检测是否点击UI
    EventSystem.current.RaycastAll(eventData, raycastResults);
    return  raycastResults.Find(x => x.gameObject.name == name).gameObject;
}

你可能感兴趣的:(unity笔记)