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 - so may be there is some simple way ?