PlayerUI.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="PlayerUI.cs" company="Exit Games GmbH">
  3. // Part of: Photon Unity Networking Demos
  4. // </copyright>
  5. // <summary>
  6. // Used in PUN Basics Tutorial to deal with the networked player instance UI display tha follows a given player to show its health and name
  7. // </summary>
  8. // <author>developer@exitgames.com</author>
  9. // --------------------------------------------------------------------------------------------------------------------
  10. using UnityEngine;
  11. using UnityEngine.UI;
  12. namespace Photon.Pun.Demo.PunBasics
  13. {
  14. #pragma warning disable 649
  15. /// <summary>
  16. /// Player UI. Constraint the UI to follow a PlayerManager GameObject in the world,
  17. /// Affect a slider and text to display Player's name and health
  18. /// </summary>
  19. public class PlayerUI : MonoBehaviour
  20. {
  21. #region Private Fields
  22. [Tooltip("Pixel offset from the player target")]
  23. [SerializeField]
  24. private Vector3 screenOffset = new Vector3(0f, 30f, 0f);
  25. [Tooltip("UI Text to display Player's Name")]
  26. [SerializeField]
  27. private Text playerNameText;
  28. [Tooltip("UI Slider to display Player's Health")]
  29. [SerializeField]
  30. private Slider playerHealthSlider;
  31. PlayerManager target;
  32. float characterControllerHeight;
  33. Transform targetTransform;
  34. Renderer targetRenderer;
  35. CanvasGroup _canvasGroup;
  36. Vector3 targetPosition;
  37. #endregion
  38. #region MonoBehaviour Messages
  39. /// <summary>
  40. /// MonoBehaviour method called on GameObject by Unity during early initialization phase
  41. /// </summary>
  42. void Awake()
  43. {
  44. _canvasGroup = this.GetComponent<CanvasGroup>();
  45. this.transform.SetParent(GameObject.Find("Canvas").GetComponent<Transform>(), false);
  46. }
  47. /// <summary>
  48. /// MonoBehaviour method called on GameObject by Unity on every frame.
  49. /// update the health slider to reflect the Player's health
  50. /// </summary>
  51. void Update()
  52. {
  53. // Destroy itself if the target is null, It's a fail safe when Photon is destroying Instances of a Player over the network
  54. if (target == null) {
  55. Destroy(this.gameObject);
  56. return;
  57. }
  58. // Reflect the Player Health
  59. if (playerHealthSlider != null) {
  60. playerHealthSlider.value = target.Health;
  61. }
  62. }
  63. /// <summary>
  64. /// MonoBehaviour method called after all Update functions have been called. This is useful to order script execution.
  65. /// In our case since we are following a moving GameObject, we need to proceed after the player was moved during a particular frame.
  66. /// </summary>
  67. void LateUpdate () {
  68. // Do not show the UI if we are not visible to the camera, thus avoid potential bugs with seeing the UI, but not the player itself.
  69. if (targetRenderer!=null)
  70. {
  71. this._canvasGroup.alpha = targetRenderer.isVisible ? 1f : 0f;
  72. }
  73. // #Critical
  74. // Follow the Target GameObject on screen.
  75. if (targetTransform!=null)
  76. {
  77. targetPosition = targetTransform.position;
  78. targetPosition.y += characterControllerHeight;
  79. this.transform.position = Camera.main.WorldToScreenPoint (targetPosition) + screenOffset;
  80. }
  81. }
  82. #endregion
  83. #region Public Methods
  84. /// <summary>
  85. /// Assigns a Player Target to Follow and represent.
  86. /// </summary>
  87. /// <param name="target">Target.</param>
  88. public void SetTarget(PlayerManager _target){
  89. if (_target == null) {
  90. Debug.LogError("<Color=Red><b>Missing</b></Color> PlayMakerManager target for PlayerUI.SetTarget.", this);
  91. return;
  92. }
  93. // Cache references for efficiency because we are going to reuse them.
  94. this.target = _target;
  95. targetTransform = this.target.GetComponent<Transform>();
  96. targetRenderer = this.target.GetComponentInChildren<Renderer>();
  97. CharacterController _characterController = this.target.GetComponent<CharacterController> ();
  98. // Get data from the Player that won't change during the lifetime of this Component
  99. if (_characterController != null){
  100. characterControllerHeight = _characterController.height;
  101. }
  102. if (playerNameText != null) {
  103. playerNameText.text = this.target.photonView.Owner.NickName;
  104. }
  105. }
  106. #endregion
  107. }
  108. }