TextMeshSharpener.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using System;
  2. using UnityEngine;
  3. #if UNITY_EDITOR
  4. [ExecuteInEditMode()]
  5. #endif
  6. public class TextMeshSharpener:MonoBehaviour {
  7. /*
  8. Makes TextMesh look sharp regardless of camera size/resolution
  9. Do NOT change character size or font size; use scale only
  10. */
  11. // Properties
  12. private float lastPixelHeight = -1;
  13. private TextMesh textMesh;
  14. // ================================================================================================================
  15. // MAIN EVENT INTERFACE -------------------------------------------------------------------------------------------
  16. void Awake() {
  17. }
  18. void Start() {
  19. textMesh = GetComponent<TextMesh>();
  20. resize();
  21. }
  22. void Update() {
  23. if (Camera.main.pixelHeight != lastPixelHeight || (Application.isEditor && !Application.isPlaying)) resize();
  24. }
  25. // ================================================================================================================
  26. // INTERNAL INTERFACE ---------------------------------------------------------------------------------------------
  27. private void resize() {
  28. float ph = Camera.main.pixelHeight;
  29. float ch = Camera.main.orthographicSize;
  30. /*
  31. //Constant size:
  32. float pixelRatio = (ch * 2.0f) / ph;
  33. textMesh.characterSize = 1;
  34. transform.localScale = new Vector3(pixelRatio * 10.0f, pixelRatio * 10.0f, pixelRatio * 0.1f);
  35. */
  36. float pixelRatio = (ch * 2.0f) / ph;
  37. float targetRes = 128f;
  38. textMesh.characterSize = pixelRatio * Camera.main.orthographicSize / Math.Max(transform.localScale.x, transform.localScale.y);
  39. textMesh.fontSize = (int)Math.Round(targetRes / textMesh.characterSize);
  40. lastPixelHeight = ph;
  41. }
  42. }