{"id":297,"date":"2017-09-23T02:07:44","date_gmt":"2017-09-22T17:07:44","guid":{"rendered":"http:\/\/uniblo.biz\/?p=297"},"modified":"2017-09-28T23:38:09","modified_gmt":"2017-09-28T14:38:09","slug":"chapter9_chatdisp","status":"publish","type":"post","link":"https:\/\/uniblo.biz\/unity\/mmogame\/chapter9_chatdisp","title":{"rendered":"Unity\u30aa\u30f3\u30e9\u30a4\u30f3\u30b2\u30fc\u30e0\u4f5c\u6210\u3010Chapter9\u3011\u30c1\u30e3\u30c3\u30c8\u306e\u982d\u4e0a\u8868\u793a"},"content":{"rendered":"
\u524d\u56de\u306eChapter8\u3067\u306f\u30c1\u30e3\u30c3\u30c8\u306e\u5c0e\u5165\u3092\u884c\u3044\u307e\u3057\u305f\u3002<\/p>\n
\u3057\u304b\u3057\u73fe\u5728\u306e\u30c1\u30e3\u30c3\u30c8\u306e\u30ed\u30b0\u306fUI\u4e0a\u306b\u3057\u304b\u8868\u793a\u3055\u308c\u305a\u3001\u7279\u306b\u4eba\u304c\u5897\u3048\u3066\u3044\u3063\u305f\u5834\u5408\u3001\u3069\u306e\u30d7\u30ec\u30a4\u30e4\u30fc\u304c\u3069\u306e\u767a\u8a00\u3092\u3057\u3066\u3044\u308b\u304b\u304c\u30b2\u30fc\u30e0\u753b\u9762\u4e0a\u3067\u306f\u3068\u3066\u3082\u308f\u304b\u308a\u306b\u304f\u3044\u72b6\u614b\u3068\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n
\u305d\u306e\u305f\u3081\u4eca\u56de\u306f\u305d\u306e\u30c1\u30e3\u30c3\u30c8\u3092\u767a\u8a00\u3057\u305f\u30ad\u30e3\u30e9\u30af\u30bf\u30fc\u306e\u982d\u4e0a\u306b\u4e00\u5b9a\u6642\u9593\u8868\u793a\u3059\u308b\u3088\u3046\u306b\u3057\u307e\u3059\u3002<\/p>\n
\u30c1\u30e3\u30c3\u30c8\u306e\u982d\u4e0a\u8868\u793a\u3092\u3059\u308b\u305f\u3081\u306b\u306f\u81ea\u5206\u304cChat\u306eUI\u306b\u5165\u529b\u3057\u305f\u6587\u5b57\u5217\u3092\u81ea\u5206\u306e\u30ad\u30e3\u30e9\u30af\u30bf\u30fc\u306e\u982d\u4e0a\u306b\u8868\u793a\u3057\u306a\u3051\u308c\u3070\u306a\u308a\u307e\u305b\u3093\u3002<\/p>\n
\u3064\u307e\u308a\u30c1\u30e3\u30c3\u30c8\u306e\u982d\u4e0a\u8868\u793a\u306b\u306fChatManager\u2192PlayerPrefab\u2192PlayerUI\u3068\u3044\u3046\uff13\u3064\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u6587\u5b57\u5217\u30c7\u30fc\u30bf\u304c\u6d41\u308c\u308b\u3088\u3046\u306b\u5909\u66f4\u3092\u52a0\u3048\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n
\u3067\u306f\u4ee5\u4e0b\u306e\u624b\u9806\u3067\u5909\u66f4\u3092\u52a0\u3048\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n
\u203b\u8ffd\u52a0\u3057\u305f\u30b3\u30fc\u30c9\u306e\u884c\u306e\u80cc\u666f\u306b\u306f\u8272\u304c\u4ed8\u3044\u3066\u3044\u307e\u3059\u306e\u3067\u53c2\u8003\u306b\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/span><\/p>\n <\/p>\n \u4ee5\u4e0b\u306e\u753b\u50cf\u306e\u3088\u3046\u306bPlayerUI\u306e\u5b50\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3068\u3057\u3066\u30c6\u30ad\u30b9\u30c8\u30aa\u30d6\u30b8\u30a7\u30af\u30c8ChatText\u3092\u751f\u6210\u3057\u307e\u3059\u3002<\/p>\n <\/a><\/p>\n \u751f\u6210\u3057\u305fChatText\u306e\u8a2d\u5b9a\u306f\u4ee5\u4e0b\u306eInspector\u30d3\u30e5\u30fc\u306e\u3088\u3046\u306b\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n \u982d\u4e0a\u30c1\u30e3\u30c3\u30c8\u306e\u6587\u5b57\u306e\u30b5\u30a4\u30ba\u3084\u8272\u3084\u30d5\u30a9\u30f3\u30c8\u306f\u3053\u3053\u3067\u6c7a\u3081\u308b\u3053\u3068\u304c\u51fa\u6765\u308b<\/strong><\/span>\u306e\u3067\u304a\u597d\u307f\u3067\u8a2d\u5b9a\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n \u4eca\u56de\u306f\u898b\u3084\u3059\u3044\u3088\u3046\u306b\u767d\u3067\u7e01\u53d6\u3063\u305f\u9ed2\u592a\u6587\u5b57\u3067\u8868\u793a\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\nInRoomChat.cs\u306e\u5909\u66f4\u5f8c\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9<\/h3>\n
using System.Collections.Generic;\r\nusing UnityEngine;\r\nusing System.Collections;\r\n\r\n[RequireComponent(typeof(PhotonView))]\r\npublic class InRoomChat : Photon.MonoBehaviour\r\n{\r\n #region \u5909\u6570\u5ba3\u8a00\r\n \/\/\u7bc4\u56f2\u30c1\u30e3\u30c3\u30c8\u5b9f\u88c5\u306e\u305f\u3081\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3001\u5909\u6570\u5b9a\u7fa9\r\n GameObject[] players; \/\/\u5168\u3066\u306e\u30d7\u30ec\u30a4\u30e4\u30fc\u30ad\u30e3\u30e9\u53d6\u5f97\u7528\r\n GameObject sender; \/\/\u9001\u4fe1\u30ad\u30e3\u30e9\u53d6\u5f97\u7528\r\n GameObject myPlayer; \/\/\u81ea\u5206\u306e\u30ad\u30e3\u30e9\u53d6\u5f97\u7528\r\n PlayerManager myPM; \/\/\u81ea\u5206\u306e\u30ad\u30e3\u30e9\u306ePlayerManager\u53d6\u5f97\u7528\r\n GUIStyle ChatStyle = new GUIStyle(); \/\/\u7bc4\u56f2\u30c1\u30e3\u30c3\u30c8Style\r\n GUIStyleState ChatStyleState = new GUIStyleState();\r\n GUIStyle AllChatStyle = new GUIStyle(); \/\/\u5168\u4f53\u30c1\u30e3\u30c3\u30c8Style\r\n GUIStyleState AllChatStyleState = new GUIStyleState();\r\n\r\n public Rect GuiRect = new Rect(0, 0, 300, 200); \/\/\u30c1\u30e3\u30c3\u30c8UI\u306e\u5927\u304d\u3055\u8a2d\u5b9a\u7528\r\n public bool IsVisible = true; \/\/\u30c1\u30e3\u30c3\u30c8UI\u8868\u793a\u975e\u8868\u793a\u30d5\u30e9\u30b0\r\n public bool AlignBottom = true;\r\n public List<string> messages = new List<string>(); \/\/\u30c1\u30e3\u30c3\u30c8\u30ed\u30b0\u683c\u7d0d\u7528List\r\n public List<bool> chatKind = new List<bool>(); \/\/\u30c1\u30e3\u30c3\u30c8\u30ed\u30b0\u306e\u7a2e\u985e\u683c\u7d0d\u7528(\u7bc4\u56f2\u30c1\u30e3or\u5168\u30c1\u30e3)\r\n public string inputLine = \"\";\/\/\u5165\u529b\u6587\u7ae0\u683c\u7d0d\u7528String\r\n private Vector2 scrollPos = Vector2.zero; \/\/\u30b9\u30af\u30ed\u30fc\u30eb\u30d0\u30fc\u4f4d\u7f6e\r\n #endregion\r\n\r\n #region Start\u95a2\u6570 Updata\u95a2\u6570\r\n public void Start()\r\n {\r\n \/\/myPlayer\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u53d6\u5f97(\u7bc4\u56f2\u30c1\u30e3\u30c3\u30c8\u767a\u8a00\u6642\u306bposition\u3068myPM\u4f7f\u3046)\r\n GetmyPlayer();\r\n\r\n \/\/\u7bc4\u56f2\u30c1\u30e3\u30c3\u30c8\u306e\u5834\u5408\u306f\u767d\u6587\u5b57\u306b\u3057\u3001\u6587\u5b57\u304c\uff35\uff29\u304b\u3089\u3042\u3075\u308c\u305f\u5834\u5408\u306f\u6298\u308a\u8fd4\u3059\u8a2d\u5b9a\r\n ChatStyleState.textColor = Color.white;\r\n ChatStyle.normal = ChatStyleState;\r\n ChatStyle.wordWrap = true;\r\n \/\/\u5168\u4f53\u30c1\u30e3\u30c3\u30c8\u306e\u5834\u5408\u306f\u8d64\u6587\u5b57\u306b\u3057\u3001\u6587\u5b57\u304c\uff35\uff29\u304b\u3089\u3042\u3075\u308c\u305f\u5834\u5408\u306f\u6298\u308a\u8fd4\u3059\u8a2d\u5b9a\r\n AllChatStyleState.textColor = Color.red;\r\n AllChatStyle.normal = AllChatStyleState;\r\n AllChatStyle.wordWrap = true;\r\n }\r\n\r\n public void Update()\r\n {\r\n \/\/ChatUI\u306e\u4f4d\u7f6e\u3092\u8abf\u6574\r\n this.GuiRect.y = Screen.height - this.GuiRect.height;\r\n \/\/ChatUI\u306e\u5927\u304d\u3055\u8abf\u6574\r\n GuiRect.width = Screen.width \/ 3;\r\n GuiRect.height = Screen.height \/ 3;\r\n }\r\n #endregion\r\n\r\n #region OnGUI\u95a2\u6570\r\n public void OnGUI()\r\n {\r\n if (!this.IsVisible || !PhotonNetwork.inRoom) \/\/\u8868\u793a\u30d5\u30e9\u30b0\u304cOFF\u307e\u305f\u306fphoton\u306b\u3064\u306a\u304c\u3063\u3066\u3044\u306a\u3044\u3068\u304d\r\n {\r\n \/\/UI\u975e\u8868\u793a\r\n return;\r\n }\r\n\r\n \/\/ChatUI\u306e\u4f5c\u6210\u958b\u59cb\r\n \/\/\u30c1\u30e3\u30c3\u30c8UI\u751f\u6210\u3000Begin&EndArea\u3067\u30c1\u30e3\u30c3\u30c8UI\u306e\u4f4d\u7f6e\u3068\u5927\u304d\u3055\u3092\u8a2d\u5b9a \r\n GUILayout.Window(0, GuiRect, ChatUIWindow, \"\"); \/\/\u30c1\u30e3\u30c3\u30c8UI\u30a6\u30a4\u30f3\u30c9\u30a6\u3092\u4f5c\u6210\r\n\r\n \/\/Enter\u3092\u62bc\u3059\u3068\r\n if (Event.current.type == EventType.KeyDown && (Event.current.keyCode == KeyCode.KeypadEnter || Event.current.keyCode == KeyCode.Return))\r\n {\r\n \/\/\u30c1\u30e3\u30c3\u30c8\u5165\u529b\u5f85\u3061\u72b6\u614b\u306b\u3059\u308b\r\n GUI.FocusControl(\"ChatInput\");\r\n }\r\n }\r\n #endregion\r\n\r\n #region \u30c1\u30e3\u30c3\u30c8UI\u751f\u6210\r\n void ChatUIWindow(int windowID)\r\n {\r\n \/\/Focus\u304c\u30c1\u30e3\u30c3\u30c8UI\u306b\u4e57\u3063\u3066\u308b\u3068\u304d\u306bEnter\u3092\u62bc\u3059\u3068\u30c1\u30e3\u30c3\u30c8\u767a\u8a00\u304c\u5b9f\u884c\u3055\u308c\u308b\r\n if (Event.current.type == EventType.KeyDown && (Event.current.keyCode == KeyCode.KeypadEnter || Event.current.keyCode == KeyCode.Return))\r\n {\r\n if (!string.IsNullOrEmpty(this.inputLine)) \/\/\u30c1\u30e3\u30c3\u30c8\u5165\u529b\u6b04\u304cNull\u3084Empty\u3067\u306a\u3044\u5834\u5408\r\n {\r\n \/\/\u7bc4\u56f2\u30c1\u30e3\u30c3\u30c8\u9001\u4fe1\u95a2\u6570\u5b9f\u884c\r\n SendChat(false);\r\n\r\n return;\r\n }\r\n }\r\n\r\n \/\/\u5782\u76f4\u306e\u30b3\u30f3\u30c8\u30ed\u30fc\u30eb\u30b0\u30eb\u30fc\u30d7\u958b\u59cb\r\n GUILayout.BeginVertical();\r\n \/\/\u30b9\u30af\u30ed\u30fc\u30eb\u30d3\u30e5\u30fc\u958b\u59cb\u4f4d\u7f6e\r\n scrollPos = GUILayout.BeginScrollView(scrollPos);\r\n \/\/\u30c1\u30e3\u30c3\u30c8\u30ed\u30b0\u8868\u793a\u7528\u30d5\u30ec\u30ad\u30b7\u30d6\u30eb\u30b9\u30da\u30fc\u30b9\u751f\u6210\r\n GUILayout.FlexibleSpace();\r\n\r\n \/\/\u30d5\u30ec\u30ad\u30b7\u30d6\u30eb\u30b9\u30da\u30fc\u30b9\u306b\u30c1\u30e3\u30c3\u30c8\u30ed\u30b0\u3092\u8868\u793a\r\n for (int i = 0; i <= messages.Count - 1; i++)\r\n {\r\n if (chatKind[i] != true) \/\/\u7bc4\u56f2\u30c1\u30e3\u30c3\u30c8\u3067\u3042\u308c\u3070\r\n {\r\n GUILayout.Label(messages[i], ChatStyle);\r\n }\r\n else \/\/\u5168\u30c1\u30e3\u30c3\u30c8\u3067\u3042\u308c\u3070\r\n {\r\n GUILayout.Label(messages[i], AllChatStyle);\r\n }\r\n }\r\n\r\n \/\/\u30b9\u30af\u30ed\u30fc\u30eb\u30d3\u30e5\u30fc\u7d42\u4e86\r\n GUILayout.EndScrollView();\r\n\r\n \/\/\u6c34\u5e73\u306e\u30b3\u30f3\u30c8\u30ed\u30fc\u30eb\u30b0\u30eb\u30fc\u30d7\u958b\u59cb\r\n GUILayout.BeginHorizontal();\r\n \/\/\u5165\u529b\u30c6\u30ad\u30b9\u30c8\u30d5\u30a3\u30fc\u30eb\u30c9\u751f\u6210\u3001Focus\u304c\u4e57\u3063\u305f\u72b6\u614b\u3092ChatInput\u3068\u547d\u540d\r\n GUI.SetNextControlName(\"ChatInput\");\r\n inputLine = GUILayout.TextField(inputLine, 200);\r\n\r\n \/\/\u300cSend\u300d\u30dc\u30bf\u30f3\u3092\u751f\u6210\u304b\u3064\u62bc\u3057\u305f\u3068\u304d\u306b\u306f\u7bc4\u56f2\u30c1\u30e3\u30c3\u30c8\u9001\u4fe1\r\n if (GUILayout.Button(\"Send\", GUILayout.ExpandWidth(false)))\r\n {\r\n \/\/\u7bc4\u56f2\u30c1\u30e3\u30c3\u30c8\u9001\u4fe1\u95a2\u6570\u5b9f\u884c\r\n SendChat(false);\r\n }\r\n\r\n \/\/All\u30dc\u30bf\u30f3\u3092\u751f\u6210\u304b\u3064\u62bc\u3057\u305f\u3068\u304d\u306b\u306f\u5168\u4f53\u30c1\u30e3\u30c3\u30c8\u9001\u4fe1\r\n if (GUILayout.Button(\"All\", GUILayout.ExpandWidth(false)))\r\n {\r\n \/\/\u5168\u4f53\u30c1\u30e3\u30c3\u30c8\u9001\u4fe1\u95a2\u6570\u5b9f\u884c\r\n SendChat(true);\r\n }\r\n \/\/\u6c34\u5e73\u306e\u30b3\u30f3\u30c8\u30ed\u30fc\u30eb\u30b0\u30eb\u30fc\u30d7\u7d42\u4e86\r\n GUILayout.EndHorizontal();\r\n \/\/\u5782\u76f4\u306e\u30b3\u30f3\u30c8\u30ed\u30fc\u30eb\u30b0\u30eb\u30fc\u30d7\u7d42\u4e86\r\n GUILayout.EndVertical();\r\n }\r\n #endregion\r\n\r\n #region GetmyPlayer \u81ea\u30ad\u30e3\u30e9\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092myPlayer\u306b\u767b\u9332\r\n void GetmyPlayer()\r\n {\r\n \/\/\u81ea\u30ad\u30e3\u30e9\u306eID\u53d6\u5f97\r\n int myPlayerID = PhotonNetwork.player.ID;\r\n\r\n \/\/\u5168\u3066\u306e\u30d7\u30ec\u30a4\u30e4\u30fc\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u53d6\u5f97\r\n players = GameObject.FindGameObjectsWithTag(\"Player\");\r\n \/\/\u5168\u3066\u306e\u30d7\u30ec\u30a4\u30e4\u30fc\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304b\u3089\u81ea\u30ad\u30e3\u30e9\u3092ID\u3067\u691c\u7d22\u3057\u3001\u53d6\u308a\u51fa\u3059\r\n foreach (GameObject player in players)\r\n {\r\n int playerLoopId = player.GetComponent<PhotonView>().owner.ID;\r\n if (playerLoopId == myPlayerID)\r\n {\r\n \/\/\u81ea\u30d7\u30ec\u30a4\u30e4\u30fc\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u53d6\u5f97\r\n myPlayer = player;\r\n \/\/\u81ea\u30ad\u30e3\u30e9\u306e\u982d\u4e0a\u306b\u30c1\u30e3\u30c3\u30c8\u8868\u793a\u3059\u308b\u305f\u3081\u306bPlayerManager\u53d6\u5f97\r\n myPM = myPlayer.GetComponent<PlayerManager>();\r\n }\r\n }\r\n return;\r\n }\r\n #endregion\r\n\r\n #region \u30c1\u30e3\u30c3\u30c8\u9001\u4fe1\u95a2\u6570\r\n void SendChat(bool isAll)\r\n {\r\n \/\/chatRPC\r\n this.photonView.RPC(\"Chat\", PhotonTargets.All, myPlayer.transform.position, this.inputLine, isAll);\r\n \/\/\u982d\u4e0a\u306b\u30c1\u30e3\u30c3\u30c8\u8868\u793a\u3059\u308b\u305f\u3081\u306bstring\u9001\u4fe1\r\n myPM.setChat(this.inputLine);\r\n\r\n \/\/\u9001\u4fe1\u5f8c\u3001\u5165\u529b\u6b04\u3092\u7a7a\u306b\u3057\u3001\u30b9\u30af\u30ed\u30fc\u30eb\u6700\u4e0b\u4f4d\u7f6e\u306b\u79fb\u52d5\r\n this.inputLine = \"\";\r\n scrollPos.y = Mathf.Infinity;\r\n }\r\n #endregion\r\n\r\n #region ChatRPC RPC\u547c\u51fa\u5074\uff1a\u9001\u4fe1\u8005\u3000RPC\u53d7\u4fe1\u5074\uff1a\u53d7\u4fe1\u8005\r\n [PunRPC]\r\n public void Chat(Vector3 senderposition, string newLine, bool isAll, PhotonMessageInfo mi)\r\n {\r\n if (messages.Count >= 100) \/\/\u30c1\u30e3\u30c3\u30c8\u30ed\u30b0\u304c\u591a\u304f\u306a\u3063\u3066\u6765\u305f\u3089\u30ed\u30b0\u3092\u524a\u9664\u3057\u3066\u304b\u3089\u53d7\u4fe1\r\n {\r\n messages.Clear(); \/\/\u5168\u3066\u306e\u30c1\u30e3\u30c3\u30c8\u30ed\u30b0\u3092\u524a\u9664\r\n chatKind.Clear(); \/\/\u5168\u3066\u306e\u30c1\u30e3\u30c3\u30c8\u306e\u7a2e\u985e\u60c5\u5831\u524a\u9664\r\n }\r\n\r\n if (!isAll) \/\/\u7bc4\u56f2\u30c1\u30e3\u3068\u3057\u3066\u53d7\u4fe1\r\n {\r\n \/\/myPlayer\u3068sender\u306e\u8ddd\u96e2\u304b\u3089\u53d7\u4fe1\u3059\u308b\u304b\u5224\u65ad\r\n if (Vector3.Distance(myPlayer.transform.position, senderposition) < 30)\r\n {\r\n \/\/chat\u53d7\u4fe1\r\n ReceiveChat(newLine, isAll, mi);\r\n }\r\n }\r\n else if (isAll) \/\/\u5168\u30c1\u30e3\u3068\u3057\u3066\u53d7\u4fe1\r\n {\r\n \/\/chat\u53d7\u4fe1\r\n ReceiveChat(newLine, isAll, mi);\r\n }\r\n \/\/\u53d7\u4fe1\u3057\u305f\u3068\u304d\u306f\u30b9\u30af\u30ed\u30fc\u30eb\u6700\u4e0b\u4f4d\u7f6e\r\n scrollPos.y = Mathf.Infinity;\r\n }\r\n #endregion\r\n\r\n #region \u30c1\u30e3\u30c3\u30c8\u53d7\u4fe1\u95a2\u6570\r\n void ReceiveChat(string _newLine, bool isAll, PhotonMessageInfo _mi)\r\n {\r\n \/\/\u9001\u4fe1\u8005\u306e\u540d\u524d\u7528\u5909\u6570\r\n string senderName = \"anonymous\";\r\n if (_mi.sender != null)\r\n {\r\n \/\/\u9001\u4fe1\u8005\u306e\u540d\u524d\u304c\u3042\u308c\u3070\r\n if (!string.IsNullOrEmpty(_mi.sender.NickName))\r\n {\r\n senderName = _mi.sender.NickName;\r\n }\r\n else\r\n {\r\n senderName = \"player \" + _mi.sender.ID;\r\n }\r\n }\r\n \/\/\u53d7\u4fe1\u3057\u305f\u30c1\u30e3\u30c3\u30c8\u3092\u30ed\u30b0\u306b\u8ffd\u52a0\r\n this.messages.Add(senderName + \": \" + _newLine);\r\n this.chatKind.Add(isAll);\r\n\r\n return;\r\n }\r\n #endregion\r\n}<\/pre>\n
PlayerManager.cs\u306e\u5909\u66f4\u5f8c\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9<\/h3>\n
using UnityEngine;\r\nusing UnityEngine.UI;\r\nusing UnityEngine.EventSystems;\r\nusing System.Collections;\r\nusing System;\r\n\r\n\r\npublic class PlayerManager : Photon.PunBehaviour, IPunObservable\r\n{\r\n \/\/\u982d\u4e0a\u306eUI\u306ePrefab\r\n public GameObject PlayerUiPrefab;\r\n\r\n \/\/\u73fe\u5728\u306eHP\r\n public int HP = 100;\r\n \r\n \/\/Local\u306e\u30d7\u30ec\u30a4\u30e4\u30fc\u3092\u8a2d\u5b9a\r\n public static GameObject LocalPlayerInstance;\r\n\r\n \/\/\u30c1\u30e3\u30c3\u30c8\u540c\u671f\u7528\u5909\u6570\r\n public string ChatText = \"\";\r\n private bool isRunning;\r\n Coroutine ChatCoroutine;\r\n\r\n \/\/\u982d\u4e0aUI\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\r\n GameObject _uiGo;\r\n\r\n #region \u30d7\u30ec\u30a4\u30e4\u30fc\u521d\u671f\u8a2d\u5b9a\r\n void Awake()\r\n {\r\n if (photonView.isMine)\r\n {\r\n PlayerManager.LocalPlayerInstance = this.gameObject;\r\n }\r\n }\r\n #endregion\r\n\r\n #region \u982d\u4e0aUI\u306e\u751f\u6210\r\n void Start()\r\n {\r\n if (PlayerUiPrefab != null) \/\/PlayerUIPrefab\u304c\u8a2d\u5b9a\u3055\u308c\u3066\u3044\u306a\u3044\u6642\u306e\u30a8\u30e9\u30fc\u56de\u907f\r\n {\r\n \/\/Player\u306e\u982d\u4e0aUI\u306e\u751f\u6210\u3068PlayerUIScript\u3067\u306eSetTarget\u95a2\u6570\u547c\u51fa\r\n _uiGo = Instantiate(PlayerUiPrefab) as GameObject;\r\n _uiGo.SendMessage(\"SetTarget\", this, SendMessageOptions.RequireReceiver);\r\n }\r\n else\r\n {\r\n Debug.LogWarning(\"<Color=Red><a>Missing<\/a><\/Color> PlayerUiPrefab reference on player Prefab.\", this);\r\n }\r\n }\r\n #endregion\r\n\r\n void Update()\r\n {\r\n \/\/\u3053\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304cLocal\u3067\u306a\u3051\u308c\u3070\u5b9f\u884c\u3057\u306a\u3044\r\n if (!photonView.isMine) \r\n {\r\n return;\r\n }\r\n \/\/LocalVariables\u3092\u53c2\u7167\u3057\u3001\u73fe\u5728\u306eHP\u3092\u66f4\u65b0\r\n HP = LocalVariables.currentHP;\r\n }\r\n\r\n #region \u982d\u4e0aChat\u306e\u8868\u793a\r\n public void setChat(string inputLine)\r\n {\r\n \/\/\u30b3\u30eb\u30fc\u30c1\u30f3\u304c\u52d5\u4f5c\u4e2d\u3067\u3042\u308c\u3070\r\n if (isRunning)\r\n {\r\n StopCoroutine(ChatCoroutine); \/\/\u30b3\u30eb\u30fc\u30c1\u30f3\u3092\u505c\u6b62\r\n ChatCoroutine = null; \/\/\u524a\u9664\r\n isRunning = false; \r\n }\r\n \r\n \/\/\u982d\u4e0a\u30c1\u30e3\u30c3\u30c8\u7528String\u306b\u5165\u529b\u6587\u5b57\u5217\u3092\u683c\u7d0d\r\n ChatText = inputLine;\r\n \/\/\u65b0\u3057\u3044\u30b3\u30eb\u30fc\u30c1\u30f3\u3092\u751f\u6210\r\n ChatCoroutine = StartCoroutine(_ChatText(6f));\r\n }\r\n\r\n \/\/\u982d\u4e0a\u30c1\u30e3\u30c3\u30c8\u8868\u793a\u7528\u30b3\u30eb\u30fc\u30c1\u30f3\r\n IEnumerator _ChatText(float pausetime)\r\n { \r\n \/\/\u30b3\u30eb\u30fc\u30c1\u30f3\u52d5\u4f5c\u30d5\u30e9\u30b0ON\r\n isRunning = true;\r\n \/\/pausetime\u306e\u9593\u3001\u982d\u4e0a\u30c1\u30e3\u30c3\u30c8\u3092\u8868\u793a\u3057\u7d9a\u3051\u308b(\u4eca\u306f6\u79d2\u306b\u8a2d\u5b9a)\r\n yield return new WaitForSeconds(pausetime);\r\n \/\/\u982d\u4e0a\u30c1\u30e3\u30c3\u30c8\u3092\u975e\u8868\u793a\u306b\u3059\u308b\r\n ChatText = \"\";\r\n \/\/\u30b3\u30eb\u30fc\u30c1\u30f3\u52d5\u4f5c\u30d5\u30e9\u30b0OFF\r\n isRunning = false;\r\n }\r\n #endregion\r\n\r\n #region OnPhotonSerializeView\u540c\u671f\r\n \/\/\u30d7\u30ec\u30a4\u30e4\u30fc\u306eHP,\u30c1\u30e3\u30c3\u30c8\u3092PlayerUIScript\u3068\u540c\u671f\r\n public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info) \r\n {\r\n if (stream.isWriting) \/\/\u9001\u4fe1\r\n {\r\n stream.SendNext(this.HP);\r\n stream.SendNext(this.ChatText);\r\n }\r\n else \/\/\u53d7\u4fe1\r\n {\r\n this.HP = (int)stream.ReceiveNext();\r\n this.ChatText = (string)stream.ReceiveNext();\r\n }\r\n }\r\n #endregion\r\n}\r\n<\/pre>\n
PlayerUIScript.cs\u306e\u5909\u66f4\u5f8c\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9<\/h3>\n
using UnityEngine;\r\nusing UnityEngine.UI;\r\nusing System.Collections;\r\n\r\npublic class PlayerUIScript : MonoBehaviour\r\n{\r\n #region Public Properties\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 ChatText;\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 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 (ChatText != null)\r\n {\r\n ChatText.text = _target.ChatText;\r\n }\r\n }\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 #endregion\r\n\r\n #region PlayerUI\u304c\u8ffd\u8de1\u3059\u308btarget\u304b\u3089\u60c5\u5831\u3092\u8a2d\u5b9a\u3059\u308b\u95a2\u6570\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\u53d6\u5f97\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; \/\/\u30d7\u30ec\u30a4\u30e4\u30fc\u540d\r\n }\r\n if (PlayerHPSlider != null)\r\n {\r\n PlayerHPSlider.value = _target.HP; \/\/HP\r\n }\r\n if (ChatText != null)\r\n {\r\n ChatText.text = _target.ChatText; \/\/\u982d\u4e0a\u30c1\u30e3\u30c3\u30c8Text\r\n }\r\n }\r\n #endregion\r\n}<\/pre>\n
PlayerUIPrefab\u306b\u982d\u4e0aChat\u8868\u793a\u7528\u306b\u5b50\u30c6\u30ad\u30b9\u30c8\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3068\u3057\u3066ChatText\u3092\u8ffd\u52a0<\/h3>\n