using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace TinaX
{
public static class GameObjectExtends
{
public static void Destroy(this GameObject selfObj)
{
GameObject.Destroy(selfObj);
}
///
/// Destroy self, with delay time
///
///
/// delay time
public static void Destroy(this GameObject selfObj, float t)
{
GameObject.Destroy(selfObj, t);
}
///
/// 显示
///
///
///
public static GameObject Show(this GameObject selfObj)
{
selfObj.SetActive(true);
return selfObj;
}
///
/// 隐藏
///
///
///
public static GameObject Hide(this GameObject selfObj)
{
selfObj.SetActive(false);
return selfObj;
}
///
/// 命名
///
///
///
///
public static GameObject Name(this GameObject selfObj, string name)
{
selfObj.name = name;
return selfObj;
}
///
/// 不要销毁
///
///
///
///
public static GameObject DontDestroy(this GameObject selfObj)
{
GameObject.DontDestroyOnLoad(selfObj);
return selfObj;
}
///
/// Dont Destory | 不要销毁
///
///
///
///
public static GameObject DontKillMe(this GameObject selfObj)
{
GameObject.DontDestroyOnLoad(selfObj);
return selfObj;
}
///
/// 获取Component,如果不存在就新建
///
///
///
public static T GetComponentOrAdd(this GameObject obj) where T : Component
{
var t = obj.GetComponent();
if (t == null)
{
t = obj.AddComponent();
}
return t;
}
///
/// 获取Component,如果不存在就新建
///
///
/// Component type
///
public static Component GetComponentOrAdd(this GameObject obj, System.Type type)
{
var c = obj.GetComponent(type);
if (c == null)
{
c = obj.AddComponent(type);
}
return c;
}
///
/// [链式]移除Component, 如果它存在的话
///
///
///
public static GameObject RemoveComponentIfExists(this GameObject obj) where T : Component
{
var t = obj.GetComponent();
if (t != null)
{
Object.Destroy(t);
}
return obj;
}
///
/// [链式]移除Component, 如果它存在的话
///
///
/// Component type
public static GameObject RemoveComponentIfExists(this GameObject obj, System.Type type)
{
var t = obj.GetComponent(type);
if (t != null)
{
Object.Destroy(t);
}
return obj;
}
///
/// [链式]移除Component, 如果它存在的话
///
///
/// Component type
public static GameObject RemoveComponentIfExists(this GameObject obj, string type)
{
var t = obj.GetComponent(type);
if (t != null)
{
Object.Destroy(t);
}
return obj;
}
///
/// [链式]移除全部相同的Components, 如果它们存在的话
///
///
///
public static GameObject RemoveComponentsIfExists(this GameObject obj) where T : Component
{
var t = obj.GetComponents();
for (var i = 0; i < t.Length; i++)
{
Object.Destroy(t[i]);
}
return obj;
}
public static GameObject RemoveComponentsIfExists(this GameObject obj, System.Type type)
{
var t = obj.GetComponents(type);
for (var i = 0; i < t.Length; i++)
{
Object.Destroy(t[i]);
}
return obj;
}
public static bool TryGetComponent(this GameObject obj , string typeName, out object component)
{
var c = obj.GetComponent(typeName);
if(c != null)
{
component = c;
return true;
}
component = null;
return false;
}
///
/// [链式]设置一个GameObject和它的所有子物体的Layer
///
///
///
public static GameObject SetLayerRecursive(this GameObject o, int layer)
{
SetLayerInternal(o.transform, layer);
return o;
}
private static void SetLayerInternal(Transform t, int layer)
{
t.gameObject.layer = layer;
foreach (Transform o in t)
{
SetLayerInternal(o, layer);
}
}
///
/// 在本帧直接销毁
///
///
public static void DestroyNow(this GameObject selfGo)
{
GameObject.DestroyImmediate(selfGo);
}
///
/// 获取或创建GameObject
///
///
public static GameObject FindOrCreateGameObject(this GameObject selfGo, string GameObjectName)
{
var trans = selfGo.transform.Find(GameObjectName);
if (trans == null)
{
var go = new GameObject(GameObjectName).SetParent(selfGo);
return go;
}
else
{
return trans.gameObject;
}
}
///
/// 获取或创建GameObject
///
///
public static GameObject FindOrCreateGo(this GameObject selfGo, string GameObjectName)
{
return selfGo.FindOrCreateGameObject(GameObjectName);
}
///
/// 获取或创建GameObject
///
///
public static GameObject FindOrCreateGameObject(this GameObject selfGo, string GameObjectName, params System.Type[] Components)
{
var trans = selfGo.transform.Find(GameObjectName);
if (trans == null)
{
var go = new GameObject(GameObjectName, Components).SetParent(selfGo);
return go;
}
else
{
return trans.gameObject;
}
}
public static GameObject CreateGameObject(this GameObject selfGo, string GameObjectName)
{
var go = new GameObject(GameObjectName).SetParent(selfGo);
return go;
}
public static GameObject CreateGameObject(this GameObject selfGo, string GameObjectName, params System.Type[] Components)
{
var go = new GameObject(GameObjectName, Components).SetParent(selfGo);
return go;
}
public static bool TryFindChildGameObject(this GameObject selfGo, string GameObjectName, out GameObject child)
{
if(selfGo.transform == null) { child = null; return false; }
var trans_child = selfGo.transform.Find(GameObjectName);
if(trans_child != null)
{
child = trans_child.gameObject;
return true;
}
else
{
child = null;
return false;
}
}
///
/// 设置父级GameObject
///
/// 返回自身
public static GameObject SetParent(this GameObject selfGo, GameObject parentGameObject)
{
selfGo.transform.SetParent(parentGameObject.transform);
return selfGo;
}
///
/// 设置世界坐标
///
///
public static GameObject SetPosition(this GameObject selfGo, Vector3 position)
{
if (selfGo.transform != null)
{
selfGo.transform.position = position;
}
return selfGo;
}
///
/// 设置本地坐标
///
///
public static GameObject SetLocalPosition(this GameObject selfGo, Vector3 localPosition)
{
if (selfGo.transform != null)
{
selfGo.transform.localPosition = localPosition;
}
return selfGo;
}
public static GameObject SetLocalScale(this GameObject selfGo, Vector3 scaleValue)
{
if (selfGo.transform != null)
{
selfGo.transform.localScale = scaleValue;
}
return selfGo;
}
public static GameObject SetRotation(this GameObject selfGo, Quaternion value)
{
if (selfGo.transform != null)
{
selfGo.transform.rotation = value;
}
return selfGo;
}
public static GameObject SetLocalRotation(this GameObject selfGo, Quaternion value)
{
if (selfGo.transform != null)
{
selfGo.transform.localRotation = value;
}
return selfGo;
}
public static GameObject SetEulerAngles(this GameObject selfGo, Vector3 value)
{
if (selfGo.transform != null)
{
selfGo.transform.eulerAngles = value;
}
return selfGo;
}
///
/// 检查组件是否存在
///
/// Game object
/// True when component is attached.
public static bool HasComponent(this GameObject gameObject) where T : Component
{
return gameObject.GetComponent() != null;
}
///
/// 检查组件是否存在
///
/// Game object
/// 组件类型
/// True when component is attached.
public static bool HasComponent(this GameObject gameObject, string type)
{
return gameObject.GetComponent(type) != null;
}
///
/// 检查组件是否存在
///
/// Game object
/// 组件类型
/// True when component is attached.
public static bool HasComponent(this GameObject gameObject, System.Type type)
{
return gameObject.GetComponent(type) != null;
}
///
/// 是否为空
///
///
///
public static bool IsNull(this GameObject go)
{
return go == null;
}
}
}