Unity 使用this关键字进行函数拓展 - GameObject
【摘要】
Example:
private void Start(){ var testComponent = gameObject .Activate() .Deactivate() .Name("") .Layer(0) .Layer("") .Tag...
Example:
private void Start()
{
var testComponent = gameObject
.Activate()
.Deactivate()
.Name("")
.Layer(0)
.Layer("")
.Tag("")
.GetComponentForcibly(typeof(LineRenderer));
bool isActive = testComponent
.Activate()
.Deactivate()
.Name("")
.Layer(0)
.Layer("")
.Tag("")
.IsActive();
}
Extension:
/// <summary>
/// The extension of gameobject.
/// </summary>
public static class GameObjectExtension
{
/// <summary>
/// Activate the gameobject.
/// </summary>
/// <param name="self"></param>
/// <returns></returns>
public static GameObject Activate(this GameObject self)
{
self.SetActive(true);
return self;
}
/// <summary>
/// Deactivate the gameobject.
/// </summary>
/// <param name="self"></param>
/// <returns></returns>
public static GameObject Deactivate(this GameObject self)
{
self.SetActive(false);
return self;
}
/// <summary>
/// Set name for the gameobject.
/// </summary>
/// <param name="self"></param>
/// <param name="name"></param>
/// <returns></returns>
public static GameObject Name(this GameObject self, string name)
{
self.name = name;
return self;
}
/// <summary>
/// Set layer for the gameobject.
/// </summary>
/// <param name="self"></param>
/// <param name="layer"></param>
/// <returns></returns>
public static GameObject Layer(this GameObject self, int layer)
{
self.layer = layer;
return self;
}
/// <summary>
/// Set layer for the gameobject.
/// </summary>
/// <param name="self"></param>
/// <param name="layer"></param>
/// <returns></returns>
public static GameObject Layer(this GameObject self, string layer)
{
self.layer = LayerMask.NameToLayer(layer);
return self;
}
/// <summary>
/// Set tag for the gameobject.
/// </summary>
/// <param name="self"></param>
/// <param name="tag"></param>
/// <returns></returns>
public static GameObject Tag(this GameObject self, string tag)
{
self.tag = tag;
return self;
}
/// <summary>
/// Get specified component, if it's null, add and return.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="self"></param>
/// <returns></returns>
public static T GetComponentForcibly<T>(this GameObject self) where T : Component
{
var component = self.GetComponent<T>();
return component ?? self.AddComponent<T>();
}
/// <summary>
/// Get specified component, if it's null, add and return.
/// </summary>
/// <param name="self"></param>
/// <param name="type"></param>
/// <returns></returns>
public static Component GetComponentForcibly(this GameObject self, Type type)
{
var component = self.GetComponent(type);
return component ?? self.AddComponent(type);
}
/// <summary>
/// Get the local active state of the compontent's gameObject.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="self"></param>
/// <returns></returns>
public static bool IsActive<T>(this T self) where T : Component
{
return self.gameObject.activeSelf;
}
/// <summary>
/// Activate the component's gameobject.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="self"></param>
/// <returns></returns>
public static T Activate<T>(this T self) where T : Component
{
self.gameObject.SetActive(true);
return self;
}
/// <summary>
/// Deactivate the component's gameobject.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="self"></param>
/// <returns></returns>
public static T Deactivate<T>(this T self) where T : Component
{
self.gameObject.SetActive(false);
return self;
}
/// <summary>
/// Set name for the component's gameobject.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="self"></param>
/// <param name="name"></param>
/// <returns></returns>
public static T Name<T>(this T self, string name) where T : Component
{
self.gameObject.name = name;
return self;
}
/// <summary>
/// Set layer for the component's gameobject.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="self"></param>
/// <param name="layer"></param>
/// <returns></returns>
public static T Layer<T>(this T self, int layer) where T : Component
{
self.gameObject.layer = layer;
return self;
}
/// <summary>
/// Set layer for the component's gameobject.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="self"></param>
/// <param name="layer"></param>
/// <returns></returns>
public static T Layer<T>(this T self, string layer) where T : Component
{
self.gameObject.layer = LayerMask.NameToLayer(layer);
return self;
}
/// <summary>
/// Set tag for the component's gameobject.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="self"></param>
/// <param name="tag"></param>
/// <returns></returns>
public static T Tag<T>(this T self, string tag) where T : Component
{
self.gameObject.tag = tag;
return self;
}
}
文章来源: coderz.blog.csdn.net,作者:CoderZ1010,版权归原作者所有,如需转载,请联系作者。
原文链接:coderz.blog.csdn.net/article/details/109022581
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
作者其他文章
评论(0)