UnityTextureAtlasData.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /**
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2012-2017 DragonBones team and other contributors
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  7. * this software and associated documentation files (the "Software"), to deal in
  8. * the Software without restriction, including without limitation the rights to
  9. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  10. * the Software, and to permit persons to whom the Software is furnished to do so,
  11. * subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all
  14. * copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. using System.Collections.Generic;
  24. using UnityEngine;
  25. namespace DragonBones
  26. {
  27. /**
  28. * @language zh_CN
  29. * Unity 贴图集数据。
  30. * @version DragonBones 3.0
  31. */
  32. public class UnityTextureAtlasData : TextureAtlasData
  33. {
  34. /**
  35. * @private
  36. */
  37. internal bool _disposeEnabled;
  38. /**
  39. * @language zh_CN
  40. * Unity 贴图。
  41. * @version DragonBones 3.0
  42. */
  43. public Material texture;
  44. public Material uiTexture;
  45. /**
  46. * @private
  47. */
  48. public UnityTextureAtlasData()
  49. {
  50. }
  51. /**
  52. * @private
  53. */
  54. protected override void _OnClear()
  55. {
  56. base._OnClear();
  57. if (_disposeEnabled && texture != null)
  58. {
  59. UnityFactoryHelper.DestroyUnityObject(texture);
  60. }
  61. if (_disposeEnabled && uiTexture != null)
  62. {
  63. UnityFactoryHelper.DestroyUnityObject(uiTexture);
  64. }
  65. _disposeEnabled = false;
  66. texture = null;
  67. uiTexture = null;
  68. }
  69. /**
  70. * @private
  71. */
  72. public override TextureData CreateTexture()
  73. {
  74. return BaseObject.BorrowObject<UnityTextureData>();
  75. }
  76. }
  77. /// <private/>
  78. internal class UnityTextureData : TextureData
  79. {
  80. public const string SHADER_PATH = "Shaders/";
  81. public const string SHADER_GRAP = "DB_BlendMode_Grab";
  82. public const string SHADER_FRAME_BUFFER = "DB_BlendMode_Framebuffer";
  83. public const string UI_SHADER_GRAP = "DB_BlendMode_UIGrab";
  84. public const string UI_SHADER_FRAME_BUFFER = "DB_BlendMode_UIFramebuffer";
  85. /// <summary>
  86. /// 叠加模式材质球的缓存池
  87. /// </summary>
  88. internal Dictionary<string, Material> _cacheBlendModeMats = new Dictionary<string, Material>();
  89. public UnityTextureData()
  90. {
  91. }
  92. protected override void _OnClear()
  93. {
  94. base._OnClear();
  95. foreach (var key in this._cacheBlendModeMats.Keys)
  96. {
  97. var mat = this._cacheBlendModeMats[key];
  98. if (mat != null)
  99. {
  100. UnityFactoryHelper.DestroyUnityObject(mat);
  101. }
  102. //this._cacheBlendModeMats[key] = null;
  103. }
  104. //
  105. this._cacheBlendModeMats.Clear();
  106. }
  107. private Material _GetMaterial(BlendMode blendMode)
  108. {
  109. //normal model, return the parent shareMaterial
  110. if (blendMode == BlendMode.Normal)
  111. {
  112. return (this.parent as UnityTextureAtlasData).texture;
  113. }
  114. var blendModeStr = blendMode.ToString();
  115. if (this._cacheBlendModeMats.ContainsKey(blendModeStr))
  116. {
  117. return this._cacheBlendModeMats[blendModeStr];
  118. }
  119. //framebuffer won't work in the editor mode
  120. #if UNITY_EDITOR
  121. var newMaterial = new Material(Resources.Load<Shader>(SHADER_PATH + SHADER_GRAP));
  122. #else
  123. var newMaterial = new Material(Resources.Load<Shader>(SHADER_PATH + SHADER_GRAP));
  124. #endif
  125. var texture = (this.parent as UnityTextureAtlasData).texture.mainTexture;
  126. newMaterial.name = texture.name + "_" + SHADER_GRAP + "_Mat";
  127. newMaterial.hideFlags = HideFlags.HideAndDontSave;
  128. newMaterial.mainTexture = texture;
  129. this._cacheBlendModeMats.Add(blendModeStr, newMaterial);
  130. return newMaterial;
  131. }
  132. private Material _GetUIMaterial(BlendMode blendMode)
  133. {
  134. //normal model, return the parent shareMaterial
  135. if (blendMode == BlendMode.Normal)
  136. {
  137. return (this.parent as UnityTextureAtlasData).uiTexture;
  138. }
  139. var blendModeStr = "UI_" + blendMode.ToString();
  140. if (this._cacheBlendModeMats.ContainsKey(blendModeStr))
  141. {
  142. return this._cacheBlendModeMats[blendModeStr];
  143. }
  144. //framebuffer won't work in the editor mode
  145. #if UNITY_EDITOR
  146. var newMaterial = new Material(Resources.Load<Shader>(SHADER_PATH + UI_SHADER_GRAP));
  147. #else
  148. var newMaterial = new Material(Resources.Load<Shader>(SHADER_PATH + UI_SHADER_GRAP));
  149. #endif
  150. var texture = (this.parent as UnityTextureAtlasData).uiTexture.mainTexture;
  151. newMaterial.name = texture.name + "_" + SHADER_GRAP + "_Mat";
  152. newMaterial.hideFlags = HideFlags.HideAndDontSave;
  153. newMaterial.mainTexture = texture;
  154. this._cacheBlendModeMats.Add(blendModeStr, newMaterial);
  155. return newMaterial;
  156. }
  157. internal Material GetMaterial(BlendMode blendMode, bool isUGUI = false)
  158. {
  159. if (isUGUI)
  160. {
  161. return _GetUIMaterial(blendMode);
  162. }
  163. else
  164. {
  165. return _GetMaterial(blendMode);
  166. }
  167. }
  168. public override void CopyFrom(TextureData value)
  169. {
  170. base.CopyFrom(value);
  171. //
  172. (value as UnityTextureData)._cacheBlendModeMats = this._cacheBlendModeMats;
  173. }
  174. }
  175. }