108 lines
3.2 KiB
C#
108 lines
3.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEditor;
|
|
|
|
namespace XRLib
|
|
{
|
|
public static partial class Wtil
|
|
{
|
|
#if UNITY_EDITOR
|
|
public static bool GetCurrentSelectGameObject(out GameObject obj)
|
|
{
|
|
obj = Selection.activeGameObject;
|
|
if (obj == null)
|
|
return false;
|
|
return true;
|
|
}
|
|
#endif
|
|
public static bool isScreenOver(this Camera cam, Vector3 pos)
|
|
{
|
|
var viewPos = cam.WorldToViewportPoint(pos);
|
|
return viewPos.z < 0 || (viewPos.x < 0 || viewPos.x > 1) || (viewPos.y < 0 || viewPos.y > 1);
|
|
}
|
|
|
|
public static void AddLayer(this Camera cam, int layer)
|
|
{
|
|
cam.cullingMask |= 1 << layer;
|
|
}
|
|
public static bool Overlaps(this RectTransform rectTrans1, RectTransform rectTrans2)
|
|
{
|
|
var rect1 = new Rect(rectTrans1.localPosition.x, rectTrans1.localPosition.y, rectTrans1.rect.width, rectTrans1.rect.height);
|
|
var rect2 = new Rect(rectTrans2.localPosition.x, rectTrans2.localPosition.y, rectTrans2.rect.width, rectTrans2.rect.height);
|
|
return rect1.Overlaps(rect2);
|
|
}
|
|
|
|
public static void RemoveLayer(this Camera cam, int layer)
|
|
{
|
|
cam.cullingMask &= ~(1 << layer);
|
|
}
|
|
|
|
|
|
public static void NullCleaning<T, T2>(this Dictionary<T, T2> table)
|
|
{
|
|
var keys = table.Keys;
|
|
|
|
foreach (var k in keys)
|
|
{
|
|
if (k == null)
|
|
{
|
|
table.Remove(k);
|
|
continue;
|
|
}
|
|
|
|
if (table[k] == null)
|
|
{
|
|
table.Remove(k);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static bool Add<T>(this HashSet<T> hash, T[] values)
|
|
{
|
|
bool result = true;
|
|
for(int i = 0; i < values.Length; ++i)
|
|
{
|
|
result = hash.Add(values[i]);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public static T2[] ArrayConvertor<T, T2>(T[] origin) where T : MonoBehaviour where T2 : MonoBehaviour
|
|
{
|
|
T2[] result = new T2[origin.Length];
|
|
for (int i = 0; i < result.Length; ++i)
|
|
{
|
|
result[i] = origin[i] as T2;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
static Dictionary<Component, MeshRenderer[]> meshGroupCache = new();
|
|
public static Vector3 GetMeshGroupCenter(this Component root)
|
|
{
|
|
if (!meshGroupCache.TryGetValue(root, out _))
|
|
{
|
|
meshGroupCache.Add(root, root.GetComponentsInChildren<MeshRenderer>());
|
|
}
|
|
var meshs = meshGroupCache[root];
|
|
Vector3 total = Vector3.zero;
|
|
foreach (var m in meshs)
|
|
{
|
|
total += m.bounds.center;
|
|
}
|
|
return total / meshs.Length;
|
|
}
|
|
|
|
public static Vector3 GetMeshGroupCenter(MeshRenderer[] meshGroup)
|
|
{
|
|
Vector3 total = Vector3.zero;
|
|
foreach (var m in meshGroup)
|
|
{
|
|
total += m.bounds.center;
|
|
}
|
|
return total / meshGroup.Length;
|
|
|
|
}
|
|
}
|
|
} |