当前位置:网站首页>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 )
边栏推荐
- IDEA下依赖冲突解决方法
- This ten-year content industry infrastructure company is actually an invisible Web3 pioneer
- 酷炫canvas动画冲击波js特效
- 51 MCU peripherals: Motor
- sticksy.js页面滚动div固定位置插件
- 51 MCU internal peripherals: timer and counter
- 51 MCU peripherals: buzzer
- This is the worst controller layer code I've ever seen
- Solutions to ten questions of leetcode database
- Wechat reservation applet graduation design of applet completion works (1) development outline
猜你喜欢

51 MCU internal peripherals: serial port communication

Wechat sports ground reservation applet graduation project of applet completion works (1) development outline

Intel apologized to the winners of Xe HPG treasure hunt game for product delay and announced the appearance of the prize

What version of Oracle10g single instance database is better to upgrade to? Ask for suggestions

CIR industrial automation radar

When testing VPN, the IP found by the command line is inconsistent with that of Baidu search

Oracle10g单实例数据库升级到哪个版本好,求建议

JS small game source code magic tower breakthrough Download

This is the worst controller layer code I've ever seen
![[STL]stack&queue模拟实现](/img/92/c040c0e937e2666ee179189c60a3f2.png)
[STL]stack&queue模拟实现
随机推荐
Graduation design of wechat small program ordering system of small program completion works (3) background function
Anti shake and throttling
BGP border gateway protocol basic knowledge points
Additional: in the lower division / county (data sheet)
游戏外挂怎么做?
CIR industrial automation radar
What version of Oracle10g single instance database is better to upgrade to? Ask for suggestions
附加:下半部分sql语句 区/县(数据表)
The development of art NFT
图解LeetCode——919. 完全二叉树插入器(难度:中等)
read
How to connect tdengine with idea database tool?
Robot jumping problem
Wechat sports ground reservation applet graduation design of applet completion works (3) background function
智能运维场景解析:如何通过异常检测发现业务系统状态异常
The international summit osdi included Taobao system papers for the first time, and end cloud collaborative intelligence was recommended by the keynote speech of the conference
Canvas dynamic picture avatar shaking JS special effect
Foundation 31: Selenium positioning dynamic ID element
JS touch screen game source code ice and snow journey
The simplest sklearn environment configuration tutorial in the whole network (100% success)