TMPro_InstructionOverlay.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace TMPro.Examples
  4. {
  5. public class TMPro_InstructionOverlay : MonoBehaviour
  6. {
  7. public enum FpsCounterAnchorPositions { TopLeft, BottomLeft, TopRight, BottomRight };
  8. public FpsCounterAnchorPositions AnchorPosition = FpsCounterAnchorPositions.BottomLeft;
  9. private const string instructions = "Camera Control - <#ffff00>Shift + RMB\n</color>Zoom - <#ffff00>Mouse wheel.";
  10. private TextMeshPro m_TextMeshPro;
  11. private TextContainer m_textContainer;
  12. private Transform m_frameCounter_transform;
  13. private Camera m_camera;
  14. //private FpsCounterAnchorPositions last_AnchorPosition;
  15. void Awake()
  16. {
  17. if (!enabled)
  18. return;
  19. m_camera = Camera.main;
  20. GameObject frameCounter = new GameObject("Frame Counter");
  21. m_frameCounter_transform = frameCounter.transform;
  22. m_frameCounter_transform.parent = m_camera.transform;
  23. m_frameCounter_transform.localRotation = Quaternion.identity;
  24. m_TextMeshPro = frameCounter.AddComponent<TextMeshPro>();
  25. m_TextMeshPro.font = Resources.Load<TMP_FontAsset>("Fonts & Materials/LiberationSans SDF");
  26. m_TextMeshPro.fontSharedMaterial = Resources.Load<Material>("Fonts & Materials/LiberationSans SDF - Overlay");
  27. m_TextMeshPro.fontSize = 30;
  28. m_TextMeshPro.isOverlay = true;
  29. m_textContainer = frameCounter.GetComponent<TextContainer>();
  30. Set_FrameCounter_Position(AnchorPosition);
  31. //last_AnchorPosition = AnchorPosition;
  32. m_TextMeshPro.text = instructions;
  33. }
  34. void Set_FrameCounter_Position(FpsCounterAnchorPositions anchor_position)
  35. {
  36. switch (anchor_position)
  37. {
  38. case FpsCounterAnchorPositions.TopLeft:
  39. //m_TextMeshPro.anchor = AnchorPositions.TopLeft;
  40. m_textContainer.anchorPosition = TextContainerAnchors.TopLeft;
  41. m_frameCounter_transform.position = m_camera.ViewportToWorldPoint(new Vector3(0, 1, 100.0f));
  42. break;
  43. case FpsCounterAnchorPositions.BottomLeft:
  44. //m_TextMeshPro.anchor = AnchorPositions.BottomLeft;
  45. m_textContainer.anchorPosition = TextContainerAnchors.BottomLeft;
  46. m_frameCounter_transform.position = m_camera.ViewportToWorldPoint(new Vector3(0, 0, 100.0f));
  47. break;
  48. case FpsCounterAnchorPositions.TopRight:
  49. //m_TextMeshPro.anchor = AnchorPositions.TopRight;
  50. m_textContainer.anchorPosition = TextContainerAnchors.TopRight;
  51. m_frameCounter_transform.position = m_camera.ViewportToWorldPoint(new Vector3(1, 1, 100.0f));
  52. break;
  53. case FpsCounterAnchorPositions.BottomRight:
  54. //m_TextMeshPro.anchor = AnchorPositions.BottomRight;
  55. m_textContainer.anchorPosition = TextContainerAnchors.BottomRight;
  56. m_frameCounter_transform.position = m_camera.ViewportToWorldPoint(new Vector3(1, 0, 100.0f));
  57. break;
  58. }
  59. }
  60. }
  61. }