当前位置:网站首页>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 )
边栏推荐
- Cool canvas animation shock wave JS special effect
- 这是我见过写得最烂的Controller层代码...
- JDBC quick start
- Wechat reservation of small program completion works (5) assignment book of small program graduation project
- 【无标题】
- flink sql怎么持久化?
- js触屏小游戏源码冰雪之旅
- Redis learning notes
- Illustration leetcode - 919. Complete binary tree inserter (difficulty: medium)
- Graduation project of wechat small program ordering system of small program completion works (6) opening defense ppt
猜你喜欢

Wechat reservation of the completed works of the applet graduation project (6) opening defense ppt

51单片机内部外设:定时器和计数器

Wechat reservation of completed works of applet graduation project (4) opening report

这是我见过写得最烂的Controller层代码...

CIR industrial automation radar

Wechat reservation of small program completion works (5) assignment book of small program graduation project

uni-app

25位撤销博士学位

Leetcode · 83 biweekly race · 6129. Number of all 0 subarrays · mathematics

Mongodb database
随机推荐
Graduation design of wechat small program ordering system of small program completion works (5) assignment
Solving a random number problem
51单片机外设篇:电机
Source code of short video live broadcast system
机器人跳跃问题
【无标题】
Fundamentals of C language
Does the server operation and maintenance need to be online 24 hours? Do you need to work overtime on weekends?
unity客户端读取文本配置
Additional: in the lower division / county (data sheet)
图解LeetCode——919. 完全二叉树插入器(难度:中等)
Illustration leetcode - 1184. Distance between bus stops (difficulty: simple)
Swift初始化器及可选链
When testing VPN, the IP found by the command line is inconsistent with that of Baidu search
艺术 NFT 的发展之路
Practice of establishing dynamic programming state transition equation
Table table expansion internal row switching effect
Wechat reservation of small program completion works (5) assignment book of small program graduation project
Full solution of JDBC API
Sticky.js page scrolling div fixed position plug-in