2023-04-03 11:04:31 +08:00

181 lines
5.7 KiB
C#

#if UNITY_EDITOR
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using BF;
namespace BFEditor
{
[ExecuteInEditMode]
public class ShowRaycast : MonoBehaviour
{
Text[] texts;
Image[] images;
EmptyRaycast[] emptys;
RawImage[] rawImages;
int imageCount;
int textCount;
int emptyCount;
int rawImageCount;
List<string> imagePathList = new List<string>();
List<string> textPathList = new List<string>();
List<string> emptyPathList = new List<string>();
List<string> rawImagePathList = new List<string>();
Vector3[] vector3s = new Vector3[4];
bool editorWindowMode = false;
public void SetEditorWindowMode(bool flag)
{
editorWindowMode = flag;
}
public int GetTotalCount()
{
return imageCount + textCount + emptyCount + rawImageCount;
}
public int GetImageCount()
{
return imageCount;
}
public int GetTextCount()
{
return textCount;
}
public int GetRawImageCount()
{
return rawImageCount;
}
public int GetEmptyCount()
{
return emptyCount;
}
public List<string> GetImagePathList()
{
return imagePathList;
}
public List<string> GetTextPathList()
{
return textPathList;
}
public List<string> GetEmptyPathList()
{
return emptyPathList;
}
public List<string> GetRawImagePathList()
{
return rawImagePathList;
}
void Clear()
{
imageCount = 0;
textCount = 0;
emptyCount = 0;
rawImageCount = 0;
imagePathList.Clear();
textPathList.Clear();
emptyPathList.Clear();
rawImagePathList.Clear();
}
void OnDrawGizmos()
{
if (editorWindowMode)
Clear();
texts = transform.GetComponentsInChildren<Text>(true);
images = transform.GetComponentsInChildren<Image>(true);
emptys = transform.GetComponentsInChildren<BF.EmptyRaycast>(true);
rawImages = transform.GetComponentsInChildren<RawImage>(true);
GameObject tempGo = new GameObject("temp");
RectTransform newRt = tempGo.AddComponent<RectTransform>();
Action<RectTransform, Color> drawAction = (RectTransform rt, Color color) =>
{
if (rt == null)
return;
rt.GetWorldCorners(vector3s);
Gizmos.color = color;
Gizmos.DrawLine(vector3s[0], vector3s[1]);
Gizmos.DrawLine(vector3s[1], vector3s[2]);
Gizmos.DrawLine(vector3s[2], vector3s[3]);
Gizmos.DrawLine(vector3s[3], vector3s[0]);
newRt.SetParent(rt.parent);
newRt.localPosition = rt.localPosition;
newRt.localRotation = rt.localRotation;
newRt.localScale = rt.localScale;
newRt.sizeDelta = new Vector2(rt.sizeDelta.x + 1.0f, rt.sizeDelta.y + 1.0f);
newRt.GetWorldCorners(vector3s);
Gizmos.DrawLine(vector3s[0], vector3s[1]);
Gizmos.DrawLine(vector3s[1], vector3s[2]);
Gizmos.DrawLine(vector3s[2], vector3s[3]);
Gizmos.DrawLine(vector3s[3], vector3s[0]);
};
int i = 0;
for (; i < images.Length; i++)
if (images[i].raycastTarget)
{
drawAction(images[i].GetComponent<RectTransform>(), Color.red);
if (editorWindowMode)
{
imageCount++;
string path = "";
BF.Utils.GetTransformPath(images[i].transform, transform, ref path);
imagePathList.Add(path);
}
}
i = 0;
for (; i < texts.Length; i++)
if (texts[i].raycastTarget)
{
drawAction(texts[i].GetComponent<RectTransform>(), Color.green);
if (editorWindowMode)
{
textCount++;
string path = "";
BF.Utils.GetTransformPath(texts[i].transform, transform, ref path);
textPathList.Add(path);
}
}
i = 0;
for (; i < emptys.Length; i++)
if (emptys[i].raycastTarget)
{
drawAction(emptys[i].GetComponent<RectTransform>(), Color.yellow);
if (editorWindowMode)
{
emptyCount++;
string path = "";
BF.Utils.GetTransformPath(emptys[i].transform, transform, ref path);
emptyPathList.Add(path);
}
}
i = 0;
for (; i < rawImages.Length; i++)
if (rawImages[i].raycastTarget)
{
drawAction(rawImages[i].GetComponent<RectTransform>(), Color.blue);
if (editorWindowMode)
{
rawImageCount++;
string path = "";
BF.Utils.GetTransformPath(rawImages[i].transform, transform, ref path);
rawImagePathList.Add(path);
}
}
DestroyImmediate(tempGo);
}
}
}
#endif