当前位置:网站首页>Unity ugui interaction (new ideas)
Unity ugui interaction (new ideas)
2022-07-25 08:55:00 【Scholar with wine】
I came across a question in the Q & A these two days : What we want to achieve is to give a location , Then interact with UI, At first I misunderstood , Then I thought again , It's natural to think of rays + Collision body . When you open the editor , Think about it again , In this way, we not only need to change the canvas RenderMode, Also give all that you want to interact UI Make a collision body , There seems to be some trouble .UGUI Isn't it your own set of radiographic tests , You can try it .
Then the first edition wrote this
void Start()
{
mPointer = transform.GetComponent<RectTransform>();
eventSystem = EventSystem.current;
pointerEventData = new PointerEventData(eventSystem);
}
// Update is called once per frame
void Update()
{
PointerSimulation();
}
void PointerSimulation()
{
pointerEventData.position = mPointer.anchoredPosition;
eventSystem.RaycastAll(pointerEventData, uiRaycastResultCache);
Debug.Log($"{
uiRaycastResultCache.Count}");
}
Run the test , Find out debug Has always been a 0, Think about it. ,pointerEventData.position We usually use Input.MousePosition, The screen space coordinate system used by the mouse is based on the lower left corner of the screen (0,0) Dot , So modify the one used to simulate the mouse UI The relative layout of .
Here's for the novice , Do you want us to use UGUI Please make sure your canvas has Graphic Raycaster Components 、UI above raycast target It's on .

The next step is to follow UI Interaction , The first reaction in my mind is to interact UI Hang him up Item Little script , Then the management class gets all Item perform event wait , Then it soon fell , It still feels like trouble . Finally, I checked , Learned a new API:ExecuteEvents.Execute
It can be PointerEventData Pass it to the specified object and trigger the transmitted assignment UI Event type , therefore , The script of the second version is as follows
void PointerSimulation()
{
pointerEventData.position = mPointer.anchoredPosition;
eventSystem.RaycastAll(pointerEventData, uiRaycastResultCache);
Debug.Log($"{
uiRaycastResultCache.Count}");
if (uiRaycastResultCache.Count > 0)
{
for (int i = 0; i < uiRaycastResultCache.Count; i++)
{
eventSystem.SetSelectedGameObject(uiRaycastResultCache[i].gameObject);
ExecuteEvents.Execute(uiRaycastResultCache[i].gameObject, pointerEventData, ExecuteEvents.pointerClickHandler);
}
}
}
When UI Radiographic detection UI after , Stored in list in , Then traverse list, Set the hit object to the selected state , Then simulate triggering a mouse click . Of course, we can do more , such as :
void Drag()
{
pointerEventData.position = Input.mousePosition;
// Radiographic testing ui
eventSystem.RaycastAll(pointerEventData, uiRaycastResultCache);
Debug.Log($"{
uiRaycastResultCache.Count}");
if (uiRaycastResultCache.Count > 0)
{
eventSystem.SetSelectedGameObject(uiRaycastResultCache[0].gameObject);
if (Input.GetMouseButtonDown(0))
{
ExecuteEvents.Execute(uiRaycastResultCache[0].gameObject, pointerEventData, ExecuteEvents.pointerDownHandler);
}
if (Input.GetMouseButton(0))
{
ExecuteEvents.Execute(uiRaycastResultCache[0].gameObject, pointerEventData, ExecuteEvents.dragHandler);
}
if(Input.GetMouseButtonUp(0))
{
ExecuteEvents.Execute(uiRaycastResultCache[0].gameObject, pointerEventData, ExecuteEvents.pointerUpHandler);
}
}
}
You can put you UI Write down all the time types involved in it , There will be no error if it is not used . Here I simply write a button triggered and UI Drag and drop (UI You need to write the script of dragging specific logic by yourself , I won't let it go )
边栏推荐
- 超赞的yolo目标检测训练所用垃圾分类数据集共享——标注好的约3000张
- Additional: SQL statement area / county in the middle half (data table)
- 360度拖拽全景图插件tpanorama.js
- js弹出式城市筛选组件匹配手机移动端
- Record the process of two multi terminal troubleshooting
- Technical aspect ② what are the index types in MySQL and briefly introduce them? When do I need to create an index? When is it not necessary to create an index? Why does the query speed increase after
- CIR industrial automation radar
- Graduation project of wechat small program ordering system of small program completion works (7) Interim inspection report
- NFT guide for musicians
- Es source code reading (I) "suggestions collection"
猜你喜欢

The garbage classification data set used in the excellent Yolo target detection training is shared - about 3000 labeled

Visual query (sp_helptext) -- quick query of stored procedures containing specified strings (with source code)

51单片机外设篇:电机

Technical aspect ② what are the index types in MySQL and briefly introduce them? When do I need to create an index? When is it not necessary to create an index? Why does the query speed increase after

IDEA下依赖冲突解决方法

How can hospitals achieve efficient and low-cost operation and maintenance? Is there any software that can meet it?

Wechat applet ordering system graduation design of applet completion works (8) graduation design thesis template

本周大新闻|FCC曝光Pico 4 VR一体机,雷朋母公司建立智能眼镜实验室
![[graduation project] cinema booking management system based on micro Service Framework](/img/74/b12f1fe105c117aa96ebaa4226f85b.png)
[graduation project] cinema booking management system based on micro Service Framework

英特尔就产品延误向Xe HPG寻宝游戏获奖者道歉并公布奖品样貌
随机推荐
Qt|qlabole change line spacing when displaying multiple lines
This ten-year content industry infrastructure company is actually an invisible Web3 pioneer
Additional: in the lower division / county (data sheet)
Swift初始化器及可选链
Wechat sports ground reservation applet graduation design of applet completion works (2) applet function
英特尔就产品延误向Xe HPG寻宝游戏获奖者道歉并公布奖品样貌
The simplest sklearn environment configuration tutorial in the whole network (100% success)
360 degree drag panorama plug-in tpanorama.js
How can hospitals achieve efficient and low-cost operation and maintenance? Is there any software that can meet it?
How does Flink SQL persist?
Graduation project of wechat small program ordering system of small program completion works (4) opening report
The hole of scroll view in uniapp
Graduation design of wechat small program ordering system of small program completion works (5) assignment
Django4.0 + Web + MySQL5.7 实现简单登录操作
YOLOV5环境配置
Wechat sports field reservation of the finished works of the applet graduation project (4) opening report
Wechat sports ground reservation applet graduation design of applet completion works (3) background function
Read and write models and organize notes
Freemaker template engine
51 single chip microcomputer controls nixie tube display