CPJSprite.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using CommonLang;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using UnityEngine;
  7. using CSpriteMeta = CommonUI.Cell.Game.CSpriteMeta;
  8. using CSpriteController = CommonUI.Cell.Game.CSpriteController;
  9. using CSpriteEventHandler = CommonUI.Cell.Game.CSpriteEventHandler;
  10. using CommonUI_Unity3D.Impl;
  11. using UnityEngine.UI;
  12. namespace CommonUnity3D.UGUI
  13. {
  14. public class CPJSprite : DisplayNode
  15. {
  16. private readonly CPJSpriteGraphics mGraphics;
  17. private CSpriteController mSprite;
  18. private CommonUI.Cell.Game.CCD mVisibleBounds;
  19. public CPJSprite(string name) : base(name)
  20. {
  21. this.Enable = false;
  22. this.EnableChildren = false;
  23. this.mGraphics = mGameObject.AddComponent<CPJSpriteGraphics>();
  24. }
  25. public CPJSprite(CSpriteMeta meta) : this(meta.Data.Name)
  26. {
  27. this.SpriteMeta = meta;
  28. }
  29. protected override void OnDispose()
  30. {
  31. if (mSprite != null) mSprite.Dispose();
  32. base.OnDispose();
  33. }
  34. public void SetPreferredBounds()
  35. {
  36. if (mSprite != null)
  37. {
  38. this.Size2D = new Vector2(mVisibleBounds.Width, mVisibleBounds.Height);
  39. Vector2 pivot = new Vector2(
  40. CMath.getRate(0, mVisibleBounds.X1, mVisibleBounds.X2),
  41. 1f - CMath.getRate(0, mVisibleBounds.Y1, mVisibleBounds.Y2));
  42. this.mTransform.pivot = pivot;
  43. }
  44. }
  45. public CSpriteMeta SpriteMeta
  46. {
  47. get { return mSprite.Meta; }
  48. set
  49. {
  50. if (mSprite != null) mSprite.Dispose();
  51. this.mSprite = new CSpriteController(value);
  52. this.mGraphics.SetSpriteMeta(this);
  53. this.mVisibleBounds = mSprite.Meta.getVisibleBounds();
  54. }
  55. }
  56. public CPJSpriteGraphics Graphics
  57. {
  58. get { return mGraphics; }
  59. }
  60. public CSpriteController Controller
  61. {
  62. get { return mSprite; }
  63. }
  64. public CommonUI.Cell.Game.CCD VisibleBounds
  65. {
  66. get { return mVisibleBounds; }
  67. }
  68. protected override void OnUpdate()
  69. {
  70. base.OnUpdate();
  71. if (this.mSprite != null)
  72. {
  73. this.mGraphics.SetFrame(mSprite.CurrentAnimate, mSprite.CurrentFrame);
  74. this.mSprite.Update();
  75. }
  76. }
  77. }
  78. }