126 lines
4.3 KiB
C#
126 lines
4.3 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class FirstSceneBg : MonoBehaviour
|
|
{
|
|
private RawImage _bg;
|
|
private RawImage _logo;
|
|
private float _publishDelta;
|
|
private Text _publishText;
|
|
private Text _tip;
|
|
private Text _gameVersion;
|
|
public static FirstSceneBg instance { get; private set; }
|
|
|
|
private void Awake()
|
|
{
|
|
instance = this;
|
|
DontDestroyOnLoad(gameObject);
|
|
_bg = transform.Find("BG").GetComponent<RawImage>();
|
|
_logo = transform.Find("Logo").GetComponent<RawImage>();
|
|
_tip = transform.Find("DownloadTip/Text").GetComponent<Text>();
|
|
_publishText = transform.Find("PublishText/Text").GetComponent<Text>();
|
|
_gameVersion = transform.Find("Version").GetComponent<Text>();
|
|
var publishBg = (RectTransform) _publishText.rectTransform.parent;
|
|
_publishDelta = publishBg.sizeDelta.y - _publishText.preferredHeight;
|
|
publishBg.gameObject.SetActive(false);
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
AssetUpdateDownloaderTick.instance.LoadStreamingAsset("ExTextures/PacketRes/LoginBG.jpg", OnBgLoad);
|
|
AssetUpdateDownloaderTick.instance.LoadStreamingAsset("ExTextures/PacketRes/Logo.png", OnLogoLoad);
|
|
// 适配健康游戏忠告
|
|
var healthy = (RectTransform)transform.Find("HealthyGaming");
|
|
var healthyText = healthy.Find("Text").GetComponent<Text>();
|
|
var sizeDelta = healthy.sizeDelta;
|
|
sizeDelta.y = healthyText.preferredHeight + 10f;
|
|
healthy.sizeDelta = sizeDelta;
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
instance = null;
|
|
}
|
|
|
|
public static void CreateInstance()
|
|
{
|
|
var prefab = Resources.Load<GameObject>("Prefab/FirstSceneBg");
|
|
if (prefab)
|
|
{
|
|
var item = Instantiate(prefab, Vector3.zero, Quaternion.identity);
|
|
var firstBg = item.AddComponent<FirstSceneBg>();
|
|
firstBg.SetTip("检查资源版本中...");
|
|
}
|
|
}
|
|
|
|
public void ShowTip(bool isShow)
|
|
{
|
|
_tip.transform.parent.gameObject.SetActive(isShow);
|
|
}
|
|
|
|
public void ShowGameVersion()
|
|
{
|
|
_gameVersion.text = string.Format("游戏版本:{0}.{1}", Application.version, AssetUpdateManager.LoadCurrentVersion());
|
|
float posY;
|
|
if (_publishText.gameObject.activeInHierarchy)
|
|
{
|
|
var publishBg = (RectTransform) _publishText.rectTransform.parent;
|
|
var sizeDelta = publishBg.sizeDelta;
|
|
posY = sizeDelta.y;
|
|
}
|
|
else
|
|
posY = 0f;
|
|
var rectTransform = _gameVersion.rectTransform;
|
|
var pos = rectTransform.anchoredPosition;
|
|
pos.y = posY;
|
|
rectTransform.anchoredPosition = pos;
|
|
}
|
|
|
|
public void SetTip(string text)
|
|
{
|
|
_tip.text = text;
|
|
}
|
|
|
|
public void SetPublishText(string text)
|
|
{
|
|
_publishText.text = text;
|
|
// 试图适配屏幕Dpi
|
|
var canvasRect = (RectTransform)_publishText.rectTransform.root;
|
|
var fontSize = Mathf.CeilToInt(canvasRect.sizeDelta.y * (20f / 720f));
|
|
_publishText.fontSize = fontSize;
|
|
var publishBg = (RectTransform) _publishText.rectTransform.parent;
|
|
var sizeDelta = publishBg.sizeDelta;
|
|
sizeDelta.y = _publishText.preferredHeight + _publishDelta;
|
|
publishBg.sizeDelta = sizeDelta;
|
|
publishBg.gameObject.SetActive(true);
|
|
}
|
|
|
|
private void OnBgLoad(WWW www)
|
|
{
|
|
Texture2D texture = null;
|
|
if (string.IsNullOrEmpty(www.error))
|
|
texture = www.texture;
|
|
if (texture)
|
|
{
|
|
_bg.texture = texture;
|
|
_bg.color = Color.white;
|
|
var rootSize = ((RectTransform) transform.root).sizeDelta;
|
|
var ratio = Mathf.Max(rootSize.x / texture.width , rootSize.y / texture.height);
|
|
var sizeDelta = new Vector2(texture.width, texture.height) * ratio + Vector2.one * 5f;
|
|
_bg.rectTransform.sizeDelta = sizeDelta;
|
|
}
|
|
}
|
|
|
|
private void OnLogoLoad(WWW www)
|
|
{
|
|
Texture2D texture = null;
|
|
if (string.IsNullOrEmpty(www.error))
|
|
texture = www.texture;
|
|
if (texture)
|
|
{
|
|
_logo.texture = texture;
|
|
_logo.color = Color.white;
|
|
_logo.rectTransform.sizeDelta = new Vector2(texture.width, texture.height);
|
|
}
|
|
}
|
|
} |