AssetRequest.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using Object = UnityEngine.Object;
  6. public class AssetRequest : Reference,IEnumerator
  7. {
  8. private List<Object> _requires;
  9. public Type assetType;
  10. public LoadedCallback completed;
  11. public string name;
  12. AssetBundle assetbundle;
  13. public AssetRequest()
  14. {
  15. asset = null;
  16. LoadState = AssetLoadState.Init;
  17. }
  18. public AssetLoadState LoadState { get; protected set; }
  19. protected void Complete()
  20. {
  21. if(completed != null)
  22. {
  23. completed(this);
  24. completed = null;
  25. }
  26. }
  27. public virtual bool isDone
  28. {
  29. get { return LoadState == AssetLoadState.Loaded || LoadState == AssetLoadState.Unload; }
  30. }
  31. public virtual float progress
  32. {
  33. get { return 1; }
  34. }
  35. public virtual string error { get; protected set; }
  36. public string text { get; protected set; }
  37. public byte[] bytes { get; protected set; }
  38. public Object asset { get; internal set; }
  39. private bool checkRequires
  40. {
  41. get { return _requires != null; }
  42. }
  43. private void UpdateRequires()
  44. {
  45. for (var i = 0; i < _requires.Count; i++)
  46. {
  47. var item = _requires[i];
  48. if (item != null)
  49. continue;
  50. Release();
  51. _requires.RemoveAt(i);
  52. i--;
  53. }
  54. if (_requires.Count == 0)
  55. _requires = null;
  56. }
  57. internal virtual void Load()
  58. {
  59. #if UNITY_EDITOR
  60. LoadModule.loadDelegate = UnityEditor.AssetDatabase.LoadAssetAtPath;
  61. if (LoadModule.runtimeMode == false && LoadModule.loadDelegate != null)
  62. {
  63. asset = LoadModule.loadDelegate(name, assetType);
  64. }
  65. #else
  66. string[] path = name.Split('/');
  67. string[] icon = path[path.Length-1].Split('.');
  68. string pa = icon[0];
  69. //assetbundle = LoadModule.GetAssetBundle(pa);
  70. //if(assetbundle == null)
  71. //{
  72. // AssetBundle ab = AssetBundle.LoadFromFile(pa);//속潼渡齡竟AB관
  73. // if (ab != null)
  74. // {
  75. // LoadModule.AddAssetBundle(pa, ab);
  76. // }
  77. // assetbundle = ab;
  78. //}
  79. asset = MaterialsManager.Instance.LoadView(pa);
  80. //asset = assetbundle.LoadAsset<GameObject>(icon[icon.Length-1]);//속潼渡齡竟
  81. #endif
  82. if (asset == null)
  83. {
  84. error = "error! file not exist:" + name;
  85. Debug.LogError(error);
  86. return;
  87. }
  88. LoadState = AssetLoadState.Loaded;
  89. Complete();
  90. }
  91. internal virtual void Unload()
  92. {
  93. if (asset == null)
  94. return;
  95. if (!LoadModule.runtimeMode)
  96. if (!(asset is GameObject))
  97. Resources.UnloadAsset(asset);
  98. asset = null;
  99. LoadState = AssetLoadState.Unload;
  100. }
  101. internal virtual bool Update()
  102. {
  103. if (checkRequires)
  104. UpdateRequires();
  105. if (!isDone)
  106. return true;
  107. if (completed == null)
  108. return false;
  109. try
  110. {
  111. completed.Invoke(this);
  112. }
  113. catch (Exception ex)
  114. {
  115. Debug.LogException(ex);
  116. }
  117. completed = null;
  118. return false;
  119. }
  120. internal virtual void LoadImmediate() { }
  121. #region IEnumerator implementation
  122. public bool MoveNext()
  123. {
  124. return !isDone;
  125. }
  126. public void Reset()
  127. {
  128. }
  129. // Start is called before the first frame update
  130. void Start()
  131. {
  132. }
  133. // Update is called once per frame
  134. public object Current
  135. {
  136. get { return null; }
  137. }
  138. #endregion
  139. }