using System.Collections.Generic;
using UnityEngine;
public class UiBase :MonoBehaviour
{
//public RectTransform targetRect;
// public CanvasGroup canvasGroup;
protected string basePath = "Prefabs/";// 图片在Resources中的基础路径
///
/// 关闭UI界面
///
public void ClosePanel()
{
this.gameObject.SetActive(false);
}
///
/// 打开UI界面
///
protected void OpenPanel()
{
this .gameObject.SetActive(true);
}
///
/// 递归查找物体,并且关闭
///
///
///
///
protected void CloseFindDeepChildUI(string childName)
{
// 使用递归查找失活的 childName
Transform item = FindDeepChild(transform, childName);
if (item != null)
{
Debug.Log("找到失活状态的 childName");
GameObject gochildName = item.gameObject;
// 如果需要激活物体,可以使用以下代码:
gochildName.SetActive(false);
}
else
{
Debug.LogError("未找到 childName,请检查路径是否正确");
}
}
///
/// 递归查找物体
///
///
///
///
protected Transform FindDeepChild(Transform parent, string childName)
{
// 递归查找失活物体
foreach (Transform child in parent)
{
if (child.name == childName)
{
GameObject item = child.gameObject;
return child;
}
Transform result = FindDeepChild(child, childName);
if (result != null)
return result;
}
return null;
}
///
/// 递归查找物体,并且打开
///
///
///
///
protected void OPenFindDeepChildUI(string childName)
{
// 使用递归查找失活的 childName
Transform item = FindDeepChild(transform, childName);
if (item != null)
{
GameObject gochildName = item.gameObject;
Debug.Log("找到失活状态的 childName" + gochildName);
// 如果需要激活物体,可以使用以下代码:
gochildName.gameObject.SetActive(true);
}
else
{
Debug.LogError("未找到 childName,请检查路径是否正确");
}
}
///
/// 加载Sprite
///
///
///
protected Sprite ResSprit(string spritePath)
{
Sprite go = Resources.Load(spritePath);
return go;
}
///
/// 实例化UI
///
/// UI路径
protected void LoadUI(string prefabsPath)
{
// 如果预制体模板未加载,则从 Resources 加载
string fullPath = basePath + prefabsPath;
GameObject prefab = Resources.Load(fullPath);
GameObject newUIPanel = Instantiate(prefab);
// 设置父级,默认为 `UIManager.Instance.goEmptyUI`,如果传入了 `parentTransform`,则使用它
newUIPanel.transform.SetParent(GameManager.Instance.goParent.transform);
newUIPanel.transform.localScale = Vector3.one;
// 如果没有传递位置参数,则使用默认位置
newUIPanel.transform.localPosition= Vector3.zero;
}
/////
///// 播放半面UI的动画
/////
//public void ActivateHalfUI(float fadeDuration, float scaleDuration, float moveDuration,Vector2 moveStartPosition, Vector2 moveEndPosition)
//{
// if (targetRect != null)
// {
// // 创建一个 DOTween 序列
// Sequence sequence = DOTween.Sequence();
// // 设置起始位置
// targetRect.anchoredPosition = moveStartPosition;
// // 移动动画:从指定的起始位置移动到目标位置
// sequence.Join(targetRect.DOAnchorPos(moveEndPosition, moveDuration).SetEase(Ease.OutCubic));
// // 透明度动画:从 0 到 1
// if (canvasGroup != null)
// {
// canvasGroup.alpha = 0f;
// sequence.Join(canvasGroup.DOFade(1f, fadeDuration)); // 渐变到透明度 1
// }
// // 缩放动画:从 0 到 1
// sequence.Join(targetRect.DOScale(Vector3.one, scaleDuration).SetEase(Ease.OutBack));
// // 执行所有动画
// sequence.Play();
// }
// else
// {
// Debug.LogError("Target object does not have a RectTransform or CanvasGroup.");
// }
//}
/////
///// 关闭半面UI动画
/////
//public void DeactivateHalfUI(float fadeDuration, float scaleDuration, float moveDuration, Vector2 moveStartPosition, Vector2 moveEndPosition)
//{
// if (targetRect != null)
// {
// Sequence sequence = DOTween.Sequence();
// //sequence.Append(targetRect.DOScale(Vector3.zero, scaleDuration).SetEase(Ease.OutBack));
// sequence.Join(targetRect.DOMove(new Vector2(0, -1920), moveDuration).SetEase(Ease.InOutQuart));
// sequence.Join(canvasGroup.DOFade(0, fadeDuration).SetEase(Ease.OutBack));
// sequence.OnKill(() => {
// gameObject.SetActive(false);
// });
// }
// else
// {
// Debug.LogError("Target object does not have a RectTransform or CanvasGroup.");
// }
//}
public void ClearAllChildren(Transform parent)
{
foreach (Transform child in parent)
{
GameObject.Destroy(child.gameObject);
}
}
}