203 lines
5.8 KiB
C#
203 lines
5.8 KiB
C#
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
public class UiBase :MonoBehaviour
|
||
{
|
||
//public RectTransform targetRect;
|
||
// public CanvasGroup canvasGroup;
|
||
|
||
protected string basePath = "Prefabs/";// 图片在Resources中的基础路径
|
||
|
||
/// <summary>
|
||
/// 关闭UI界面
|
||
/// </summary>
|
||
public void ClosePanel()
|
||
{
|
||
this.gameObject.SetActive(false);
|
||
}
|
||
/// <summary>
|
||
/// 打开UI界面
|
||
/// </summary>
|
||
protected void OpenPanel()
|
||
{
|
||
this .gameObject.SetActive(true);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 递归查找物体,并且关闭
|
||
/// </summary>
|
||
/// <param name="parent"></param>
|
||
/// <param name="childName"></param>
|
||
/// <returns></returns>
|
||
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,请检查路径是否正确");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 递归查找物体
|
||
/// </summary>
|
||
/// <param name="parent"></param>
|
||
/// <param name="childName"></param>
|
||
/// <returns></returns>
|
||
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;
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 递归查找物体,并且打开
|
||
/// </summary>
|
||
/// <param name="parent"></param>
|
||
/// <param name="childName"></param>
|
||
/// <returns></returns>
|
||
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,请检查路径是否正确");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 加载Sprite
|
||
/// </summary>
|
||
/// <param name="spritePath"></param>
|
||
/// <returns></returns>
|
||
protected Sprite ResSprit(string spritePath)
|
||
{
|
||
|
||
Sprite go = Resources.Load<Sprite>(spritePath);
|
||
return go;
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 实例化UI
|
||
/// </summary>
|
||
/// <param name="prefabsPath">UI路径</param>
|
||
protected void LoadUI(string prefabsPath)
|
||
{
|
||
|
||
|
||
// 如果预制体模板未加载,则从 Resources 加载
|
||
|
||
string fullPath = basePath + prefabsPath;
|
||
GameObject prefab = Resources.Load<GameObject>(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;
|
||
}
|
||
///// <summary>
|
||
///// 播放半面UI的动画
|
||
///// </summary>
|
||
//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.");
|
||
// }
|
||
//}
|
||
|
||
///// <summary>
|
||
///// 关闭半面UI动画
|
||
///// </summary>
|
||
//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);
|
||
}
|
||
}
|
||
}
|