{"id":198,"date":"2017-09-19T02:46:23","date_gmt":"2017-09-18T17:46:23","guid":{"rendered":"http:\/\/uniblo.biz\/?p=198"},"modified":"2017-09-28T23:36:29","modified_gmt":"2017-09-28T14:36:29","slug":"chapter7_playerui","status":"publish","type":"post","link":"https:\/\/uniblo.biz\/unity\/mmogame\/chapter7_playerui","title":{"rendered":"Unity\u30aa\u30f3\u30e9\u30a4\u30f3\u30b2\u30fc\u30e0\u4f5c\u6210\u3010Chapter7\u3011\u30d7\u30ec\u30a4\u30e4\u30fc\u982d\u4e0a\uff35\uff29\u306e\u4f5c\u6210"},"content":{"rendered":"
\u672cChapter\u3067\u306f\u4ee5\u4e0b\u306e\u753b\u50cf\u306e\u3088\u3046\u306b\u982d\u4e0a\u306b\u30d7\u30ec\u30a4\u30e4\u30fc\u540d\u3068HP\u3092\u8868\u793a\u3059\u308bUI\u306e\u4f5c\u6210\u3092\u884c\u3044\u307e\u3059\u3002<\/p>\n
<\/a><\/p>\n \u3053\u306e\u982d\u4e0aUI\u3092\u8868\u793a\u3059\u308b\u305f\u3081\u306b\u306f\u307e\u305a\u982d\u4e0aUI\u3092\u8868\u793a\u3059\u308b\u30bf\u30a4\u30df\u30f3\u30b0\u3092\u8003\u3048\u307e\u3059\u3002<\/p>\n \u3053\u306eUI\u306f\u30d7\u30ec\u30a4\u30e4\u30fc\u304c\u30ed\u30b0\u30a4\u30f3\u3057\u3001\u305d\u306ePlayer\u306e\u30ad\u30e3\u30e9\u30af\u30bf\u30fc\u304c\u751f\u6210\u3055\u308c\u308b\u306e\u3068\u307b\u307c\u540c\u6642\u306b\u751f\u6210\u3055\u308c\u3001\u30d7\u30ec\u30a4\u30e4\u30fc\u304c\u30ed\u30b0\u30a2\u30a6\u30c8\u3059\u308b\u306e\u3068\u307b\u307c\u540c\u6642\u306b\u524a\u9664\u3055\u308c\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n \u3064\u307e\u308a\u982d\u4e0aUI\u306ePrefab\u3092\u4f5c\u6210\u3057\u3001\u30d7\u30ec\u30a4\u30e4\u30fcPrefab\u306b\u982d\u4e0aUI\u3092\u751f\u6210\u7528\u306e\u30b9\u30af\u30ea\u30d7\u30c8\u3092\u7528\u610f\u3057\u3001\u30d7\u30ec\u30a4\u30e4\u30fc\u306e\u751f\u6210\u6642\u306b\u982d\u4e0aUI\u3092\u751f\u6210\u3059\u308b\u3053\u3068\u3067\u8868\u793a\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n \u307e\u3068\u3081\u308b\u3068\u982d\u4e0aUI\u3092\u751f\u6210\u3059\u308b\u305f\u3081\u306e\u624b\u9806\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\u3002<\/p>\n \u982d\u4e0aUIPrefab\u306e\u69cb\u6210\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n <\/a><\/p>\n \u4f5c\u6210\u624b\u9806\u3068\u3057\u3066\u306f<\/p>\n <\/a><\/p>\n <\/a><\/p>\n <\/a><\/p>\n <\/a><\/p>\n\u982d\u4e0aUI\u306e\u5c0e\u5165\u65b9\u6cd5<\/h2>\n
\n
\u982d\u4e0aUI\u306ePrefab\u4f5c\u6210<\/h2>\n
\n
PlayerUIScript.cs\u306e\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9<\/h3>\n
using UnityEngine;\r\nusing UnityEngine.UI;\r\n\r\nusing System.Collections;\r\n\r\n public class PlayerUIScript : MonoBehaviour\r\n {\r\n\r\n #region Public Properties\r\n\r\n \/\/\u30ad\u30e3\u30e9\u306e\u982d\u4e0a\u306b\u4e57\u308b\u3088\u3046\u306b\u8abf\u6574\u3059\u308b\u305f\u3081\u306eOffset\r\n public Vector3 ScreenOffset = new Vector3(0f, 30f, 0f);\r\n\r\n \/\/\u30d7\u30ec\u30a4\u30e4\u30fc\u540d\u524d\u8a2d\u5b9a\u7528Text\r\n public Text PlayerNameText;\r\n\r\n \/\/\u30d7\u30ec\u30a4\u30e4\u30fc\u306eHP\u7528Slider\r\n public Slider PlayerHPSlider;\r\n\r\n \/\/\u30d7\u30ec\u30a4\u30e4\u30fc\u306e\u30c1\u30e3\u30c3\u30c8\u7528Text\r\n \/\/public Text PlayerChatText;\r\n\r\n #endregion\r\n\r\n #region Private Properties\r\n \/\/\u8ffd\u5f93\u3059\u308b\u30ad\u30e3\u30e9\u306ePlayerManager\u60c5\u5831\r\n PlayerManager _target;\r\n float _characterControllerHeight;\r\n Transform _targetTransform;\r\n Vector3 _targetPosition;\r\n #endregion\r\n\r\n #region MonoBehaviour Messages\r\n\r\n void Awake()\r\n {\r\n \/\/\u3053\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306fCanvas\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u5b50\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3068\u3057\u3066\u751f\u6210\r\n this.GetComponent<Transform>().SetParent(GameObject.Find(\"Canvas\").GetComponent<Transform>());\r\n }\r\n\r\n void Update()\r\n {\r\n \/\/\u3082\u3057Player\u304c\u3044\u306a\u304f\u306a\u3063\u305f\u3089\u3053\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3082\u524a\u9664\r\n if (_target == null)\r\n {\r\n Destroy(this.gameObject);\r\n return;\r\n }\r\n\r\n \/\/ \u73fe\u5728\u306eHP\u3092Slider\u306b\u9069\u7528\r\n if (PlayerHPSlider != null)\r\n {\r\n PlayerHPSlider.value = _target.HP;\r\n }\r\n\r\n \/\/ \u982d\u4e0a\u30c1\u30e3\u30c3\u30c8\u3092\u8868\u793a\r\n \/\/if (PlayerChatText != null)\r\n \/\/{\r\n \/\/ PlayerChatText.text = _target.ChatText;\r\n \/\/}\r\n\r\n }\r\n\r\n #endregion\r\n\r\n void LateUpdate()\r\n {\r\n \/\/target\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u8ffd\u8de1\u3059\u308b\r\n if (_targetTransform != null)\r\n {\r\n _targetPosition = _targetTransform.position; \/\/\u4e09\u6b21\u5143\u7a7a\u9593\u4e0a\u306etarget\u306e\u5ea7\u6a19\u3092\u5f97\u308b\r\n _targetPosition.y += _characterControllerHeight; \/\/\u30ad\u30e3\u30e9\u30af\u30bf\u30fc\u306e\u80cc\u306e\u9ad8\u3055\u3092\u8003\u616e\u3059\u308b\r\n \/\/target\u306e\u5ea7\u6a19\u304b\u3089\u982d\u4e0aUI\u306e\u753b\u9762\u4e0a\u306e\u4e8c\u6b21\u5143\u5ea7\u6a19\u3092\u8a08\u7b97\u3057\u3066\u79fb\u52d5\u3055\u305b\u308b\r\n this.transform.position = Camera.main.WorldToScreenPoint(_targetPosition) + ScreenOffset; \r\n }\r\n }\r\n\r\n #region Public Methods\r\n public void SetTarget(PlayerManager target)\r\n {\r\n if (target == null)\/\/target\u304c\u3044\u306a\u3051\u308c\u3070\u30a8\u30e9\u30fc\u3092Console\u306b\u8868\u793a\r\n {\r\n Debug.LogError(\"<Color=Red><a>Missing<\/a><\/Color> PlayMakerManager target for PlayerUI.SetTarget.\", this);\r\n return;\r\n }\r\n \/\/target\u306e\u60c5\u5831\u3092\u3053\u306e\u30b9\u30af\u30ea\u30d7\u30c8\u5185\u3067\u4f7f\u3046\u306e\u3067\u30b3\u30d4\u30fc\r\n _target = target;\r\n _targetTransform = _target.GetComponent<Transform>();\r\n\r\n CharacterController _characterController = _target.GetComponent<CharacterController>();\r\n \r\n \/\/PlayerManager\u306e\u982d\u4e0aUI\u306b\u8868\u793a\u3057\u305f\u3044\u30c7\u30fc\u30bf\u3092\u30b3\u30d4\u30fc\r\n if (_characterController != null)\r\n {\r\n _characterControllerHeight = _characterController.height;\r\n }\r\n\r\n if (PlayerNameText != null)\r\n {\r\n PlayerNameText.text = _target.photonView.owner.NickName;\r\n }\r\n if (PlayerHPSlider != null)\r\n {\r\n PlayerHPSlider.value = _target.HP;\r\n }\r\n \/\/if (PlayerChatText != null)\r\n \/\/{\r\n \/\/ PlayerChatText.text = _target.ChatText;\r\n \/\/}\r\n }\r\n #endregion\r\n}<\/pre>\n
PlayerUI\u306eInspector\u30d3\u30e5\u30fc<\/h3>\n
PlayerUI\u306e\u5b50\u30aa\u30d6\u30b8\u30a7\u30af\u30c8Background\u306einspector\u30d3\u30e5\u30fc<\/h3>\n
PlayerUI\u306e\u5b50\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306eFillArea\u306einspector\u30d3\u30e5\u30fc<\/h3>\n
PlayerUI\u306e\u5b50\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306eFillArea\u306e\u5b50\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306eFill\u306einspector\u30d3\u30e5\u30fc<\/h3>\n
PlayerUI\u306e\u5b50\u30aa\u30d6\u30b8\u30a7\u30af\u30c8PlayerNameText\u306einspector\u30d3\u30e5\u30fc<\/h3>\n