Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

The Unity of small science

As always, here are some popular science tips from Unity:

  • Unity is a real-time 3D interactive content creation and operation platform.
  • All creators, including game development, art, architecture, car design, film and television, use Unity to bring their ideas to life.
  • The Unity platform offers a comprehensive suite of software solutions for creating, operating and monetizing any real-time interactive 2D and 3D content on mobile, tablet, PC, game console, augmented reality and virtual reality devices.
  • You can also simply think of Unity as a game engine that can be used to make professional games!

Learn Unity

Quickly find the game object in the scene with Raycast Target checked

In Unity UI events are triggered in EventSystem during the Update Process.

The UGUI iterates through all uIs on the screen where RaycastTarget is true, fires lines, and sorts to find the UI that the player fires first, then throws events to the logical layer in response.

The simple understanding is: Every UI element needs to check RaycastTarget to interact with the user (mouse clicks, etc.) but some UIs don’t need to interact with the user. Checking RaycastTarget not only costs performance, but sometimes blocks interaction with other UIs

Because a lot of UI elements are stacked on top of each other, if you check the Raycast Target you’re bound to have some UI elements that you can’t interact with because they’re covered

So how do you quickly see which UI elements in the scene have this Raycast Target checked

As for how to optimize things, later will be more detailed introduction, is not introduced in this small knowledge point article!

Put the following code directly into the script and mount it into the scenario

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class test1 : MonoBehaviour
{
	static Vector3[] fourCorners = new Vector3[4];
	void OnDrawGizmos()
	{
		foreach (MaskableGraphic g in GameObject.FindObjectsOfType<MaskableGraphic>())
		{
			if (g.raycastTarget)
			{
				RectTransform rectTransform = g.transform as RectTransform;
				rectTransform.GetWorldCorners(fourCorners);
				Gizmos.color = Color.green;
				for (int i = 0; i < 4; i++)
					Gizmos.DrawLine(fourCorners[i], fourCorners[(i + 1) % 4]); }}}}Copy the code

Then we’ll see a border prompt for UI elements in the Scene

This is the method implemented in the above code. All UI elements with RaycastTarget checked will have a border, and unchecked will not

RaycastTargetIn the monitor panel, you can uncheck it at any time. Note that the border is only visible in the Scene view, not the Game view.