在拼UI的過(guò)程中會(huì)添加很多圖片文字,但是很容易會(huì)忽略其中一點(diǎn)就是把無(wú)用的RaycastTarget去掉,因?yàn)殚_(kāi)啟此選項(xiàng),雖然此組建雖然不需要接受射線,但是它而然工作且消耗性能


在網(wǎng)上找了2個(gè)小工具:
- 其中一個(gè)是在Editor模式下用藍(lán)色框出啟用RatcastTarget的組件

Code如下。掛在任意GameObject上即可
#if UNITY_EDITOR
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class DebugUILine : 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.blue;
for (int i = 0; i < 4; i++)
Gizmos.DrawLine(fourCorners[i], fourCorners[(i + 1) % 4]);
}
}
}
}
#endif
- 第二個(gè)是自動(dòng)取消創(chuàng)建對(duì)應(yīng)Image Text 的RaycastTarget選項(xiàng)(重寫(xiě)unity創(chuàng)建對(duì)應(yīng)組件,創(chuàng)建組建后自動(dòng)取消選項(xiàng))
Code 如下
using UnityEngine;
using System.Collections;
using UnityEditor;
using UnityEngine.UI;
public class UITools
{
/// <summary>
/// 自動(dòng)取消RatcastTarget
/// </summary>
[MenuItem("GameObject/UI/Image")]
static void CreatImage()
{
if (Selection.activeTransform)
{
if (Selection.activeTransform.GetComponentInParent<Canvas>())
{
GameObject go = new GameObject("image", typeof(Image));
go.GetComponent<Image>().raycastTarget = false;
go.transform.SetParent(Selection.activeTransform);
}
}
}
//重寫(xiě)Create->UI->Text事件
[MenuItem("GameObject/UI/Text")]
static void CreatText()
{
if (Selection.activeTransform)
{
//如果選中的是列表里的Canvas
if (Selection.activeTransform.GetComponentInParent<Canvas>())
{
//新建Text對(duì)象
GameObject go = new GameObject("text", typeof(Text));
//將raycastTarget置為false
go.GetComponent<Text>().raycastTarget = false;
//設(shè)置其父物體
go.transform.SetParent(Selection.activeTransform);
}
}
}
//重寫(xiě)Create->UI->Text事件
[MenuItem("GameObject/UI/Raw Image")]
static void CreatRawImage()
{
if (Selection.activeTransform)
{
//如果選中的是列表里的Canvas
if (Selection.activeTransform.GetComponentInParent<Canvas>())
{
//新建Text對(duì)象
GameObject go = new GameObject("RawImage", typeof(RawImage));
//將raycastTarget置為false
go.GetComponent<RawImage>().raycastTarget = false;
//設(shè)置其父物體
go.transform.SetParent(Selection.activeTransform);
}
}
}
}