DailyPanelImage.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.Sprites;
  4. namespace DevilLink2.UI
  5. {
  6. // Sliced Image with an extra fixed-width center column for the two central gems.
  7. // Uses the original sprite/UVs: no extracted textures and no duplicated overlays.
  8. public sealed class DailyPanelImage : Image
  9. {
  10. protected override void OnPopulateMesh(VertexHelper mesh)
  11. {
  12. if (!sprite || type != Type.Sliced) { base.OnPopulateMesh(mesh); return; }
  13. mesh.Clear();
  14. var rect = GetPixelAdjustedRect(); var border = sprite.border;
  15. float sourceWidth = sprite.rect.width, sourceHeight = sprite.rect.height;
  16. float unit = pixelsPerUnit * pixelsPerUnitMultiplier; // Includes pixelsPerUnitMultiplier and Canvas reference PPU.
  17. float center = 160f; // Source x432..592 contains both complete central gems.
  18. float fixedScale = Mathf.Min(1f, Mathf.Min(rect.width * unit / (border.x + border.z + center), rect.height * unit / (border.y + border.w)));
  19. float left=border.x/unit*fixedScale, right=border.z/unit*fixedScale;
  20. float bottom=border.y/unit*fixedScale, top=border.w/unit*fixedScale;
  21. float half=center/unit*fixedScale*.5f;
  22. var xs = new[]{rect.xMin,rect.xMin+left,rect.center.x-half,rect.center.x+half,rect.xMax-right,rect.xMax};
  23. var ys = new[]{rect.yMin,rect.yMin+bottom,rect.yMax-top,rect.yMax};
  24. var sx = new[]{0f,border.x,sourceWidth*.5f-center*.5f,sourceWidth*.5f+center*.5f,sourceWidth-border.z,sourceWidth};
  25. var sy = new[]{0f,border.y,sourceHeight-border.w,sourceHeight};
  26. var uv=DataUtility.GetOuterUV(sprite);
  27. for(int y=0;y<3;y++)for(int x=0;x<5;x++)
  28. {
  29. if(!fillCenter && y==1 && x>0 && x<4)continue;
  30. int first=mesh.currentVertCount;
  31. mesh.AddVert(new Vector3(xs[x],ys[y]),color,new Vector2(Mathf.Lerp(uv.x,uv.z,sx[x]/sourceWidth),Mathf.Lerp(uv.y,uv.w,sy[y]/sourceHeight)));
  32. mesh.AddVert(new Vector3(xs[x],ys[y+1]),color,new Vector2(Mathf.Lerp(uv.x,uv.z,sx[x]/sourceWidth),Mathf.Lerp(uv.y,uv.w,sy[y+1]/sourceHeight)));
  33. mesh.AddVert(new Vector3(xs[x+1],ys[y+1]),color,new Vector2(Mathf.Lerp(uv.x,uv.z,sx[x+1]/sourceWidth),Mathf.Lerp(uv.y,uv.w,sy[y+1]/sourceHeight)));
  34. mesh.AddVert(new Vector3(xs[x+1],ys[y]),color,new Vector2(Mathf.Lerp(uv.x,uv.z,sx[x+1]/sourceWidth),Mathf.Lerp(uv.y,uv.w,sy[y]/sourceHeight)));
  35. mesh.AddTriangle(first,first+1,first+2);mesh.AddTriangle(first,first+2,first+3);
  36. }
  37. }
  38. }
  39. }