181 lines
5.2 KiB
C#
181 lines
5.2 KiB
C#
using System.Collections;
|
|
using AibisDream.Kit;
|
|
using DG.Tweening;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace AibisDream
|
|
{
|
|
public class MainUIController : Singleton<MainUIController>
|
|
{
|
|
private const string TVScene = "TV Scene";
|
|
private const string BlackImage = "Fade Image";
|
|
private const string NewsImage = "News";
|
|
private const string ObjPanel = "Show Obj";
|
|
|
|
#region ShowObj参数
|
|
|
|
[Header("Obj尺寸参数")]
|
|
public float objWidth;
|
|
public float objHeight;
|
|
|
|
#endregion
|
|
|
|
#region 主场景图片
|
|
|
|
private FadeImage _tvScene;
|
|
private FadeImage _blackImage;
|
|
private FadeImage _news;
|
|
private GameObject _objPanel;
|
|
private Image _objImage;
|
|
private Image _fullScreen;
|
|
|
|
#endregion
|
|
|
|
public override void OnSingletonInit()
|
|
{
|
|
InitChildObj();
|
|
}
|
|
|
|
#region 基本淡入淡出
|
|
|
|
public void FadeIn(float duration)
|
|
{
|
|
_blackImage.FadeIn(duration);
|
|
}
|
|
|
|
public IEnumerator FadeInSync(float duration)
|
|
{
|
|
return _blackImage.FadeInSync(duration);
|
|
}
|
|
|
|
public void FadeOut(float duration)
|
|
{
|
|
_blackImage.FadeOut(duration);
|
|
}
|
|
|
|
public IEnumerator FadeOutSync(float duration)
|
|
{
|
|
return _blackImage.FadeOutSync(duration);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region TV场景淡入淡出
|
|
|
|
public void TVFadeIn(float duration)
|
|
{
|
|
_tvScene.FadeIn(duration);
|
|
_news.FadeIn(duration);
|
|
}
|
|
|
|
public IEnumerator TVFadeInSync(float duration)
|
|
{
|
|
_news.FadeIn(duration);
|
|
return _tvScene.FadeInSync(duration);
|
|
}
|
|
|
|
public void TVFadeOut(float duration)
|
|
{
|
|
_tvScene.FadeOut(duration);
|
|
_news.FadeOut(duration);
|
|
}
|
|
|
|
public IEnumerator TVFadeOutSync(float duration)
|
|
{
|
|
_news.FadeOut(duration);
|
|
return _tvScene.FadeOutSync(duration);
|
|
}
|
|
|
|
public void SwitchTV(string newsName)
|
|
{
|
|
_news.SwitchImage($"Art/Background/{newsName}");
|
|
}
|
|
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// 隐藏整个UI
|
|
/// </summary>
|
|
public void HideMainUI()
|
|
{
|
|
gameObject.SetActive(false);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 显示物体
|
|
/// </summary>
|
|
/// <returns>协程</returns>
|
|
public IEnumerator ShowObj(string picName)
|
|
{
|
|
_objPanel.gameObject.SetActive(true);
|
|
|
|
// 切换图片
|
|
var newSprite = Resources.Load<Sprite>($"Art/Obj/{picName}");
|
|
// 获取图片的长宽比
|
|
var imageAspect = (float)newSprite.texture.width / newSprite.texture.height;
|
|
|
|
// 计算新的尺寸
|
|
if (imageAspect > 1) // 宽图
|
|
{
|
|
var newHeight = objWidth / imageAspect;
|
|
_objImage.rectTransform.sizeDelta = new Vector2(objWidth, newHeight);
|
|
}
|
|
else // 高图
|
|
{
|
|
var newWidth = objHeight * imageAspect;
|
|
_objImage.rectTransform.sizeDelta = new Vector2(newWidth, objHeight);
|
|
}
|
|
|
|
// 淡入
|
|
_objImage.sprite = newSprite;
|
|
_objImage.gameObject.SetActive(true);
|
|
yield return _objImage.DOBlendableColor(Color.white, 1).WaitForCompletion();
|
|
}
|
|
|
|
public IEnumerator ShowFullScreen(string picName)
|
|
{
|
|
_fullScreen.sprite = Resources.Load<Sprite>($"Art/Obj/{picName}");
|
|
|
|
// 淡入
|
|
_fullScreen.gameObject.SetActive(true);
|
|
yield return _objImage.DOBlendableColor(Color.white, 1).WaitForCompletion();
|
|
}
|
|
|
|
public IEnumerator HideFullScreen()
|
|
{
|
|
_fullScreen.gameObject.SetActive(true);
|
|
yield return _objImage.DOBlendableColor(Color.clear, 1).WaitForCompletion();
|
|
_fullScreen.gameObject.SetActive(false);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 隐藏物体
|
|
/// </summary>
|
|
/// <returns>协程</returns>
|
|
public IEnumerator HideObj()
|
|
{
|
|
_objImage.gameObject.SetActive(true);
|
|
yield return _objImage.DOBlendableColor(Color.clear, 1).WaitForCompletion();
|
|
_objImage.gameObject.SetActive(false);
|
|
_objPanel.SetActive(false);
|
|
}
|
|
|
|
private void InitChildObj()
|
|
{
|
|
var rawTvScene = transform.Find(TVScene).gameObject.GetComponent<Image>();
|
|
_tvScene = new FadeImage(rawTvScene);
|
|
|
|
var rawBlackImage = transform.Find(BlackImage).gameObject.GetComponent<Image>();
|
|
_blackImage = new FadeImage(rawBlackImage);
|
|
|
|
var rawNews = transform.Find(NewsImage).gameObject.GetComponent<Image>();
|
|
_news = new FadeImage(rawNews);
|
|
|
|
_objPanel = transform.Find(ObjPanel).gameObject;
|
|
_objImage = _objPanel.transform.GetChild(0).GetComponent<Image>();
|
|
|
|
_fullScreen = transform.Find("Full Screen Image").GetComponent<Image>();
|
|
}
|
|
}
|
|
} |