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:
///
/// The extension of gameobject.
///
public static class GameObjectExtension
{
///
/// Activate the gameobject.
///
///
///
public static GameObject Activate(this GameObject self)
{
self.SetActive(true);
return self;
}
///
/// Deactivate the gameobject.
///
///
///
public static GameObject Deactivate(this GameObject self)
{
self.SetActive(false);
return self;
}
///
/// Set name for the gameobject.
///
///
///
///
public static GameObject Name(this GameObject self, string name)
{
self.name = name;
return self;
}
///
/// Set layer for the gameobject.
///
///
///
///
public static GameObject Layer(this GameObject self, int layer)
{
self.layer = layer;
return self;
}
///
/// Set layer for the gameobject.
///
///
///
///
public static GameObject Layer(this GameObject self, string layer)
{
self.layer = LayerMask.NameToLayer(layer);
return self;
}
///
/// Set tag for the gameobject.
///
///
///
///
public static GameObject Tag(this GameObject self, string tag)
{
self.tag = tag;
return self;
}
///
/// Get specified component, if it's null, add and return.
///
///
///
///
public static T GetComponentForcibly(this GameObject self) where T : Component
{
var component = self.GetComponent();
return component ?? self.AddComponent();
}
///
/// Get specified component, if it's null, add and return.
///
///
///
///
public static Component GetComponentForcibly(this GameObject self, Type type)
{
var component = self.GetComponent(type);
return component ?? self.AddComponent(type);
}
///
/// Get the local active state of the compontent's gameObject.
///
///
///
///
public static bool IsActive(this T self) where T : Component
{
return self.gameObject.activeSelf;
}
///
/// Activate the component's gameobject.
///
///
///
///
public static T Activate(this T self) where T : Component
{
self.gameObject.SetActive(true);
return self;
}
///
/// Deactivate the component's gameobject.
///
///
///
///
public static T Deactivate(this T self) where T : Component
{
self.gameObject.SetActive(false);
return self;
}
///
/// Set name for the component's gameobject.
///
///
///
///
///
public static T Name(this T self, string name) where T : Component
{
self.gameObject.name = name;
return self;
}
///
/// Set layer for the component's gameobject.
///
///
///
///
///
public static T Layer(this T self, int layer) where T : Component
{
self.gameObject.layer = layer;
return self;
}
///
/// Set layer for the component's gameobject.
///
///
///
///
///
public static T Layer(this T self, string layer) where T : Component
{
self.gameObject.layer = LayerMask.NameToLayer(layer);
return self;
}
///
/// Set tag for the component's gameobject.
///
///
///
///
///
public static T Tag(this T self, string tag) where T : Component
{
self.gameObject.tag = tag;
return self;
}
}