UEImageBox.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using CommonLang.Xml;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Xml;
  6. using UnityEngine;
  7. using CommonUI.Data;
  8. using TextAnchor = CommonUI.Data.TextAnchor;
  9. using FontStyle = CommonUI.Data.FontStyle;
  10. namespace CommonUnity3D.UGUIEditor.UI
  11. {
  12. public class UEImageBox : UIComponent
  13. {
  14. protected UGUI.ImageSprite mImageContent;
  15. public void SetContent(UGUI.ImageSprite spr, float rotate, float scale_x, float scale_y)
  16. {
  17. if (mImageContent != spr && mImageContent != null)
  18. {
  19. mImageContent.RemoveFromParent();
  20. }
  21. var center = new Vector2(0.5f, 0.5f);
  22. this.mImageContent = spr;
  23. this.mImageContent.mTransform.anchorMin = center;
  24. this.mImageContent.mTransform.anchorMax = center;
  25. this.mImageContent.mTransform.pivot = center;
  26. this.mImageContent.mTransform.localScale = new Vector2(scale_x / 100f, scale_y / 100f);
  27. this.mImageContent.mTransform.localRotation = Quaternion.Euler(0f, 0f, -rotate);
  28. this.AddChild(mImageContent);
  29. }
  30. protected override void DecodeEnd(UIEditor.Decoder editor, UIComponentMeta e)
  31. {
  32. base.DecodeEnd(editor, e);
  33. this.Decode_Image(editor, e as UEImageBoxMeta);
  34. this.EnableChildren = false;
  35. }
  36. protected virtual void Decode_Image(UIEditor.Decoder editor, UEImageBoxMeta e)
  37. {
  38. string image_name = e.imagePath;
  39. string atlas_name = e.imageAtlas;
  40. if (!string.IsNullOrEmpty(atlas_name) && atlas_name.StartsWith("#"))
  41. {
  42. var spr = editor.editor.ParseImageSpriteFromAtlas(atlas_name, new Vector2(0.5f, 0.5f));
  43. if (spr != null)
  44. {
  45. this.SetContent(spr, e.x_rotate, e.x_scaleX, e.x_scaleY);
  46. }
  47. }
  48. else if (!string.IsNullOrEmpty(image_name))
  49. {
  50. var spr = editor.editor.ParseImageSpriteFromImage(image_name, new Vector2(0.5f, 0.5f));
  51. if (spr != null)
  52. {
  53. this.SetContent(spr, e.x_rotate, e.x_scaleX, e.x_scaleY);
  54. }
  55. }
  56. }
  57. }
  58. }