You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

145 line
3.3 KiB

  1. using System;
  2. using Inno.DLC;
  3. using SUISS.Core;
  4. using SUISSEngine;
  5. using UnityEngine;
  6. public class JPEGLoader : MonoBehaviour
  7. {
  8. public static int TextureScalingFactor
  9. {
  10. get
  11. {
  12. if (JPEGLoader.MemorySize < 1024 || JPEGLoader.MaxTextureSize < 4096)
  13. {
  14. return 4;
  15. }
  16. if (JPEGLoader.MemorySize > 1500)
  17. {
  18. return 1;
  19. }
  20. return 2;
  21. }
  22. }
  23. public void DownloadCompleted(string group, string name)
  24. {
  25. if (group == this.dlcGroup && name == this.dlcName)
  26. {
  27. if (this.spriteRenderer != null)
  28. {
  29. this.spriteRenderer.sprite = null;
  30. }
  31. this.OnDestroy();
  32. this.Awake();
  33. }
  34. }
  35. protected void Awake()
  36. {
  37. byte[] array = null;
  38. if (!string.IsNullOrEmpty(this.dlcGroup) && !string.IsNullOrEmpty(this.dlcName))
  39. {
  40. DLCManager dlcmanager = ServiceLocator.Find<DLCManager>();
  41. if (dlcmanager != null && dlcmanager.IsEnabled)
  42. {
  43. DLCGroup dlcgroup = dlcmanager.GetGroup(this.dlcGroup).FileSelection("Type", "Background");
  44. if (dlcgroup.IsDownloaded)
  45. {
  46. DLCFile fileWithName = dlcgroup.GetFileWithName(this.dlcName);
  47. if (fileWithName != null)
  48. {
  49. array = fileWithName.GetContents();
  50. }
  51. else
  52. {
  53. UnityEngine.Debug.LogError(string.Format("No file with name {0} in group {1}", this.dlcName, this.dlcGroup));
  54. }
  55. }
  56. }
  57. else
  58. {
  59. UnityEngine.Debug.LogWarning("No DLC manager");
  60. }
  61. }
  62. if (array == null)
  63. {
  64. TextAsset textAsset = Resources.Load(this.jpegFileResourcePath) as TextAsset;
  65. array = textAsset.bytes;
  66. Resources.UnloadAsset(textAsset);
  67. }
  68. float pixelsPerUnit = 1f;
  69. this._texture = null;
  70. if (this._texture == null)
  71. {
  72. this._texture = new Texture2D(0, 0, TextureFormat.RGB24, false);
  73. this._texture.wrapMode = TextureWrapMode.Clamp;
  74. this._texture.LoadImage(array);
  75. this._texture.Apply(false, true);
  76. }
  77. this._sprite = Sprite.Create(this._texture, new Rect(0f, 0f, (float)this._texture.width, (float)this._texture.height), new Vector2(0f, 1f), pixelsPerUnit);
  78. if (this.spriteRenderer != null)
  79. {
  80. this.spriteRenderer.sprite = this._sprite;
  81. }
  82. else
  83. {
  84. UnityEngine.Debug.LogError(string.Format("JPEGLoading at {0} should have SpriteRenderer or UI2DSprite", base.name));
  85. }
  86. }
  87. protected void OnDestroy()
  88. {
  89. UnityEngine.Object.DestroyImmediate(this._sprite);
  90. UnityEngine.Object.DestroyImmediate(this._texture);
  91. }
  92. protected static int MemorySize
  93. {
  94. get
  95. {
  96. if (JPEGLoader._memorySize < 0)
  97. {
  98. JPEGLoader._memorySize = SystemInfo.graphicsMemorySize + SystemInfo.systemMemorySize;
  99. }
  100. return JPEGLoader._memorySize;
  101. }
  102. }
  103. protected static int MaxTextureSize
  104. {
  105. get
  106. {
  107. if (JPEGLoader._maxTextureSize < 0)
  108. {
  109. JPEGLoader._maxTextureSize = SystemInfo.maxTextureSize;
  110. }
  111. return JPEGLoader._maxTextureSize;
  112. }
  113. }
  114. [SerializeField]
  115. protected string jpegFileResourcePath;
  116. [SerializeField]
  117. [SelfReference(true)]
  118. protected SpriteRenderer spriteRenderer;
  119. [SerializeField]
  120. protected bool forceHighQuality;
  121. [SerializeField]
  122. protected string dlcGroup;
  123. [SerializeField]
  124. protected string dlcName;
  125. protected Texture2D _texture;
  126. protected Sprite _sprite;
  127. protected static int _memorySize = -1;
  128. protected static int _maxTextureSize = -1;
  129. }