Advertisement
Guest User

Untitled

a guest
May 11th, 2015
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4.  
  5. [RequireComponent (typeof (Collider))]
  6. public class Label : MonoBehaviour
  7. {
  8. public string labelText = "";
  9. // The text rendered in the label.
  10. public GUISkin customSkin = null;
  11. // The skin containing the style used to render the label (leave as null to use the default skin)
  12. public string styleName = "Box";
  13. // The style used to render the label. Must be available in the used skin.
  14. public Camera guiCamera = null;
  15. // The camera used to display the GUI. Used for coordinate and distance calculations.
  16. public float fadeDistance = 30.0f, hideDistance = 35.0f;
  17. // Specifies when the label should start fading and when it should hide
  18. public float maxViewAngle = 90.0f;
  19. // Specifies at which angle to the camera forward vector, the label should no longer render
  20.  
  21.  
  22.  
  23. void Reset ()
  24. // Fallback for the camera reference
  25. {
  26. if (guiCamera == null)
  27. {
  28. guiCamera = Camera.main;
  29. maxViewAngle = guiCamera.fieldOfView * 0.5f;
  30. }
  31. }
  32.  
  33.  
  34. public void SetLabel (string label)
  35. // Handle SetLabel messages sent to the GO
  36. {
  37. labelText = label;
  38. }
  39.  
  40.  
  41. void OnGUI ()
  42. {
  43. useGUILayout = false;
  44. // We're not using GUILayout, so don't spend processing on it
  45.  
  46. if (Event.current.type != EventType.Repaint)
  47. // We are only interested in repaint events
  48. {
  49. return;
  50. }
  51.  
  52. Vector3 worldPosition = GetComponent<collider>().bounds.center + Vector3.up * collider.bounds.size.y * 0.5f;
  53. // Place the label on top of the collider
  54. float cameraDistance = (worldPosition - guiCamera.transform.position).magnitude;
  55.  
  56. if (
  57. cameraDistance > hideDistance ||
  58. Vector3.Angle (
  59. guiCamera.transform.forward,
  60. worldPosition - guiCamera.transform.position
  61. ) >
  62. maxViewAngle
  63. )
  64. // If the world position is outside of the field of view or further away than hideDistance, don't render the label
  65. {
  66. return;
  67. }
  68.  
  69. if (cameraDistance > fadeDistance)
  70. // If the distance to the label position is greater than the fade distance, apply the needed fade to the label
  71. {
  72. GUI.color = new Color (
  73. 1.0f,
  74. 1.0f,
  75. 1.0f,
  76. 1.0f - (cameraDistance - fadeDistance) / (hideDistance - fadeDistance)
  77. );
  78. }
  79.  
  80. Vector2 position = guiCamera.WorldToScreenPoint (worldPosition);
  81. position = new Vector2 (position.x, Screen.height - position.y);
  82. // Get the GUI space position
  83.  
  84. GUI.skin = customSkin;
  85. // Set the custom skin. If no custom skin is set (null), Unity will use the default skin
  86.  
  87. string contents = string.IsNullOrEmpty (labelText) ? gameObject.name : labelText;
  88.  
  89. Vector2 size = GUI.skin.GetStyle (styleName).CalcSize (new GUIContent (contents));
  90. // Get the content size with the selected style
  91.  
  92. Rect rect = new Rect (position.x - size.x * 0.5f, position.y - size.y, size.x, size.y);
  93. // Construct a rect based on the calculated position and size
  94.  
  95. GUI.skin.GetStyle (styleName).Draw (rect, contents, false, false, false, false);
  96. // Draw the label with the selected style
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement