Page 1 of 1

Unity chat question (text size and etc).

PostPosted: Sat Dec 24, 2016 2:26 pm
by OwnerOfSuccuby
Hello - i can not find the answer but may be some body knows.

I want to remake chat from flash to unity. What i do wrong ?

I make unit chat bubble - that have button in it -> button have text (UniteEngine.UI.Text component).

I want to write text in this text component. For better chat looking i want to resize chat bubble as text size - but ... how to make it better ?

Just for example now i do it like this :

Code: Select All Code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class ChatScript : MonoBehaviour {
   
   //public  chatBubble;
   //public Transform ChatBubble;
   //public Transform ChatInZone;

   public GameObject ChatBubble;
   public GameObject ChatInZone;


   int BubblesCount=0;
   public static int MaxLength=25;

   int BubblesToShow=0;

   ChatInBubbleClass[] InBubblesArray = new ChatInBubbleClass[MaxLength*2];
   ChatInBubbleClass[] TempInBubblesArray = new ChatInBubbleClass[MaxLength*2];

   int BP=0;// Переменная для отображения пузырей чата

   void newInBubble(string CTxt, string CT, string FPM, string TPM){
      ChatInBubbleClass temp = new ChatInBubbleClass();
      temp.WriteToInBubble (CTxt, CT, FPM, TPM);
      InBubblesArray[BubblesCount] = temp;
      BubblesCount = BubblesCount + 1;
      if (BubblesCount >= 2 * MaxLength) {
         chatToSmallerRebuild ();
      }
   }

   void chatToSmallerRebuild(){
      /*
      for (int i = MaxLength; i <= 2 * MaxLength; i++){
         TempInBubblesArray [(i - MaxLength)] = InBubblesArray [i];
         InBubblesArray = TempInBubblesArray;
         TempInBubblesArray = null;
      }
      BubblesCount = MaxLength;
      */
   }

   void PrintBubble(ChatInBubbleClass CBTP){

      var ChatBubbleTransform = Instantiate(ChatBubble) as GameObject;

      ChatBubbleTransform.transform.SetParent(ChatInZone.transform);


      ChatBubbleTransform.GetComponentInChildren<UnityEngine.UI.Text>().text=CBTP.ChatText;

      /*-----------------------><-------------------------------------*/


      ChatBubbleTransform.GetComponentInChildren<UnityEngine.UI.Text> ().color = new Color(0,0,0,1);

      ChatBubbleTransform.transform.localPosition = new Vector3(0, (-BP*62f/(BubblesToShow)), 0);

      BP = BP + 1;
   }

   public void TEST(){
      Debug.Log (BP);
      EventManager.AllBubblesDestroy ();
      newInBubble ("InBubbleInBubble", "ALL", "test", "test");
      PutBubblesToChat ("ALL");
      //BP = BP + 1;
   }

   void PutBubblesToChat(string ChatType){
      BP = 0;
      BubblesToShow = 0;

      for (int i = 0; i < BubblesCount; i++) {
         if (ChatType == "ALL") {
            BubblesToShow = BubblesToShow + 1;
         }
         else if (ChatType == "CLAN") {
            if ((InBubblesArray [i].ChatType == "CLAN")||(InBubblesArray [i].ChatType == "PM")) {
               BubblesToShow = BubblesToShow + 1;            
            }
         }
         else if (ChatType == "GROUP") {
            if ((InBubblesArray [i].ChatType == "GROUP")||(InBubblesArray [i].ChatType == "PM")) {
               BubblesToShow = BubblesToShow + 1;            
            }
         }
         if (ChatType == "PM") {
            if (InBubblesArray [i].ChatType == "PM") {
               BubblesToShow = BubblesToShow + 1;            
            }
         }
      }

      if (BubblesToShow > 0) {
         ChatInZone.transform.localScale = new Vector3 (1, BubblesToShow, 1);
      }


      if (BubblesToShow > 0) {
         if (GameObject.Find ("ChatScrollVert")!=null){
            Debug.Log (" GameObject.Find (\"ChatScrollVert\").GetComponent<UnityEngine.UI.Scrollbar> ().value = " + GameObject.Find ("ChatScrollVert").GetComponent<UnityEngine.UI.Scrollbar> ().value);
            Debug.Log (2f/BubblesToShow);
            if (GameObject.Find ("ChatScrollVert").GetComponent<UnityEngine.UI.Scrollbar> ().value <= (5f/BubblesToShow)) {
               GameObject.Find ("ChatScrollVert").GetComponent<UnityEngine.UI.Scrollbar> ().value = 1f/BubblesToShow;
            }
         }

      }

      //EventManager.AllBubblesDestroy ();
      for (int i = 0; i < BubblesCount; i++) {
         if (ChatType == "ALL") {
            PrintBubble (InBubblesArray [i]);
         }
         else if (ChatType == "CLAN") {
            if ((InBubblesArray [i].ChatType == "CLAN")||(InBubblesArray [i].ChatType == "PM")) {
               PrintBubble (InBubblesArray [i]);
            }
         }
         else if (ChatType == "GROUP") {
            if ((InBubblesArray [i].ChatType == "GROUP")||(InBubblesArray [i].ChatType == "PM")) {
               PrintBubble (InBubblesArray [i]);
            }
         }
         if (ChatType == "PM") {
            if (InBubblesArray [i].ChatType == "PM") {
               PrintBubble (InBubblesArray [i]);
            }
         }
      }

      if (BubblesToShow > 0) {
         if (GameObject.Find ("ChatScrollVert")!=null){
            Debug.Log (" GameObject.Find (\"ChatScrollVert\").GetComponent<UnityEngine.UI.Scrollbar> ().value = " + GameObject.Find ("ChatScrollVert").GetComponent<UnityEngine.UI.Scrollbar> ().value);
            Debug.Log (2f/BubblesToShow);
            if (GameObject.Find ("ChatScrollVert").GetComponent<UnityEngine.UI.Scrollbar> ().value <= (5f/BubblesToShow)) {
               GameObject.Find ("ChatScrollVert").GetComponent<UnityEngine.UI.Scrollbar> ().value = 0;
            }
         }

      }
   }

   // Use this for initialization
   void Start () {
      
   }
   
   // Update is called once per frame
   void Update () {
      
   }
}


Unfortunatly i do not understand for now how to remake size of component in Unity so i just ChatInZone.transform.localScale = new Vector3 (1, BubblesToShow, 1); - scale it to the value i need i do not like this solution but can not find how to make it better on unity.

The next problem i need to know what size will text contain if for example i know width - i need height - than transform all other components - how to make t on unity ?

For now i can try to make my own or find some libruary for c# and use it - but i do not like this solution and do not want to create new bassicles :mrgreen: - so may be there is some simple way ? :D ;)

Re: Unity chat question (text size and etc).

PostPosted: Sat Dec 24, 2016 5:00 pm
by OwnerOfSuccuby
Find some node code solution :

https://unity3d.com/ru/learn/tutorials/ ... list=17111

Do not know about layOut elements and groups - i will test it soon :mrgreen:

Re: Unity chat question (text size and etc).

PostPosted: Sun Feb 12, 2017 1:57 am
by BlueLight
I know i'm a few months late but i'm not sure exactly what the problem is.
By the sounds, you want to increases size of the panel and buttons as the text increases?
In that case you will need to use layout manager.