当前位置:网站首页>Unity extension
Unity extension
2022-06-13 00:50:00 【Small fish game development】
public static class GameObjectHelper
{
const string gameObjectIsNull = "GameObject is Null";
public static T IsAddComponent<T>(this GameObject self)where T:Component
{
Assert.IsNotNull(self, gameObjectIsNull);
var com = self.GetComponent<T>();
if(com == null)
{
com = self.AddComponent<T>();
}
return com;
}
}
public static class TransformHelper
{
const string transformIsNull = "Transform is Null";
public static T IsAddComponent<T>(this Transform self) where T : Component
{
Assert.IsNotNull(self, transformIsNull);
return self.gameObject.IsAddComponent<T>();
}
public static Transform GetRoot(this Transform self)
{
Assert.IsNotNull(self, transformIsNull);
Transform transform = self;
while(transform.parent)
{
transform = transform.parent;
}
return transform;
}
public static Transform [] GetChilds(this Transform self)
{
Assert.IsNotNull(self, transformIsNull);
List<Transform> list = new List<Transform>();
Queue<Transform> stack = new Queue<Transform>();
stack.Enqueue(self);
while (stack.Count>0)
{
var transform = stack.Dequeue();
for (var i=0;i<transform.childCount;i++)
{
var child = transform.GetChild(i);
stack.Enqueue(child);
list.Add(child);
}
}
return list.ToArray();
}
public static Transform FindChildsName(this Transform self,string name)
{
var array = self.GetChilds();
for(int i=0,end=array.Length;i<end;i++)
{
if (array[i].gameObject.name.Equals(name))
return array[i];
}
return null;
}
public static Transform[] GetChildsTag(this Transform self,string tag)
{
var array = self.GetChilds();
List<Transform> list = new List<Transform>();
for(int i = 0,end=array.Length; i < end; i++)
{
if(array[i].gameObject.tag.Equals(tag))
{
list.Add(array[i]);
}
}
return list.ToArray();
}
public static Transform[] GetChildsLayer(this Transform self, string layer)
{
return self.GetChildsLayer(LayerMask.NameToLayer(layer));
}
public static Transform[] GetChildsLayer(this Transform self, int layer)
{
var array = self.GetChilds();
List<Transform> list = new List<Transform>();
for (int i = 0, end = array.Length; i < end; i++)
{
if (array[i].gameObject.layer == layer)
{
list.Add(array[i]);
}
}
return list.ToArray();
}
}
边栏推荐
- Canvas game 2048 free map size
- pytorch是什么?解释pytorch的基本概念
- How many steps are appropriate for each cycle of deep learning?
- 【服务器数据恢复】存储服务器之间迁移数据时数据丢失恢复成功案例
- MySQL query table field information
- In / out / inout details of MySQL stored procedures
- [JS component] custom paging
- Biological unlocking - Fingerprint entry process
- How to determine whether T is a value type in a generic type or a reference class- How to determine whether T is a value type or reference class in generic?
- Canvas airplane game
猜你喜欢
![[JS component] customize the right-click menu](/img/a3/4555619db17e4c398e72c7d6b12f5d.jpg)
[JS component] customize the right-click menu

How to determine whether T is a value type in a generic type or a reference class- How to determine whether T is a value type or reference class in generic?

(01). Net Maui actual construction project

阿姨学代码续集:能力吊打大批程序员

Target recognition gadget

Kotlin 协程的四种启动模式

什么是 Meebits?一个简短的解释

Easyexcel read excel simple demo

深度学习每周期的步数多少合适?

DNS attack surface analysis
随机推荐
[JS] battle chess
gpu加速pytorch能用吗?
草在结种子了
[virtual machine] notes on virtual machine environment problems
Kali system -- dnsrecon for DNS collection and analysis
Development notes of Mongoose
阿姨学代码续集:能力吊打大批程序员
Rest at home today
[003] embedded learning: creating project templates - using stm32cubemx
AOF持久化
[error] invalid use of incomplete type uses an undefined type
Easyexcel read excel simple demo
Today's sleep quality record 74 points
Undirected graph -- computing the degree of a node in compressed storage
Win10 home vs pro vs enterprise vs enterprise LTSC
Oceanbase is the leader in the magic quadrant of China's database in 2021
Arduino interrupt
【北亚服务器数据恢复】虚拟机文件丢失导致Hyper-V服务瘫痪的数据恢复案例
从ADK的WinPE自己手动构建自己的PE
pytorch是什么?解释pytorch的基本概念