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. Dismiss Notice

Resolved PubNub chat text font size issue

Discussion in 'UGUI & TextMesh Pro' started by unity_fqlyjbgDafPWCw, Apr 29, 2021.

  1. unity_fqlyjbgDafPWCw

    unity_fqlyjbgDafPWCw

    Joined:
    Feb 5, 2021
    Posts:
    9
    I've been working on a simple multiplayer chat using PubNub since I can't use localhost as a server. I keep running into this issue where at the start of the game, old chat messages are perfectly sized and placed, but new ones are twice as big and offset far to the left.

    I'm using the script that comes with PubNub's demo project, plus a few edits of my own. Aside from adding the variable "scrollContent" -- a scrollview with a vertical layout group component -- this is the only function I've needed to change:
    Code (CSharp):
    1.  
    2. void CreateChat(JSONInformation payLoad){
    3.  
    4.         // Create a string with the username and text
    5.         string currentObject = string.Concat(payLoad.username, payLoad.text);
    6.        
    7.  
    8.         // Create a new gameobject that will display text of the data sent via PubNub
    9.         GameObject chatMessage = new GameObject(currentObject);
    10.         chatMessage.transform.SetParent(scrollContent.transform);
    11.         chatMessage.transform.localPosition = Vector3.zero;
    12.         //chatMessage.transform.position = new Vector3(canvasObject.transform.GetChild(0).GetChild(0).GetChild(0).GetChild(0).position.x - paddingX, canvasObject.transform.GetChild(0).GetChild(0).GetChild(0).GetChild(0).position.y - paddingY + padding - (indexcounter * height), 1F);      
    13.         chatMessage.AddComponent<Text>().text = currentObject;
    14.  
    15.         // Assign text to the gameobject. Add visual properties to text
    16.         var chatText = chatMessage.GetComponent<Text>();
    17.         chatText.font = customFont;
    18.         chatText.color = UnityEngine.Color.blue;
    19.         chatText.fontSize = 25;
    20.        
    21.         // Assign a RectTransform to gameobject to maniuplate positioning of chat.
    22.         RectTransform rectTransform;
    23.         rectTransform = chatText.GetComponent<RectTransform>();
    24.         rectTransform.sizeDelta = new Vector2(435, height);
    25.        
    26.         // Assign the gameobject to the queue of chatmessages
    27.         chatMessageQueue.Enqueue(chatMessage);
    28.                        
    29.         // Keep track of how many objects we have displayed on the screen
    30.         indexcounter++;
    31.     }
    32.     }

    From what I can tell, the formatting for all of the messages is handled by this same function, (editing things like font size here will change the font size for both new and old messages, though they will still both be different sizes and posiitons) but here's the github repository for the sample project I'm basing this off of: https://github.com/pubnub/Unity-Realtime-Chat-Room-Tutorial

    I should note that I'm not using their original background, and my current heirarchy for the chat objects is much different than theirs, but even when I run their sample scene with their original, unedited script, The font size is too large (although it is the same size at least, and I can't figure out why.)

    help is much appreciated.
     
  2. unity_fqlyjbgDafPWCw

    unity_fqlyjbgDafPWCw

    Joined:
    Feb 5, 2021
    Posts:
    9
    Solved: It was an issue with the scale being set according to the world, and THEN being set to the parent object.

    Previous code:
    Code (CSharp):
    1. GameObject chatMessage = new GameObject(currentObject);
    2.         chatMessage.transform.SetParent(scrollContent.transform);
    3.         chatMessage.transform.localPosition = Vector3.zero;
    4.         //chatMessage.transform.position = new Vector3(canvasObject.transform.GetChild(0).GetChild(0).GetChild(0).GetChild(0).position.x - paddingX, canvasObject.transform.GetChild(0).GetChild(0).GetChild(0).GetChild(0).position.y - paddingY + padding - (indexcounter * height), 1F);    
    5.         chatMessage.AddComponent<Text>().text = currentObject;

    By instantiating the text object as children, rather than creating the objects then setting their parent, the scale is automatically set to 1.

    Fixed code:
    Code (CSharp):
    1. GameObject chatMessage = Instantiate(new GameObject(currentObject), scrollContent.transform);
    2.         chatMessage.transform.localPosition = Vector3.zero;
    3.         chatMessage.AddComponent<Text>().text = currentObject;