TempleKit.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using TMPro;
  5. using UnityEditor;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. namespace Olympus.Editor
  9. {
  10. // All textures are authored here or are independently generated full-image assets.
  11. // Never read a mockup, screen plate, atlas region, or non-default UV rectangle.
  12. public static class TempleKit
  13. {
  14. public const string Folder="Assets/Olympus/Resources/Olympus/Elements/";
  15. public const float K=750f/941;
  16. public static Color Cream=new Color32(255,242,211,255),Gold=new Color32(255,214,117,255),Ink=new Color32(47,23,5,255);
  17. public static TMP_FontAsset Font=>AssetDatabase.LoadAssetAtPath<TMP_FontAsset>("Assets/Olympus/Resources/Olympus/Suite/Temple Serif.asset");
  18. static Material HeadingMaterial=>AssetDatabase.LoadAssetAtPath<Material>(Folder+"Embossed serif.mat");
  19. public static RectTransform R(string name,Transform parent,float x,float y,float w,float h)
  20. {
  21. var r=OlympusUi.Rect(name,parent);r.anchorMin=r.anchorMax=new Vector2(0,1);r.pivot=new Vector2(0,1);r.anchoredPosition=new Vector2(x*K,-y*K);r.sizeDelta=new Vector2(w*K,h*K);return r;
  22. }
  23. public static Sprite S(string key)
  24. {
  25. string replacement=key=="gold_button"?"v3_button":key=="ornate_panel"?"v3_panel":key=="tile"?"v3_tile":key=="circle"?"v3_round":key=="blue_disc"?"v3_timer":key=="trophy"?"v3_trophy":key=="chest"?"v3_chest":key;
  26. if(key=="victory_crest")replacement="trophy";
  27. if(key=="reward_chest")replacement="chest";
  28. return AssetDatabase.LoadAssetAtPath<Sprite>(Folder+replacement+".png");
  29. }
  30. public static Sprite Art(string key)=>AssetDatabase.LoadAssetAtPath<Sprite>("Assets/Olympus/Resources/Olympus/Art/"+key+".png");
  31. public static Sprite Question(string key)=>AssetDatabase.LoadAssetAtPath<Sprite>("Assets/Olympus/Resources/Olympus/Questions/"+key+".png");
  32. public static Sprite Power(int i)=>S(new[]{"v3_hint","v3_time","v3_reveal","v3_skip"}[i]);
  33. public static Image I(string name,Transform p,Sprite s,float x,float y,float w,float h,bool aspect=true)
  34. {var i=R(name,p,x,y,w,h).gameObject.AddComponent<Image>();i.sprite=s;i.raycastTarget=false;i.preserveAspect=aspect;return i;}
  35. public static Image Plate(string name,Transform p,float x,float y,float w,float h,string key="panel")
  36. {
  37. string sprite=key=="panel"||key=="wood"?(h<155?"v3_plaque":"v3_panel"):key=="button"?"v3_button":key=="row"?"v3_panel":key;
  38. var i=I(name,p,S(sprite),x,y,w,h);
  39. if(i.sprite && i.sprite.border!=Vector4.zero)
  40. {
  41. i.type=Image.Type.Sliced;i.preserveAspect=false;
  42. var slice=i.gameObject.AddComponent<ProportionalSlice>();slice.capsule=key=="button"||key=="ivory"||sprite=="v3_plaque";slice.cornerSize=(key=="row"?16:42)*K;slice.Apply();
  43. }
  44. return i;
  45. }
  46. public static TextMeshProUGUI T(string name,Transform p,string text,float x,float y,float w,float h,float size,Color? color=null)
  47. {
  48. var t=R(name,p,x,y,w,h).gameObject.AddComponent<TextMeshProUGUI>();t.font=Font;t.text=text;t.fontSize=t.fontSizeMax=size*K;t.fontSizeMin=size*K*.65f;t.enableAutoSizing=true;t.color=color??Cream;t.alignment=TextAlignmentOptions.Center;t.textWrappingMode=TextWrappingModes.Normal;t.raycastTarget=false;
  49. if(size>=40&&(!color.HasValue||color.Value==Gold))
  50. {
  51. t.fontSharedMaterial=HeadingMaterial;t.enableVertexGradient=true;
  52. t.colorGradient=new VertexGradient(Color.white,Color.white,new Color32(225,190,126,255),new Color32(225,190,126,255));
  53. }
  54. var s=t.gameObject.AddComponent<Shadow>();s.effectDistance=new Vector2(1,-1.6f);s.effectColor=new Color(0,0,0,.75f);return t;
  55. }
  56. public static Button B(string name,Transform p,float x,float y,float w,float h,string label=null,float size=38,string key="button")
  57. {
  58. var i=Plate(name,p,x,y,w,h,key);i.raycastTarget=true;var b=i.gameObject.AddComponent<Button>();b.targetGraphic=i;var c=b.colors;c.highlightedColor=new Color(1.08f,1.06f,1,1);c.pressedColor=new Color(.7f,.57f,.35f,1);c.disabledColor=new Color(.48f,.43f,.34f,1);b.colors=c;
  59. if(label!=null)T(name+" label",i.transform,label,5,2,w-10,h-4,size,key=="button"||key=="tile"?Ink:Cream);return b;
  60. }
  61. public static Button Hit(string name,Transform p,float x,float y,float w,float h)
  62. {var b=B(name,p,x,y,w,h);b.image.sprite=null;b.image.color=Color.clear;return b;}
  63. public static Graphic G(string name,Transform p,TempleGlyph.Kind kind,float x,float y,float w,float h,Color? color=null)
  64. {
  65. if(kind==TempleGlyph.Kind.Laurel)
  66. {
  67. // Keep the painted leaves' proportions while spacing the two branches around text.
  68. // Both Images reference the complete independently generated asset, never a mockup slice.
  69. var root=I(name,p,null,x,y,w,h);root.color=Color.clear;float leafHeight=Mathf.Min(h,w*.52f),sw=leafHeight*2163f/727f,branch=sw*.27f;
  70. for(int side=0;side<2;side++)
  71. {
  72. var window=R("Laurel window "+side,root.transform,side==0?0:w-branch,(h-leafHeight)*.5f,branch,leafHeight);window.gameObject.AddComponent<RectMask2D>();
  73. I("Painted laurel "+side,window,S("v2_laurels"),side==0?0:branch-sw,0,sw,leafHeight);
  74. }
  75. return root;
  76. }
  77. string sprite=kind==TempleGlyph.Kind.Gear?"v3_gear":kind==TempleGlyph.Kind.Book?"v3_book":kind==TempleGlyph.Kind.Profile?"v3_profile":kind==TempleGlyph.Kind.Sound?"v3_sound":kind==TempleGlyph.Kind.Music?"v3_music":kind==TempleGlyph.Kind.Hand?"v3_hand":kind==TempleGlyph.Kind.Brush?"v3_brush":kind==TempleGlyph.Kind.Letters?"v3_letters":null;
  78. if(sprite!=null)return I(name,p,S(sprite),x,y,w,h,kind!=TempleGlyph.Kind.Laurel);
  79. var g=R(name,p,x,y,w,h).gameObject.AddComponent<TempleGlyph>();g.kind=kind;g.color=color??Gold;g.raycastTarget=false;return g;
  80. }
  81. public static void Background(Transform p,bool trial=false)
  82. {I("Independent temple environment",p,trial?Art("background_olympus"):S("v3_background"),0,0,941,1672).gameObject.AddComponent<ScreenSpaceBackground>();}
  83. public static void Title(Transform p,string value,float y=200)
  84. {Plate("Title plaque",p,180,y-12,581,112,"wood");G("Title laurels",p,TempleGlyph.Kind.Laurel,150,y-13,640,116);T("Title",p,value,240,y,460,90,56,Gold);}
  85. public static void Logo(Transform p,float y,float height=215)
  86. {
  87. // Independently painted brand wordmark; all functional labels remain TMP.
  88. I("Sculpted Olympus wordmark",p,S("v3_logo"),58,y,825,height);
  89. }
  90. public static void Prepare()
  91. {
  92. Directory.CreateDirectory(Folder);
  93. var heading=HeadingMaterial;
  94. if(!heading){heading=new Material(Font.material);AssetDatabase.CreateAsset(heading,Folder+"Embossed serif.mat");}
  95. heading.SetColor("_OutlineColor",new Color32(44,20,5,255));heading.SetFloat("_OutlineWidth",.09f);heading.SetFloat("_FaceDilate",.015f);EditorUtility.SetDirty(heading);
  96. // Only these three procedural textures remain in the current UI kit.
  97. foreach(var key in new[]{"ivory","coin","green_disc"})Make(key);
  98. AssetDatabase.Refresh();
  99. foreach(string dir in new[]{Folder,"Assets/Olympus/Resources/Olympus/Art/","Assets/Olympus/Resources/Olympus/Questions/"})
  100. foreach(string file in Directory.GetFiles(dir,"*.png"))
  101. {
  102. string path=file.Replace('\\','/');var imp=(TextureImporter)AssetImporter.GetAtPath(path);string key=Path.GetFileNameWithoutExtension(path);
  103. var border=Vector4.zero;
  104. if(key=="ivory")border=new Vector4(56,54,56,54);
  105. float ppu=100;
  106. if(key=="v3_panel"||key=="v3_plaque"||key=="v3_button")
  107. {
  108. imp.GetSourceTextureWidthAndHeight(out int sw,out int sh);
  109. if(key=="v3_panel")border=Vector4.one*(sw*.13f);
  110. else border=new Vector4(sh*.5f,sh*.48f,sh*.5f,sh*.48f);
  111. }
  112. bool illustrative=key.StartsWith("avatar_")||key.StartsWith("power_")||key.StartsWith("v2_")||new[]{"trophy","chest","gold_coin","portal_medallion"}.Contains(key);
  113. bool compact=key.StartsWith("avatar_")||key.StartsWith("v3_")&&!new[]{"v3_panel","v3_plaque","v3_button","v3_background","v3_logo"}.Contains(key);
  114. int maxSize=key=="v3_background"?2048:key=="v3_logo"?1024:compact?256:key.StartsWith("power_")||key.StartsWith("v3_")?512:2048;
  115. imp.textureType=TextureImporterType.Sprite;imp.spriteImportMode=SpriteImportMode.Single;imp.spriteBorder=border;imp.spritePixelsPerUnit=ppu;imp.alphaIsTransparency=true;imp.mipmapEnabled=illustrative||compact;imp.filterMode=illustrative||compact?FilterMode.Trilinear:FilterMode.Bilinear;imp.maxTextureSize=maxSize;imp.npotScale=TextureImporterNPOTScale.None;var settings=new TextureImporterSettings();imp.ReadTextureSettings(settings);settings.spriteMeshType=SpriteMeshType.FullRect;imp.SetTextureSettings(settings);imp.textureCompression=TextureImporterCompression.Uncompressed;imp.SaveAndReimport();
  116. // A 1254px source is not a 1254 UI-unit icon. SetNativeSize and
  117. // dragging assets onto a 750x1334 Canvas now have sensible sizes.
  118. if(key.StartsWith("v3_")&&key!="v3_background")IndependentSpriteImporter.TrimPadding(imp,path,key);
  119. float native=NativeWidth(key);
  120. if(native>0){var sprite=AssetDatabase.LoadAssetAtPath<Sprite>(path);imp.GetSourceTextureWidthAndHeight(out int sourceWidth,out int sourceHeight);imp.spritePixelsPerUnit=sprite.rect.width/native*100*sourceWidth/sprite.texture.width;imp.SaveAndReimport();}
  121. }
  122. }
  123. public static float NativeWidth(string key)
  124. {
  125. if(key=="v3_background")return 750;
  126. if(key=="v3_logo")return 658;
  127. if(new[]{"v3_hint","v3_time","v3_reveal","v3_skip"}.Contains(key))return 124;
  128. if(key=="v3_panel")return 340;
  129. if(key=="v3_button"||key=="v3_plaque")return 256;
  130. if(key=="v3_timer")return 112;
  131. if(key=="v3_tile")return 72;
  132. if(key.StartsWith("v3_"))return 64;
  133. if(key.StartsWith("avatar_"))return 80;
  134. if(key=="gold_coin")return 40;
  135. if(key=="portal_medallion")return 360;
  136. return 0;
  137. }
  138. public static void SaveComponents()
  139. {
  140. const string folder="Assets/Olympus/Prefabs/UI/";Directory.CreateDirectory(folder);
  141. foreach(string name in new[]{"GoldButton","LetterTile","Panel","AnswerField","Avatar","Powerup"})
  142. {
  143. RectTransform r;
  144. if(name=="GoldButton"||name=="LetterTile")r=(RectTransform)B(name,null,0,0,name=="GoldButton"?320:91,name=="GoldButton"?90:91,name=="GoldButton"?"CONTINUE":"A",40,name=="GoldButton"?"button":"tile").transform;
  145. else if(name=="Panel"||name=="AnswerField")r=Plate(name,null,0,0,400,name=="Panel"?400:100,name=="Panel"?"panel":"ivory").rectTransform;
  146. else r=I(name,null,name=="Avatar"?Art("avatar_1"):Power(0),0,0,name=="Avatar"?100:156,name=="Avatar"?100:156,true).rectTransform;
  147. r.anchoredPosition=Vector2.zero;PrefabUtility.SaveAsPrefabAsset(r.gameObject,folder+name+".prefab");UnityEngine.Object.DestroyImmediate(r.gameObject);
  148. }
  149. }
  150. static Color C(byte r,byte g,byte b)=>new Color32(r,g,b,255);
  151. static void Make(string key)
  152. {
  153. bool round=key=="circle"||key=="coin"||key.EndsWith("_disc"),gold=key=="button"||key=="tile"||key=="coin",ivory=key=="ivory";
  154. int w=key=="button"||ivory?384:128,h=key=="button"||ivory?112:128;var t=new Texture2D(w,h,TextureFormat.RGBA32,false);var pix=new Color32[w*h];
  155. for(int y=0;y<h;y++)for(int x=0;x<w;x++)
  156. {
  157. float xx=x+.5f,yy=y+.5f,edge;
  158. if(round)edge=60-Vector2.Distance(new Vector2(xx,yy),new Vector2(64,64));
  159. else if(key=="button"||ivory){float cx=Mathf.Clamp(xx,54,w-54);edge=52-Vector2.Distance(new Vector2(xx,yy),new Vector2(cx,h/2));}
  160. else {edge=Mathf.Min(Mathf.Min(xx,w-xx),Mathf.Min(yy,h-yy))-2;edge=Mathf.Min(edge,(Mathf.Min(xx,w-xx)+Mathf.Min(yy,h-yy)-16)*.707f);}
  161. Color c;if(edge<0)c=Color.clear;
  162. else if(edge<2)c=C(58,28,7);else if(edge<4)c=C(255,232,162);else if(edge<8)c=Color.Lerp(C(130,64,10),C(246,191,78),yy/h);else if(edge<10)c=C(255,221,131);else if(edge<12)c=C(87,43,10);
  163. else if(gold){float f=(float)y/h;c=Color.Lerp(C(174,98,12),C(255,225,130),f);if(f>.68f)c=Color.Lerp(c,C(255,246,197),.23f);}
  164. else if(ivory)c=Color.Lerp(C(221,213,194),C(255,251,239),(float)y/h);
  165. else if(key.EndsWith("_disc"))c=Color.Lerp(key=="blue_disc"?C(8,39,87):C(15,63,5),key=="blue_disc"?C(15,179,231):C(117,204,20),(float)y/h);
  166. else {c=Color.Lerp(key=="wood"?C(68,29,9):C(28,14,7),key=="wood"?C(119,61,23):C(69,38,17),(float)y/h);float grain=Mathf.Sin(x*.14f+Mathf.Sin(y*.08f)*3)*Mathf.Sin(y*.9f)*.017f;c+=new Color(grain,grain*.65f,grain*.25f,0);}
  167. pix[y*w+x]=c;
  168. }
  169. t.SetPixels32(pix);t.Apply();File.WriteAllBytes(Folder+key+".png",t.EncodeToPNG());UnityEngine.Object.DestroyImmediate(t);
  170. }
  171. }
  172. }