GameLoader.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System.Collections;
  2. using UnityEngine;
  3. public class GameLoader : MonoBehaviour
  4. {
  5. public string SceneName;
  6. public Texture2D Back;
  7. public Texture2D whitePixel;
  8. private void Start()
  9. {
  10. ImageFit(Back);
  11. StartCoroutine(LoadLevel());
  12. }
  13. public void OnGUI()
  14. {
  15. GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), whitePixel, ScaleMode.StretchToFill);
  16. GUI.DrawTexture(new Rect(x,y,width,height), Back,ScaleMode.StretchToFill);
  17. }
  18. private IEnumerator LoadLevel()
  19. {
  20. yield return new WaitForSeconds(5);
  21. Application.LoadLevelAsync(SceneName);
  22. }
  23. /*private AsyncOperation operation;
  24. [SerializeField]
  25. private UISprite progress;
  26. internal void Start ()
  27. {
  28. operation = Application.LoadLevelAsync(SceneName);
  29. }
  30. internal void Update()
  31. {
  32. if (operation != null)
  33. {
  34. progress.fillAmount = operation.progress;
  35. }
  36. }*/
  37. private float width;
  38. private float height;
  39. private float x;
  40. private float y;
  41. private void ImageFit(Texture2D texture, Vector2? placeSize = null)
  42. {
  43. var size = placeSize == null ? new Vector2(Screen.width, Screen.height) : placeSize.Value;
  44. //texture.MakePixelPerfect();
  45. var widthPercent = texture.width / (size.x / 100f);
  46. var heightPercent = texture.height / (size.y / 100f);
  47. if (widthPercent > heightPercent)
  48. {
  49. width = (int)((texture.width * 100) / widthPercent) * 0.8f;
  50. height = (int)((texture.height * 100) / widthPercent) * 0.8f;
  51. x = Screen.width * 0.1f;
  52. y = size.y/2f - height/2f;
  53. }
  54. else
  55. {
  56. width = (int)((texture.width * 100) / heightPercent) * 0.8f;
  57. height = (int)((texture.height * 100) / heightPercent) * 0.8f;
  58. x = (size.x / 2f - width / 2f) + Screen.width * 0.1f;;
  59. y = 0;
  60. }
  61. }
  62. }