Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[Help!] Chat Scrollview from Bottom to Top - Scripting

Discussion in 'Scripting' started by ExbowFTW, Jun 22, 2015.

  1. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
    Hey guys... I am new to the Unity Forum Community, but I need some "basic" help with my script:
    I found this script (down below) from the Unity Asset Store (called Unity Multiplayer Chat). The whole code works and is fully functional - however some of the features I want to change.

    I changed the code so that now whenever you talk, your message goes below the previous messages, kind of like Minecraft's chat box. However, whenever I talk, the scrollbar won't auto - scroll down to show the message you just wrote, or a message that another player wrote. It just stays at the very top of the scrollbar, forcing you to scroll down manually to see the most-recent message.

    I'd hopefully like to change that so it will auto-scroll down to the most recent message posted, making it exactly like a Minecraft chat box. In the picture below, you can tell by the scroll bar that there are more messages below. However, the scrollbar doesn't automatically scroll to them - you have to do it yourself.

    Picture:
    Screen Shot 2015-06-21 at 8.51.48 PM.png

    This is the code that I need to edit - it contains the scrollbar stuff:

    Code (csharp):
    1.  
    2. scrollPos = GUILayout.BeginScrollView(scrollPos); //Scroll View begins... it's position is scrollPos
    3. GUILayout.FlexibleSpace();
    4. for (inti = 0; i < messages.Count; i++)
    5.  {
    6. GUILayout.BeginHorizontal("Box");
    7. if (messages[i].name == User)
    8.  {
    9. GUI.color = newColor(My_Color.r, My_Color.g, My_Color.b, m_alpha);
    10.  }
    11. else
    12.  {
    13. GUI.color = newColor(messages[i].color.r, messages[i].color.g, messages[i].color.b, m_alpha);
    14.  
    15.  }
    16. GUILayout.Label(""+messages[i].name+"");
    17. GUILayout.Space(5);
    18. GUI.color = newColor(1, 1, 1, m_alpha);
    19. GUILayout.Label(messages[i].text);
    20. GUILayout.EndHorizontal();
    21.  }
    22. GUILayout.EndScrollView();                                        //Scroll View ends here
    23.  
    24.  
    To fix this I think you may need to make the scrollPos at the end of the Scroll View to make it start at the bottom.. however I'm really not sure how to do it - I only started Unity about 1 month ago. Just ignore most of the color stuff, since that is for the chat color.

    Thanks for all your help, guys <3 :D
     
  2. Mabenan

    Mabenan

    Joined:
    Feb 7, 2014
    Posts:
    132
  3. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
  4. Mabenan

    Mabenan

    Joined:
    Feb 7, 2014
    Posts:
    132
  5. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
    You mean the drag-and drop one in the Inspector? Yeah, I did that for my Main Menu Scene. But doing it manually for a chat box seemed harder... so I just found some code from the Asset Store that worked ;)
     
  6. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
    Is there a way to make all the chat messages formatted to the left side, next to the name, instead of centered in the middle (look at the chat message picture above). Like this:

    EXBOW: HEY
    EXBOW: WHAT'S UP

    Instead of:

    EXBOW: ______HEY (I used the ____ the space it out)
    EXBOW: ___WHAT'S UP

    Here's the whole entire code... most of it doesn't matter - I just need to know where the part is that makes the text centered, and then how to fix that to make it formatted to the left side... THANKS FOR HELPING:

    Code (csharp):
    1.  
    2. ////////////////////////////////CODEWRITTEBYBRINERLOVOGAMES2014/////////////////////
    3. //////////UMCCHATSYSTEM////////////////////////////////////////////////////////////////
    4. ///////////////INASCENEWITHASERVERREADY////////////////////////////////////////////
    5. ///////////PUTTHISSCRIPTANDAGOWITHANETWORKVIEW///////////////////////////////////
    6. /////////////////////////////////ANDREADYTOCHAT//////////////////////////////////////
    7. /////////////////////////////////////////////////////////////////////////////////////////
    8. usingUnityEngine;
    9. usingSystem.Collections;
    10. usingSystem.Collections.Generic;
    11.  
    12. [RequireComponent(typeof(NetworkView))]
    13. [RequireComponent(typeof(AudioSource))]
    14. publicclassbl_MultiplayerChat : MonoBehaviour {
    15.  
    16.  
    17. publicstructChatData
    18. {
    19. publicstringname { get; set; } //Nameofsender
    20. publicstringtext { get; set; } //Messagetext
    21. publicColorcolor { get; set; } //Sendercolor
    22. publicfloattimer { get; set; } //Removemessageaftercertaintime
    23.  
    24. publicChatData(stringstring1, stringstring2, Colorcolor1, floattimer1)
    25. {
    26. name = string1;
    27. text = string2;
    28. color = color1;
    29. timer = timer1;
    30. }
    31.  
    32. }
    33.  
    34. publicNetworkViewnet;
    35. ///<summary>
    36. ///ListwithalMessagesinRoom
    37. ///</summary>
    38. publicList<ChatData> messages = newList<ChatData>();
    39. publicstringplist;
    40. publicstringgameversion = "Version: 3.5.1 Beta";
    41. ///<summary>
    42. ///SkinforGUIs
    43. ///</summary>
    44. publicGUISkinm_Skin = null;
    45. ///<summary>
    46. ///Colorforplayernamedysplay
    47. ///</summary>
    48. publicAudioClipSend_Sound;
    49. publicAudioSourceaudioS;
    50.  
    51. publicColorMy_Color;
    52. publicColorm_Color;
    53. publicstaticColorSender_Color;
    54. ///<summary>
    55. ///GetcurrentChatinRoom
    56. ///</summary>
    57. publicstaticbl_MultiplayerChatchat;
    58. ///<summary>
    59. ///ChatGUIHeight
    60. ///</summary>
    61. publicintm_Height = 140;
    62. ///<summary>
    63. ///ScrollforChatlist
    64. ///</summary>
    65. privateVector2scrollPos = Vector2.zero;
    66. ///<summary>
    67. ///Widhtofchatgui
    68. ///</summary>
    69. publicintm_Widht = 250;
    70. ///<summary>
    71. ///Maxwordforsendinchat
    72. ///</summary>
    73. publicintm_Max_Lengh = 15;
    74. ///<summary>
    75. ///StringforSendnewMessage
    76. ///</summary>
    77. privatestringchatInput = "";
    78. ///<summary>
    79. ///its "Apply_alpha" enabledapplythistimeforguichatfade
    80. ///</summary>
    81. publicfloatm_Time_To_Fade = 7;
    82. privatefloatm_alpha = 5;
    83. ///<summary>
    84. ///Timeforremoveoldmessages
    85. ///</summary>
    86. publicfloatm_Remove_Time = 20;
    87. staticfloatRemove_Time ;
    88.  
    89. ///<summary>
    90. ///itsfalse = guinotfade.
    91. ///</summary>
    92. publicboolApply_alpha = true;
    93. ///<summary>
    94. ///itsfalse = oldmessagesnotremovefromchat
    95. ///</summary>
    96. publicboolRemove_OnTime = true;
    97. privatefloatlastUnfocusTime = 0;
    98. ///<summary>
    99. ///PlayerName + Backup
    100. ///</summary>
    101. privatestringUser;
    102. privatestringUserBackUp;
    103. ///<summary>
    104. ///filtertoavoidunwantedword
    105. ///createnewstringtofilteradding: ,"word_to_filter" (allinlowercase).
    106. ///</summary>
    107. privatestring[] blacklist = newstring[] {
    108. //"wrong", "test","nopermit","<",
    109. "F***","damn","S***","hell","bitch","nigga"   //Blacklisted words! Program purpose sonly... No bad intentions! Sorry!
    110. };
    111. privateboolm_isEnabled = true;
    112.  
    113.  
    114.  
    115. ///<summary>
    116. ///GetPlayerName, SendinaPlayerPrefsinotherSceneforexampleaLobby
    117. ///</summary>
    118. voidStart()
    119. {
    120. audioS = GetComponent<AudioSource> ();
    121. net = GetComponent<NetworkView> ();
    122. if(PlayerPrefs.HasKey("UserName")){
    123. User = PlayerPrefs.GetString("UserName");
    124. UserBackUp = User;
    125.  
    126. }else{
    127. User = "Player"+Random.Range(0,999);
    128. UserBackUp = User;
    129. }
    130. Sender_Color = m_Color;
    131. Remove_Time = m_Remove_Time;
    132. }
    133.  
    134. voidAwake()
    135. {
    136. chat = this;
    137. }
    138.  
    139. voidUpdate()
    140. {
    141.  
    142.  
    143.  
    144.  
    145. if (Remove_OnTime)
    146. {
    147. //Removechatmessageaftertimerreach0
    148. for (inti = 0; i < messages.Count; i++)
    149. {
    150. ChatDataMInfo = messages[i];
    151. MInfo.timer -= Time.deltaTime;
    152. if (MInfo.timer > 0)
    153. {
    154. messages[i] = newChatData(MInfo.name, MInfo.text, MInfo.color, MInfo.timer);
    155. }
    156. else
    157. {
    158. messages.RemoveAt(i);
    159. }
    160. }
    161. }
    162. if (!Apply_alpha || !Network.isClient && !Network.isServer)
    163. return;
    164.  
    165. if (m_alpha > 0.0f)
    166. {
    167. m_alpha -= Time.deltaTime;
    168. }
    169. if (m_isEnabled)
    170. m_alpha = m_Time_To_Fade;
    171. }
    172.  
    173. voidOnGUI()
    174. {
    175. if (!Network.isClient && !Network.isServer)
    176. return;
    177.  
    178. GUI.skin = m_Skin;
    179. GUI.color = newColor(1, 1, 1, m_alpha);
    180. GUI.SetNextControlName("");
    181. //usethisforstaticchat
    182. GUILayout.BeginArea(newRect(0, Screen.height - m_Height, m_Widht+ 30, m_Height),"Chat Room");
    183. GUILayout.BeginHorizontal();
    184. GUILayout.BeginVertical("window");
    185. scrollPos = GUILayout.BeginScrollView(scrollPos);
    186. GUILayout.FlexibleSpace();
    187. for (inti = messages.Count - 1; i >= 0; i--) //Thismakesthemessagestypedgofrom
    188. //for (inti = 0; i < messages.Count; i++)
    189. {
    190. GUILayout.BeginHorizontal("Box");
    191. if (messages[i].name == User)
    192. {
    193. GUI.color = newColor(My_Color.r, My_Color.g, My_Color.b, m_alpha);
    194. }
    195. else
    196. {
    197. GUI.color = newColor(messages[i].color.r, messages[i].color.g, messages[i].color.b, m_alpha);
    198.  
    199. }
    200. GUILayout.Label(""+messages[i].name+"");
    201. GUILayout.Space(5);
    202. GUI.color = newColor(1, 1, 1, m_alpha);
    203. GUILayout.Label(messages[i].text);
    204. GUILayout.EndHorizontal();
    205. }
    206. GUILayout.EndScrollView();
    207. GUILayout.EndVertical();
    208. GUI.color = newColor(1, 0, 0, m_alpha);
    209. if (GUILayout.Button("X",GUILayout.Width(30),GUILayout.Height(m_Height-35)))
    210. {
    211. m_alpha = 0;
    212. m_isEnabled = false;
    213. GUI.FocusControl("");
    214. GUI.UnfocusWindow();
    215. }
    216. GUILayout.EndHorizontal();
    217. GUI.color = newColor(1, 1, 1, m_alpha);
    218. GUILayout.BeginHorizontal("Box");
    219.  
    220. if (m_isEnabled)
    221. {
    222. GUI.color = Color.white;
    223. GUI.SetNextControlName("ChatField");
    224. chatInput = GUILayout.TextField(chatInput, m_Max_Lengh, GUILayout.MinWidth(225));
    225. if (GUILayout.Button("SEND", GUILayout.Height(25), GUILayout.Width(69)))
    226. {
    227. SendChat(RPCMode.All);
    228. }
    229. if (GUILayout.Button("CLOSET", GUILayout.Height(25), GUILayout.Width(69)))
    230. {
    231. m_isEnabled = false;
    232. }
    233. if (Apply_alpha)
    234. {
    235. GUI.color = Color.green;
    236. }
    237. else
    238. {
    239. GUI.color = Color.red;
    240. }
    241. if (GUILayout.Button("FADE", GUILayout.Height(25), GUILayout.Width(69)))
    242. {
    243. Apply_alpha = !Apply_alpha;
    244. }
    245.  
    246.  
    247. }
    248. else
    249. {
    250. GUI.SetNextControlName("");
    251. }
    252.  
    253. if (Event.current.type == EventType.keyDown && Event.current.character == '\n' && m_isEnabled)
    254. {
    255.  
    256. if (GUI.GetNameOfFocusedControl() == "ChatField")
    257. {
    258. if (chatInput.Length > 0)
    259. {
    260. SendChat(RPCMode.All);
    261. //lastUnfocusTime = Time.time;
    262. GUI.FocusControl("ChatField");
    263. }
    264. else
    265. {
    266. m_isEnabled = false;
    267. GUI.SetNextControlName("");
    268. GUI.FocusControl("");
    269. GUI.UnfocusWindow();
    270. }
    271. }
    272. else
    273. {
    274. if (lastUnfocusTime < Time.time - 0.1f)
    275. {
    276. GUI.FocusControl("ChatField");
    277. }
    278. }
    279. }
    280. elseif (Event.current.type == EventType.keyDown && Event.current.character == '\n' && !m_isEnabled)
    281. {
    282. m_isEnabled = true;
    283. GUI.FocusControl("ChatField");
    284. }
    285.  
    286. GUILayout.FlexibleSpace();
    287. GUILayout.EndHorizontal();
    288. GUILayout.EndArea();
    289. }
    290.  
    291. voidOnJoinedRoom(){
    292. chatInput = User + " joined the game!";
    293. SendChatServer(RPCMode.All);
    294. }
    295.  
    296. voidOnPhotonPlayerDisconnected(){
    297. chatInput = User + " left the game!";
    298. SendChatServer(RPCMode.All);
    299. }
    300.  
    301.  
    302. voidPlayerlist(stringlist)
    303. {
    304. plist = list;
    305. }
    306.  
    307.  
    308.  
    309. ///<summary>
    310. ///Sendanewmessagetoserverforallplayer
    311. ///</summary>
    312. ///<paramname="text">textforshowinchat</param>
    313. publicstaticvoidAddMessage(stringtext,stringsender)
    314. {
    315. Colorcolor = newColor();
    316. color = Sender_Color;
    317. chat.messages.Add( newChatData( sender, text,color,Remove_Time));
    318. if (chat.messages.Count > 15)
    319. chat.messages.RemoveAt(0);
    320. }
    321.  
    322.  
    323. [RPC]
    324. voidSendChatMessage(stringtext,stringname)
    325. {
    326. m_alpha = m_Time_To_Fade;
    327. AddMessage(text,name);
    328. if (Send_Sound != null)
    329. {
    330. audioS.clip = Send_Sound;
    331. audioS.Play();
    332. }
    333. }
    334.  
    335. ///<summary>
    336. ///GetandSendanewmessageandapplyfilterstringforwronglabels
    337. ///</summary>
    338. ///<paramname="target"> Networkingtargets</param>
    339. voidSendChat( RPCModetarget)
    340. {
    341. if (chatInput != "" && chatInput != "/p" && chatInput != "/players" && chatInput != "/playerlist" && chatInput != "/plist" && chatInput != "/list" && chatInput != "/P" && chatInput != "/PLAYERS" && chatInput != "/PLAYERLIST" && chatInput != "/PLIST" && chatInput != "/LIST" && chatInput != "/v" && chatInput != "/V" && chatInput != "/version" && chatInput != "/VERSION" && chatInput != "/QUIT" && chatInput != "/quit" && chatInput != "/love" && chatInput != "/LOVE" && chatInput != "/devs" && chatInput != "/DEVS" && chatInput != "/lobby" && chatInput != "/LOBBY")
    342. {
    343.  
    344. foreach (stringfilterinthis.blacklist)
    345. {
    346. if (chatInput.ToLower().IndexOf(filter) != -1)
    347. {
    348. chatInput = chatInput.ToLower().Replace(filter, "****");
    349. }
    350. }
    351.  
    352. GetComponent<NetworkView>().RPC("SendChatMessage", target, chatInput,User);
    353. chatInput = "";
    354. }
    355.  
    356. //In-Game Chat Commands:
    357.  
    358. if ((chatInput == "/P" || chatInput == "/PLAYERS" || chatInput == "/PLAYERLIST" || chatInput == "/PLIST" || chatInput == "/LIST" || chatInput == "/p" || chatInput == "/players" || chatInput == "/playerlist" || chatInput == "/plist" || chatInput == "/list") && chatInput != "/v" && chatInput != "/V" && chatInput != "/version" && chatInput != "/VERSION" && chatInput != "/QUIT" && chatInput != "/quit" && chatInput != "/love" && chatInput != "/LOVE" && chatInput != "/devs" && chatInput != "/DEVS" && chatInput != "/lobby" && chatInput != "/LOBBY")
    359. {
    360. AddMessage(plist,"[ PlayerList ]");
    361. chatInput = "";
    362. }
    363.  
    364. if ((chatInput == "/v" || chatInput == "/V" || chatInput == "/version" || chatInput == "/VERSION") && chatInput != "/QUIT" && chatInput != "/quit" && chatInput != "/love" && chatInput != "/LOVE" && chatInput != "/devs" && chatInput != "/DEVS" && chatInput != "/lobby" && chatInput != "/LOBBY") {
    365. AddMessage(gameversion,"[ Server ]");
    366. chatInput = "";
    367. }
    368.  
    369. if ((chatInput == "/QUIT" || chatInput == "/quit") && chatInput != "/love" && chatInput != "/LOVE" && chatInput != "/devs" && chatInput != "/DEVS" && chatInput != "/lobby" && chatInput != "/LOBBY") {
    370. AddMessage("Bye bye!","[ Server ]");
    371. chatInput = "";
    372. Application.Quit();
    373.  
    374. }
    375.  
    376. if ((chatInput == "/love" || chatInput == "/LOVE") && chatInput != "/devs" && chatInput != "/DEVS" && chatInput != "/lobby" && chatInput != "/LOBBY") {
    377. AddMessage("We love you too <3","[ Server ]");
    378. chatInput = "";
    379. }
    380.  
    381. if ((chatInput == "/devs" || chatInput == "/DEVS") && chatInput != "/lobby" && chatInput != "/LOBBY") {
    382. AddMessage("Creator: Exbow","[ Server ]");
    383. chatInput = "";
    384. }
    385.  
    386. if (chatInput == "/lobby" | chatInput == "/LOBBY") {
    387. AddMessage("Connecting back to lobby...","[ Server ]");
    388. chatInput = "";
    389. Application.Quit ();
    390. }
    391.  
    392. }
    393.  
    394.  
    395.  
    396.  
    397. voidSendChatServer ( RPCModetarget)
    398. {
    399. if (chatInput != "")
    400. {
    401.  
    402. foreach (stringfilterinthis.blacklist)
    403. {
    404. if (chatInput.ToLower().IndexOf(filter) != -1)
    405. {
    406. chatInput = chatInput.ToLower().Replace(filter, "****");
    407. }
    408. }
    409. User = "[ Server ]";
    410. GetComponent<NetworkView>().RPC("SendChatMessage", target, chatInput,User);
    411. chatInput = "";
    412. User = UserBackUp;
    413. }
    414. }
    415.  
    416. }
    417.  
     
  7. Mabenan

    Mabenan

    Joined:
    Feb 7, 2014
    Posts:
    132
  8. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
  9. Mabenan

    Mabenan

    Joined:
    Feb 7, 2014
    Posts:
    132
    yes change the parameter alignment
     
  10. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
    Yikes... would i have to Photoshop it to change the GUISkin, or can I open it in Unity? Sorry I'm really nooby at this stuff...
     
  11. Mabenan

    Mabenan

    Joined:
    Feb 7, 2014
    Posts:
    132
    In the manual everything is described. You have to add a variable from type GUIStyle to your script wich will be your Style for the Label element. Then you can edit your Instance of GUIStyle in the editor.

    Code (CSharp):
    1. public ShowLabelScript : MonoBehaviour
    2. {
    3.     public GUIStyle myGUIStyle;
    4.  
    5.     void OnGUI(){
    6.        GUILayout.Label("TEST", myGUIStyle);
    7.      }
    8. }