89 lines
2.7 KiB
C#
89 lines
2.7 KiB
C#
using System.Text;
|
|
using UnityEngine;
|
|
|
|
namespace BingoBrain.Core
|
|
{
|
|
public static class GameObjectExtend
|
|
{
|
|
|
|
|
|
public static void SetParent(this GameObject gameObject, GameObject parentGo, bool worldPositionStays = false)
|
|
{
|
|
if (parentGo)
|
|
{
|
|
gameObject.transform.SetParent(parentGo.transform, worldPositionStays);
|
|
}
|
|
}
|
|
|
|
public static void SetParent(this GameObject gameObject, Transform parentTf, bool worldPositionStays = false)
|
|
{
|
|
if (parentTf)
|
|
{
|
|
gameObject.transform.SetParent(parentTf, worldPositionStays);
|
|
}
|
|
}
|
|
|
|
public static void SetLayer(this GameObject gameObject, string layerName)
|
|
{
|
|
Transform[] transArr = gameObject.transform.GetComponentsInChildren<Transform>();
|
|
for (int i = 0; i < transArr.Length; i++)
|
|
{
|
|
transArr[i].gameObject.layer = LayerMask.NameToLayer(layerName);
|
|
}
|
|
}
|
|
|
|
public static void SetLayer(this GameObject gameObject, LayerMask layer)
|
|
{
|
|
Transform[] transArr = gameObject.transform.GetComponentsInChildren<Transform>();
|
|
for (int i = 0; i < transArr.Length; i++)
|
|
{
|
|
transArr[i].gameObject.layer = layer;
|
|
}
|
|
}
|
|
|
|
public static void DestroyAllChild(this GameObject gameObject)
|
|
{
|
|
foreach (Transform child in gameObject.transform)
|
|
{
|
|
Object.Destroy(child.gameObject);
|
|
}
|
|
}
|
|
|
|
public static T GetSoleComponent<T>(this GameObject gameObject) where T : Component
|
|
{
|
|
T component = gameObject.GetComponent<T>();
|
|
if (component == null) component = gameObject.AddComponent<T>();
|
|
return component;
|
|
}
|
|
|
|
|
|
public static string GetGameObjectRelativePath(this GameObject gameObject, GameObject child)
|
|
{
|
|
string parentPath = gameObject.GetGameObjectPath();
|
|
string childPath = child.GetGameObjectPath();
|
|
if (childPath.StartsWith(parentPath))
|
|
{
|
|
return childPath.Remove(0, parentPath.Length + 1);
|
|
}
|
|
else
|
|
{
|
|
return string.Empty;
|
|
}
|
|
}
|
|
|
|
|
|
public static string GetGameObjectPath(this GameObject gameObject)
|
|
{
|
|
Transform transform = gameObject.transform;
|
|
StringBuilder sb = new StringBuilder();
|
|
sb.Append(transform.name);
|
|
while (transform.parent != null)
|
|
{
|
|
transform = transform.parent;
|
|
sb.Insert(0, transform.name + "/");
|
|
}
|
|
|
|
return sb.ToString();
|
|
}
|
|
}
|
|
} |