using Cysharp.Threading.Tasks;
using DG.Tweening;
using UnityEngine;
namespace PMaker.Extension
{
public static class GameObjectExtension
{
///
/// 显示游戏对象
///
///
public static void Visible(this GameObject go)
{
//go.transform.localScale = Vector3.one;
go.SetActive(true);
}
///
/// 隐藏游戏对象
///
///
public static void InVisible(this GameObject go)
{
//go.transform.localScale = Vector3.zero;
go.SetActive(false);
}
///
/// 设置游戏对象开闭
///
///
/// 显示或隐藏
public static void SetVisible(this GameObject go, bool value)
{
if (value)
{
go.Visible();
}
else
{
go.InVisible();
}
}
///
/// 检测游戏对象开闭
///
///
/// true:显示, false:隐藏
public static bool IsVisible(this GameObject go)
{
return go.activeSelf;
}
public static async UniTask Visible(this GameObject go, float duration)
{
var canvasGroup = go.ForceGetComponent();
canvasGroup.alpha = 0;
await canvasGroup.DOFade(1, duration);
}
public static async UniTask InVisible(this GameObject go, float duration)
{
var canvasGroup = go.ForceGetComponent();
canvasGroup.alpha = 1;
await canvasGroup.DOFade(0, duration);
}
///
/// 获取指定组件, 有则Get无则Add
///
///
///
///
public static T ForceGetComponent(this GameObject go) where T : Component
{
var component = go.GetComponent();
if (component == null)
{
component = go.AddComponent();
}
return component;
}
}
}