Sprite3D.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  1. using UnityEngine;
  2. using System.Collections;
  3. public class Sprite3D : MonoBehaviour
  4. {
  5. [System.Serializable]
  6. public enum EnumVertical
  7. {
  8. Top,
  9. Middle,
  10. Bottom
  11. }
  12. [System.Serializable]
  13. public enum EnumHoriontal
  14. {
  15. Left,
  16. Center,
  17. Right
  18. }
  19. [System.Serializable]
  20. public enum EnumFillType
  21. {
  22. Horiontal,
  23. Vertical,
  24. Radial360
  25. }
  26. [System.Serializable]
  27. public enum RadialPhaseType
  28. {
  29. LeftUp = 0,
  30. RightUp = 1,
  31. RightDown = 2,
  32. LeftDown = 3
  33. }
  34. public struct SpriteAttr
  35. {
  36. public int x;
  37. public int y;
  38. public int width;
  39. public int height;
  40. }
  41. public static readonly int TRANSPARENT_RENDER_QUEUE = 3000;
  42. [UnityEngine.SerializeField]
  43. private bool m_useAtlas = true;
  44. [UnityEngine.SerializeField]
  45. private AtlasInfo m_atlas;
  46. [UnityEngine.SerializeField]
  47. private string m_spriteName;
  48. [System.NonSerialized]
  49. private string m_lastAtlasName;
  50. [UnityEngine.SerializeField]
  51. private Texture2D m_texture;
  52. [UnityEngine.SerializeField]
  53. private string m_tag;
  54. [UnityEngine.SerializeField]
  55. public bool compress = true;
  56. [UnityEngine.SerializeField]
  57. public int padding = 0;
  58. [System.NonSerialized]
  59. private SpriteAttr m_spriteAttr;
  60. [System.NonSerialized]
  61. public int m_textureGUID;
  62. [UnityEngine.SerializeField]
  63. private float m_width = 1f;
  64. [UnityEngine.SerializeField]
  65. private float m_height = 1f;
  66. [UnityEngine.SerializeField]
  67. private EnumVertical m_alignVertical = EnumVertical.Middle;
  68. [UnityEngine.SerializeField]
  69. private EnumHoriontal m_alignHoriontal = EnumHoriontal.Center;
  70. [UnityEngine.SerializeField]
  71. private EnumFillType m_fillType = EnumFillType.Horiontal;
  72. [UnityEngine.SerializeField]
  73. private float m_fillAmount = 1.0f;
  74. [UnityEngine.SerializeField]
  75. private Color m_color = Color.white;
  76. [UnityEngine.SerializeField]
  77. private float m_depth = 1;
  78. //给扇形用的
  79. [UnityEngine.SerializeField]
  80. private uint m_segments = 50;
  81. [UnityEngine.SerializeField]
  82. private RadialPhaseType m_phaseType = RadialPhaseType.LeftUp;
  83. [System.NonSerialized]
  84. private bool m_propchanged = true;
  85. [System.NonSerialized]
  86. private Mesh m_mesh = null;
  87. [System.NonSerialized]
  88. private MeshRenderer m_render = null;
  89. [System.NonSerialized]
  90. private AtlasInfo.UVDetail m_uv = null;
  91. void Awake()
  92. {
  93. m_lastAtlasName = null;
  94. m_propchanged = true;
  95. m_depth = Mathf.Max(1, m_depth);
  96. m_mesh = null;
  97. PrepareMesh();
  98. RefreshUVDetail();
  99. }
  100. void Start()
  101. {
  102. Canvas3DImpl.GetInstance().registerSprite3D(this);
  103. Canvas3DImpl.GetInstance().registerAutoAtlas(this);
  104. RefreshAtlasMaterial();
  105. }
  106. void OnDestroy()
  107. {
  108. m_atlas = null;
  109. Canvas3DImpl.GetInstance().unregisterSprite3d(this);
  110. Canvas3DImpl.GetInstance().unregisterAutoAtlas(this);
  111. }
  112. void OnEnable()
  113. {
  114. Canvas3DImpl.GetInstance().RefreshLayout();
  115. }
  116. void Update()
  117. {
  118. }
  119. void LateUpdate()
  120. {
  121. if (m_propchanged == true)
  122. {
  123. GenerateMesh();
  124. m_propchanged = false;
  125. }
  126. }
  127. #region Properties
  128. public bool useAtlas
  129. {
  130. get
  131. {
  132. return m_useAtlas;
  133. }
  134. set
  135. {
  136. if (value == m_useAtlas)
  137. {
  138. return;
  139. }
  140. m_useAtlas = value;
  141. if (m_useAtlas)
  142. {
  143. m_texture = null;
  144. }
  145. else
  146. {
  147. m_atlas = null;
  148. }
  149. }
  150. }
  151. public float fillAmount
  152. {
  153. get
  154. {
  155. return m_fillAmount;
  156. }
  157. set
  158. {
  159. if (m_fillAmount == value) return; m_fillAmount = value; m_propchanged = true;
  160. }
  161. }
  162. public Color color
  163. {
  164. get
  165. {
  166. return m_color;
  167. }
  168. set
  169. {
  170. if (m_color == value) return; m_color = value; m_propchanged = true;
  171. }
  172. }
  173. public float Alpha
  174. {
  175. get { return m_color.a; }
  176. set
  177. {
  178. if (m_color.a == value) return;
  179. m_color.a = value; m_propchanged = true;
  180. }
  181. }
  182. public float depth
  183. {
  184. get
  185. {
  186. return m_depth;
  187. }
  188. set
  189. {
  190. m_depth = value;
  191. RecaculateDepth();
  192. }
  193. }
  194. public AtlasInfo atlas
  195. {
  196. get
  197. {
  198. return m_atlas;
  199. }
  200. set
  201. {
  202. if (m_atlas == value)
  203. return;
  204. m_atlas = value;
  205. useAtlas = true;
  206. m_propchanged = true;
  207. }
  208. }
  209. public string spriteName
  210. {
  211. get
  212. {
  213. return m_spriteName;
  214. }
  215. set
  216. {
  217. if (m_spriteName == value)
  218. return;
  219. m_spriteName = value;
  220. m_propchanged = true;
  221. }
  222. }
  223. public Texture2D texture
  224. {
  225. get
  226. {
  227. return m_texture;
  228. }
  229. set
  230. {
  231. if (m_texture == value)
  232. return;
  233. m_texture = value;
  234. useAtlas = false;
  235. Canvas3DImpl.GetInstance().registerAutoAtlas(this);
  236. m_propchanged = true;
  237. #if UNITY_EDITOR
  238. if (!UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode)
  239. {
  240. GenerateMesh();
  241. }
  242. #endif
  243. }
  244. }
  245. public int textureWidth
  246. {
  247. get
  248. {
  249. if (m_uv == null)
  250. {
  251. if (m_texture != null)
  252. {
  253. return m_texture.width;
  254. }
  255. return 0;
  256. }
  257. return m_uv.width;
  258. }
  259. }
  260. public int textureHeight
  261. {
  262. get
  263. {
  264. if (m_uv == null)
  265. {
  266. if (m_texture != null)
  267. {
  268. return m_texture.height;
  269. }
  270. return 0;
  271. }
  272. return m_uv.height;
  273. }
  274. }
  275. public float HalfTextureWidth
  276. {
  277. get { return textureWidth * 0.5f; }
  278. }
  279. public float HalfTextureHeight
  280. {
  281. get { return textureHeight * 0.5f; }
  282. }
  283. public RadialPhaseType PhaseType
  284. {
  285. get
  286. {
  287. return m_phaseType;
  288. }
  289. set
  290. {
  291. m_phaseType = value;
  292. }
  293. }
  294. public void SetAutoAtlas(Texture2D atlas, AtlasInfo.UVDetail uv)
  295. {
  296. m_texture = atlas;
  297. SetUV(uv);
  298. }
  299. public string autoAtlasTag
  300. {
  301. get
  302. {
  303. if (string.IsNullOrEmpty(m_tag))
  304. {
  305. return m_tag;
  306. }
  307. return m_tag.Trim();
  308. }
  309. set
  310. {
  311. if (m_tag == value)
  312. return;
  313. m_tag = value;
  314. Canvas3DImpl.GetInstance().registerAutoAtlas(this);
  315. m_propchanged = true;
  316. }
  317. }
  318. public float width
  319. {
  320. get
  321. {
  322. return m_width;
  323. }
  324. set
  325. {
  326. if (m_width == value) return; m_width = value; m_propchanged = true;
  327. }
  328. }
  329. public float height
  330. {
  331. get
  332. {
  333. return m_height;
  334. }
  335. set
  336. {
  337. if (m_height == value) return; m_height = value; m_propchanged = true;
  338. }
  339. }
  340. public EnumHoriontal alignHoriontal
  341. {
  342. get
  343. {
  344. return m_alignHoriontal;
  345. }
  346. set
  347. {
  348. if (m_alignHoriontal == value) return; m_alignHoriontal = value; m_propchanged = true;
  349. }
  350. }
  351. public EnumVertical alignVertical
  352. {
  353. get
  354. {
  355. return m_alignVertical;
  356. }
  357. set
  358. {
  359. if (m_alignVertical == value) return; m_alignVertical = value; m_propchanged = true;
  360. }
  361. }
  362. public EnumFillType fillType
  363. {
  364. get
  365. {
  366. return m_fillType;
  367. }
  368. set
  369. {
  370. if (m_fillType == value) return; m_fillType = value; m_propchanged = true;
  371. }
  372. }
  373. public uint segments
  374. {
  375. get
  376. {
  377. return m_segments;
  378. }
  379. set
  380. {
  381. if (m_segments == value) return; m_segments = value; m_propchanged = true;
  382. }
  383. }
  384. #endregion
  385. public void RefreshAtlasMaterial()
  386. {
  387. if (m_atlas != null)
  388. {
  389. m_render.sharedMaterial = m_atlas.material;
  390. }
  391. }
  392. public void SetMaterial(Material mat)
  393. {
  394. Material oldMat = m_render.sharedMaterial;
  395. m_render.sharedMaterial = mat;
  396. Object.DestroyObject(oldMat);
  397. }
  398. public void RefreshAutoAtlasMaterial()
  399. {
  400. if (m_texture != null)
  401. {
  402. #if UNITY_EDITOR
  403. if (!UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode || m_render.sharedMaterial == null) //如果在编辑模式下,这个material一定要重新生成,因为很有可能这个OBJ是拷贝的,那么他会和源OBJ共享供一个材质球
  404. #else
  405. if(m_render.sharedMaterial == null)
  406. #endif
  407. {
  408. Shader shader = Shader.Find("UI/UI3D");
  409. Material mat = new Material(shader);
  410. mat.SetTexture("_MainTex", texture);
  411. m_render.sharedMaterial = mat;
  412. }
  413. else
  414. {
  415. m_render.sharedMaterial.SetTexture("_MainTex", texture);
  416. }
  417. }
  418. }
  419. private void RefreshUVDetail()
  420. {
  421. if (null == m_atlas)
  422. {
  423. return;
  424. }
  425. if (m_lastAtlasName == m_spriteName)
  426. {
  427. return;
  428. }
  429. SetUV(m_atlas.GetUV(m_spriteName));
  430. m_lastAtlasName = m_spriteName;
  431. }
  432. public void SetUV(AtlasInfo.UVDetail uv)
  433. {
  434. PrepareMesh();
  435. if (m_mesh == null
  436. || m_mesh.triangles.Length == 0
  437. || m_uv == null
  438. || m_uv.uvBL != uv.uvBL
  439. || m_uv.uvBR != uv.uvBR
  440. || m_uv.uvTL != uv.uvTL
  441. || m_uv.uvTR != uv.uvTR)
  442. {
  443. m_propchanged = true;
  444. }
  445. m_uv = uv;
  446. RefreshAutoAtlasMaterial();
  447. }
  448. private void RecaculateDepth()
  449. {
  450. if (m_mesh == null)
  451. {
  452. return;
  453. }
  454. Bounds bounds = new Bounds();
  455. bounds.center = new Vector3(0.5f * m_width, 0.5f * m_height, m_depth / 10);
  456. bounds.size = new Vector3(m_width, m_height, m_depth / 4 * 2);
  457. m_mesh.bounds = bounds;
  458. }
  459. public void PrepareMesh()
  460. {
  461. if (m_mesh == null)
  462. {
  463. MeshFilter meshFilter = gameObject.GetComponent<MeshFilter>();
  464. if (null == meshFilter)
  465. {
  466. meshFilter = gameObject.AddComponent<MeshFilter>();
  467. }
  468. m_mesh = new Mesh();
  469. meshFilter.mesh = m_mesh;
  470. m_render = gameObject.GetComponent<MeshRenderer>();
  471. if (null == m_render)
  472. {
  473. m_render = gameObject.AddComponent<MeshRenderer>();
  474. }
  475. }
  476. }
  477. public void GenerateMesh()
  478. {
  479. RefreshUVDetail();
  480. if (null == m_uv)
  481. {
  482. return;
  483. }
  484. PrepareMesh();
  485. m_mesh.Clear();
  486. if (m_fillType == EnumFillType.Horiontal)
  487. {
  488. GenerateHoriontalFillMesh();
  489. }
  490. else if (m_fillType == EnumFillType.Vertical)
  491. {
  492. GenerateVerticalFillMesh();
  493. }
  494. else if (m_fillType == EnumFillType.Radial360)
  495. {
  496. GenerateRadial360FillMesh();
  497. }
  498. }
  499. /// <summary>
  500. /// 假设屏幕都是1280宽度,避免不同分辨率开高清模式变小(很丑陋的patch,实际上做血条UI根本不该用这个函数)
  501. /// </summary>
  502. /// <param name="camera"></param>
  503. /// <param name="depth"></param>
  504. public void SetNativeSize(Camera camera, float depth)
  505. {
  506. //int standardWidth = 1280;
  507. //int width = Mathf.Max(Screen.width, Screen.height);
  508. float ratio = Ratio();
  509. RefreshUVDetail();
  510. if (camera != null)
  511. {
  512. Vector3 basePoint = camera.transform.TransformPoint(0f, 0f, depth);
  513. Vector3 posInScreen = camera.WorldToScreenPoint(basePoint);
  514. Vector3 posInScreenWidthDistance = new Vector3(posInScreen.x + textureWidth * ratio, posInScreen.y, posInScreen.z);
  515. Vector3 posInScreenHeightDistance = new Vector3(posInScreen.x, posInScreen.y + textureHeight * ratio, posInScreen.z);
  516. Vector3 widthPoint = camera.ScreenToWorldPoint(posInScreenWidthDistance);
  517. Vector3 heightPoint = camera.ScreenToWorldPoint(posInScreenHeightDistance);
  518. this.width = Vector3.Distance(basePoint, widthPoint);
  519. this.height = Vector3.Distance(basePoint, heightPoint);
  520. }
  521. //else
  522. //{
  523. // DebugHelper.LogError("3DCamera is NULL!!!");
  524. //}
  525. }
  526. //
  527. public void SetNativeSize(Camera camera, float depth, float screenWidth, float screenHeight)
  528. {
  529. //int standardWidth = 1280;
  530. //int width = Mathf.Max(Screen.width, Screen.height);
  531. //float ratio = Ratio();
  532. //RefreshUVDetail();
  533. if (camera != null)
  534. {
  535. Vector3 basePoint = camera.transform.TransformPoint(0f, 0f, depth);
  536. Vector3 posInScreen = camera.WorldToScreenPoint(basePoint);
  537. Vector3 posInScreenWidthDistance = new Vector3(posInScreen.x + screenWidth, posInScreen.y, posInScreen.z);
  538. Vector3 posInScreenHeightDistance = new Vector3(posInScreen.x, posInScreen.y + screenHeight, posInScreen.z);
  539. Vector3 widthPoint = camera.ScreenToWorldPoint(posInScreenWidthDistance);
  540. Vector3 heightPoint = camera.ScreenToWorldPoint(posInScreenHeightDistance);
  541. this.width = Vector3.Distance(basePoint, widthPoint);
  542. this.height = Vector3.Distance(basePoint, heightPoint);
  543. }
  544. //else
  545. //{
  546. // DebugHelper.LogError("3DCamera is NULL!!!");
  547. //}
  548. }
  549. //
  550. static private float S_Ratio = -1.0f;
  551. static public float Ratio()
  552. {
  553. if (S_Ratio == -1.0f)
  554. {
  555. //int standardWidth = 960;
  556. //int standardHeight = 640;
  557. int standardWidth = 1280;
  558. int standardHeight = 720;
  559. S_Ratio = Mathf.Min((float)Screen.height / standardHeight, (float)Screen.width / standardWidth);
  560. }
  561. return S_Ratio;
  562. }
  563. static public float SetRatio(int newWidth, int newHeight)
  564. {
  565. //int standardWidth = 960;
  566. //int standardHeight = 640;
  567. int standardWidth = 1280;
  568. int standardHeight = 720;
  569. S_Ratio = Mathf.Min((float)newHeight / standardHeight, (float)newWidth / standardWidth);
  570. return S_Ratio;
  571. }
  572. private void GenerateRadial360FillMesh()
  573. {
  574. m_fillAmount = Mathf.Clamp01(m_fillAmount); // 填充数量
  575. if (m_fillAmount <= 0)
  576. {
  577. return;
  578. }
  579. Vector3 localPosition = transform.localPosition;
  580. //transform.localPosition = new Vector3(localPosition.x, localPosition.y, m_depth);
  581. m_mesh.MarkDynamic();
  582. float left = 0f;
  583. float bottom = 0f;
  584. if (m_alignHoriontal == EnumHoriontal.Center)
  585. {
  586. left = -0.5f * m_width;
  587. }
  588. else if (m_alignHoriontal == EnumHoriontal.Right)
  589. {
  590. left = -m_width;
  591. }
  592. if (m_alignVertical == EnumVertical.Middle)
  593. {
  594. bottom = -0.5f * m_height;
  595. }
  596. else if (m_alignVertical == EnumVertical.Top)
  597. {
  598. bottom = -m_height;
  599. }
  600. int pointsOnSides = (int)(m_segments * m_fillAmount) + 1; // m_segments 类似角度的作用 pointsOnSides 每一度一个点吗?
  601. float step = 2 * (width + height) / (m_segments); // 计算步长
  602. Vector3[] vertices = new Vector3[pointsOnSides + 1];
  603. Vector2[] uvs = new Vector2[pointsOnSides + 1];
  604. vertices[0] = new Vector3(left + 0.5f * m_width, bottom + 0.5f * height, 0f); //中心点
  605. uvs[0] = m_uv.uvTL.Lerp(m_uv.uvBR, 0.5f); //uv中心点
  606. int phase = (int)PhaseType; // 左上开始,左中结束 [0 - 7]
  607. int cursor = 0;
  608. float compensate = 0f;
  609. for (int i = 0; i < pointsOnSides; ++i) // 遍历计算所有的点
  610. {
  611. if (phase == 0)
  612. {
  613. float x = left + cursor * step; // 0.5f * width
  614. if (x >= left + width)
  615. {
  616. compensate = x - left - width;
  617. x = left + width;
  618. phase = 1;
  619. cursor = 1;
  620. }
  621. else
  622. {
  623. cursor++;
  624. }
  625. vertices[i + 1] = new Vector3(x, bottom + height, 0);
  626. }
  627. else if (phase == 1)
  628. {
  629. float y = bottom + height - cursor * step - compensate;
  630. if (y <= bottom)
  631. {
  632. compensate = bottom - y;
  633. y = bottom;
  634. phase = 2;
  635. cursor = 1;
  636. }
  637. else
  638. {
  639. cursor++;
  640. }
  641. vertices[i + 1] = new Vector3(left + width, y, 0);
  642. }
  643. else if (phase == 2)
  644. {
  645. float x = left + width - cursor * step - compensate;
  646. if (x <= left)
  647. {
  648. compensate = left - x;
  649. x = left;
  650. phase = 3;
  651. cursor = 1;
  652. }
  653. else
  654. {
  655. cursor++;
  656. }
  657. vertices[i + 1] = new Vector3(x, bottom, 0);
  658. }
  659. else if (phase == 3)
  660. {
  661. float y = bottom + cursor * step + compensate;
  662. if (y >= bottom + height)
  663. {
  664. compensate = y - bottom - height;
  665. y = bottom + height;
  666. phase = 4;
  667. cursor = 1;
  668. }
  669. else
  670. {
  671. cursor++;
  672. }
  673. vertices[i + 1] = new Vector3(left, y);
  674. }
  675. else if (phase == 4)
  676. {
  677. float x = left + cursor * step + compensate;
  678. if (x > left + width)
  679. {
  680. x = left + width;
  681. phase = 0;
  682. }
  683. ++cursor;
  684. vertices[i + 1] = new Vector3(x, bottom + height, 0);
  685. }
  686. float vx = vertices[i + 1].x;
  687. float vy = vertices[i + 1].y;
  688. uvs[i + 1] = new Vector2(Mathf.Lerp(m_uv.uvTL.x, m_uv.uvTR.x, (vx - left) / width), Mathf.Lerp(m_uv.uvBL.y, m_uv.uvTL.y, (vy - bottom) / height));
  689. }
  690. Color[] colors = new Color[pointsOnSides + 1];
  691. for (int i = 0; i < colors.Length; ++i)
  692. {
  693. colors[i] = m_color;
  694. }
  695. int[] indices = new int[(pointsOnSides - 1) * 3];
  696. for (int i = 0; i < pointsOnSides - 1; ++i)
  697. {
  698. indices[i * 3] = 0;
  699. indices[i * 3 + 1] = i + 1;
  700. indices[i * 3 + 2] = i + 2;
  701. }
  702. m_mesh.vertices = vertices;
  703. m_mesh.uv = uvs;
  704. m_mesh.colors = colors;
  705. m_mesh.triangles = indices;
  706. RecaculateDepth();
  707. }
  708. private void GenerateHoriontalFillMesh()
  709. {
  710. m_fillAmount = Mathf.Clamp01(m_fillAmount);
  711. if (m_fillAmount <= 0)
  712. {
  713. return;
  714. }
  715. Vector3 localPosition = transform.localPosition;
  716. //transform.localPosition = new Vector3(localPosition.x, localPosition.y, m_depth);
  717. m_mesh.MarkDynamic();
  718. float left = 0f;
  719. float bottom = 0f;
  720. if (m_alignHoriontal == EnumHoriontal.Center)
  721. {
  722. left = -0.5f * m_width;
  723. }
  724. else if (m_alignHoriontal == EnumHoriontal.Right)
  725. {
  726. left = -m_width;
  727. }
  728. if (m_alignVertical == EnumVertical.Middle)
  729. {
  730. bottom = -0.5f * m_height;
  731. }
  732. else if (m_alignVertical == EnumVertical.Top)
  733. {
  734. bottom = -m_height;
  735. }
  736. Vector3[] vertices = new Vector3[4];
  737. vertices[0] = new Vector3(left, bottom + m_height, 0);
  738. vertices[1] = new Vector3(left + m_width * m_fillAmount, bottom + m_height, 0);
  739. vertices[2] = new Vector3(left, bottom, 0);
  740. vertices[3] = new Vector3(left + m_width * m_fillAmount, bottom, 0);
  741. Vector2[] uvs = new Vector2[4];
  742. uvs[0] = m_uv.uvTL;
  743. uvs[1] = m_uv.uvTL.Lerp(m_uv.uvTR, m_fillAmount);
  744. uvs[2] = m_uv.uvBL;
  745. uvs[3] = m_uv.uvBL.Lerp(m_uv.uvBR, m_fillAmount);
  746. Color[] colors = new Color[4] { m_color, m_color, m_color, m_color };
  747. int[] indices = new int[6];
  748. indices[0] = 0;
  749. indices[1] = 1;
  750. indices[2] = 2;
  751. indices[3] = 3;
  752. indices[4] = 2;
  753. indices[5] = 1;
  754. m_mesh.vertices = vertices;
  755. m_mesh.uv = uvs;
  756. m_mesh.colors = colors;
  757. m_mesh.triangles = indices;
  758. RecaculateDepth();
  759. }
  760. private void GenerateVerticalFillMesh()
  761. {
  762. m_fillAmount = Mathf.Clamp01(m_fillAmount);
  763. if (m_fillAmount <= 0)
  764. {
  765. return;
  766. }
  767. Vector3 localPosition = transform.localPosition;
  768. //transform.localPosition = new Vector3(localPosition.x, localPosition.y, m_depth);
  769. m_mesh.MarkDynamic();
  770. float left = 0f;
  771. float bottom = 0f;
  772. if (m_alignHoriontal == EnumHoriontal.Center)
  773. {
  774. left = -0.5f * m_width;
  775. }
  776. else if (m_alignHoriontal == EnumHoriontal.Right)
  777. {
  778. left = -m_width;
  779. }
  780. if (m_alignVertical == EnumVertical.Middle)
  781. {
  782. bottom = -0.5f * m_height;
  783. }
  784. else if (m_alignVertical == EnumVertical.Top)
  785. {
  786. bottom = -m_height;
  787. }
  788. Vector3[] vertices = new Vector3[4];
  789. vertices[0] = new Vector3(left, bottom + m_height * m_fillAmount, 0);
  790. vertices[1] = new Vector3(left + m_width, bottom + m_height * m_fillAmount, 0);
  791. vertices[2] = new Vector3(left, bottom, 0);
  792. vertices[3] = new Vector3(left + m_width, bottom, 0);
  793. Vector2[] uvs = new Vector2[4];
  794. uvs[0] = m_uv.uvBL.Lerp(m_uv.uvTL, m_fillAmount);
  795. uvs[1] = m_uv.uvBR.Lerp(m_uv.uvTR, m_fillAmount);
  796. uvs[2] = m_uv.uvBL;
  797. uvs[3] = m_uv.uvBR;
  798. Color[] colors = new Color[4] { m_color, m_color, m_color, m_color };
  799. int[] indices = new int[6];
  800. indices[0] = 0;
  801. indices[1] = 1;
  802. indices[2] = 2;
  803. indices[3] = 3;
  804. indices[4] = 2;
  805. indices[5] = 1;
  806. m_mesh.vertices = vertices;
  807. m_mesh.uv = uvs;
  808. m_mesh.colors = colors;
  809. m_mesh.triangles = indices;
  810. RecaculateDepth();
  811. }
  812. }