UnityUGUI通过鼠标在Canvas上画出矩形框并在范围内生成相应大小的Image

更新时间:2023-07-08 10:55:11 阅读: 评论:0

UnityUGUI通过⿏标在Canvas上画出矩形框并在范围内⽣成相应⼤⼩的Image PS:附加功能,画出矩形框后判断⼀个图⽚是否包围在其中
⾸先⿏标画框(类似PS上框选区域)时,有四种操作:
1. 从左上到右下
2. 从左下到右上
3. 从右上到左下
4. 从右下到左上
这就需要考虑到画出的线的⾓度及所在象限的问题(为了⽣成框选的Image时设置每条代表的边的Pivot)
直接上代码:
/************************************************************
FileName: DrawLineByImage.cs
Author:菜菜      Version :1.0          Date: 2019-06-03
Description:NeedInput
************************************************************/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PicklePro;卡萨布兰卡歌曲
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System;秘密用英语怎么说
namespace PicklePro
{
//对齐⽅式
public enum AlignmentType
{
None,
UpLeft,
MidLeft,
DownLeft,
UpMid,
Center,
DownMid,
UpRight,
MidRight,
DownRight,
}
public class DrawLineByImage : MonoBehaviour,IPointerDownHandler,IPointerUpHandler
{
public RectTransform cursorRect;
public Vector2 startA;//指的是rectTransform.anchoredPosition;框选的起点
public Vector2 endB;//框选的终点
//起点到终点的宽、⾼距离
float distanceX;
float distanceY;
public RectTransform point1;//辅助显⽰宽度框的图⽚,实际是⼀张Image
public RectTransform point2;//辅助显⽰⾼度框的图⽚,实际是⼀张Image
public RectTransform parent;//所画出的框所在的⽗体
private bool drawRectangle;//是否在画的判断
GameObject width1;//宽1
GameObject width2;//宽2
GameObject higth1;//⾼1
GameObject higth2;//⾼2
private AlignmentType currentSlignmentType = AlignmentType.None;
private bool isCreat =fal;
private bool isCreatQuads =fal;
//画框
void DrawQuads(Vector2 a, Vector2 b, RectTransform point1, RectTransform point2, Transform parent) {
if(!ModelessSimulationStep.isDraw)
return;
if(a != b)
{
//实例化四条边
width1 =Instantiate(point1.gameObject, parent);
width2 =Instantiate(point1.gameObject, parent);
higth1 =Instantiate(point2.gameObject, parent);
higth2 =Instantiate(point2.gameObject, parent);
width1.SetActive(true);
width2.SetActive(true);
higth1.SetActive(true);
higth2.SetActive(true);
//求夹⾓,⽤于判断象限
float angle = Vector2.SignedAngle(a - b, Vector2.left);
//左上到右下
有声英语if(angle >0&& angle <=90)
{
//设置宽⾼的Pivot,使之能按对应的⽅式形变
width1.GetComponent<RectTransform>().pivot =new Vector2(0f,0.5f);
width2.GetComponent<RectTransform>().pivot =new Vector2(1f,0.5f);
higth1.GetComponent<RectTransform>().pivot =new Vector2(0.5f,1f);
higth2.GetComponent<RectTransform>().pivot =new Vector2(0.5f,0f);
//实际宽⾼的数值
distanceX = b.x - a.x;
distanceY = a.y - b.y;
currentSlignmentType = AlignmentType.DownLeft;
}
//左下到右上
el if(angle <=0&& angle >-90)
{
width1.GetComponent<RectTransform>().pivot =new Vector2(0f,0.5f);
width2.GetComponent<RectTransform>().pivot =new Vector2(1f,0.5f);
higth1.GetComponent<RectTransform>().pivot =new Vector2(0.5f,0f);
higth2.GetComponent<RectTransform>().pivot =new Vector2(0.5f,1f);
distanceX = b.x - a.x;
distanceY = b.y - a.y;生病的英文
currentSlignmentType = AlignmentType.MidLeft;
}
//右下到左上
el if(angle >-180&& angle <=-90)
{
width1.GetComponent<RectTransform>().pivot =new Vector2(1f,0.5f);
width2.GetComponent<RectTransform>().pivot =new Vector2(0f,0.5f);堪培拉英文
higth1.GetComponent<RectTransform>().pivot =new Vector2(0.5f,0f);
higth2.GetComponent<RectTransform>().pivot =new Vector2(0.5f,1f);
distanceX = a.x - b.x;
distanceY = b.y - a.y;
currentSlignmentType = AlignmentType.UpRight;
}
//右上到左下
el if(angle >90&& angle <180)
{
width1.GetComponent<RectTransform>().pivot =new Vector2(1f,0.5f);
width2.GetComponent<RectTransform>().pivot =new Vector2(0f,0.5f);
higth1.GetComponent<RectTransform>().pivot =new Vector2(0.5f,1f);
higth2.GetComponent<RectTransform>().pivot =new Vector2(0.5f,0f);
distanceX = a.x - b.x;
distanceY = a.y - b.y;
currentSlignmentType = AlignmentType.MidRight;
}
el
{
distanceX =0;
distanceY =0;
}
//设置起始位置
width1.GetComponent<RectTransform>().anchoredPosition = a;
width2.GetComponent<RectTransform>().anchoredPosition = b;
higth1.GetComponent<RectTransform>().anchoredPosition = a;
higth2.GetComponent<RectTransform>().anchoredPosition = b;
//posx  每隔0.1  width差100(对应的距离和实际image之间的⽐例)
width1.GetComponent<RectTransform>().sizeDelta =new Vector2(distanceX *1000, width1.GetComponent<RectTransform>().sizeDelta.y);                width2.GetComponent<RectTransform>().sizeDelta =new Vector2(distanceX *1000, width2.GetCom
ponent<RectTransform>().sizeDelta.y);
higth1.GetComponent<RectTransform>().sizeDelta =new Vector2(higth1.GetComponent<RectTransform>().sizeDelta.x, distanceY *1000);                higth2.GetComponent<RectTransform>().sizeDelta =new Vector2(higth2.GetComponent<RectTransform>().sizeDelta.x, distanceY *1000);
isCreatQuads =true;
}
}有里知花
//清除上次的框
public void ClearQuads()
{
if(ModelessSimulationStep.isConfirm)
return;
if(isCreatQuads)
{
Destroy(width1);
Destroy(width2);
Destroy(higth1);
Destroy(higth2);
isCreatQuads =fal;
}
}
#region  UGUI EventSystem检测
public void OnPointerDown(PointerEventData eventData)
{
drawRectangle =true;//如果⿏标左键按下设置开始画线标志
RectTransformUtility.ScreenPointToLocalPointInRectangle(
AwakeLoadByCurrentState._instance.ModelessSimulationSoftWorkSpeace.GetComponent<RectTransform>(), uPosition, GameObjec tManager.GetInstance().MainCamera.GetComponent<Camera>(), out startA);
//记录按下位置
startA =new Vector2(startA.x +0.01f, startA.y -0.01f);
}
public void OnPointerUp(PointerEventData eventData)
{
drawRectangle =fal;//如果⿏标左键放开结束画线
RectTransformUtility.ScreenPointToLocalPointInRectangle(
AwakeLoadByCurrentState._instance.ModelessSimulationSoftWorkSpeace.GetComponent<RectTransform>(), uPosition, GameObject Manager.GetInstance().MainCamera.GetComponent<Camera>(), out endB);
endB =new Vector2(endB.x +0.01f, endB.y -0.01f);
if(AwakeLoadByCurrentState._instance.RangeChooPanel.activeInHierarchy)
return;
ClearQuads();
you have my word
DrawQuads(startA, endB, point1, point2, parent);
ShowRangeChooPanel();
}
#endregion
#region  ⿏标点击检测画框
jungle
//void Update()
//{
//    if (Input.GetMouButtonDown(0))
//    {
//        drawRectangle = true;//如果⿏标左键按下设置开始画线标志
//        RectTransformUtility.ScreenPointToLocalPointInRectangle(
//            AwakeLoadByCurrentState._instance.ModelessSimulationSoftWorkSpeace.GetComponent<RectTransform>(), uPosition, GameO bjectManager.GetInstance().MainCamera.GetComponent<Camera>(), out startA);
搞笑哲理故事
/
/        //记录按下位置
//        startA = new Vector2(startA.x+0.01f,startA.y-0.01f);
//    }
//    if (Input.GetMouButton(0))
//    {
//        RectTransformUtility.ScreenPointToLocalPointInRectangle(
//          AwakeLoadByCurrentState._instance.ModelessSimulationSoftWorkSpeace.GetComponent<RectTransform>(), uPosition, GameO bjectManager.GetInstance().MainCamera.GetComponent<Camera>(), out endB);
//        endB = cursorRect.anchoredPosition;
//    }
/
/    if (Input.GetMouButtonUp(0))
//    {
//        drawRectangle = fal;//如果⿏标左键放开结束画线
//        RectTransformUtility.ScreenPointToLocalPointInRectangle(
//          AwakeLoadByCurrentState._instance.ModelessSimulationSoftWorkSpeace.GetComponent<RectTransform>(), uPosition, GameO bjectManager.GetInstance().MainCamera.GetComponent<Camera>(), out endB);
bjectManager.GetInstance().MainCamera.GetComponent<Camera>(), out endB);
//        endB = new Vector2(endB.x+0.01f,endB.y-0.01f);
//        if (AwakeLoadByCurrentState._instance.RangeChooPanel.activeInHierarchy) //            return;
//        ClearQuads();
//        DrawQuads(startA, endB, point1, point2, parent);
//        ShowRangeChooPanel();
//    }
//}
#endregion
public GameObject img;
//框选区域内⽣成同等⼤⼩的Image
public void CreatImage(Color _color)
{
if(img!=null)
{
Destroy(img);
}
img =new GameObject();
img.name ="img";
img.AddComponent<Image>();
img.GetComponent<Image>().color = _color;
//根据之前的划线⽅式,定⽣成的Image  Pivot位置
switch(currentSlignmentType)
{
ca AlignmentType.MidLeft:
img.GetComponent<RectTransform>().pivot =new Vector2(0,0);
break;
ca AlignmentType.UpRight:
img.GetComponent<RectTransform>().pivot =new Vector2(1,0);
break;嚆矢
ca AlignmentType.MidRight:
img.GetComponent<RectTransform>().pivot =new Vector2(1,1);
break;
ca AlignmentType.DownLeft:
ca AlignmentType.UpMid:
ca AlignmentType.Center:
ca AlignmentType.DownMid:
ca AlignmentType.DownRight:
ca AlignmentType.None:
ca AlignmentType.UpLeft:
default:
img.GetComponent<RectTransform>().pivot =new Vector2(0,1);
break;

本文发布于:2023-07-08 10:55:11,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/78/1085345.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:设置   矩形框   画线   判断
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图