Hello there, I’m kinda new to game development and came up with some problems.
Here I am making a Custom Editor script that allows me to bring up some textAreas where I can write a message (split into several parts) that will later appear in-game when interacting with a sign.
First problem: In line 41, by using calcHeight, I’m trying to limit the max characters I can put into a message(textArea) so it doesn’t get out of the box inside which they will be shown in-game, but it doesn’t seem to be working as I expected (cutting by height) since it is just cutting the last line in some “random” width.
As shown in this image.

Second problem: When I reach the max characters it calculates will fit in the box, maxChars() will return false and it won’t let me write anymore (through numChars[ ]). BUT, if I go backwards in the message I’m writing and write in the middle of the string, I will be able to go on writing until the pointer reaches the limit.
I don’t know if this is a problem of my implementation or of the textArea’s managing of maxLenght.
Sorry if you find some noobie-ness and thanks in advance.
using UnityEditor;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class CartelScriptEditor : MonoBehaviour {
[CustomEditor(typeof(Cartel))]
public class TextAreaEditor : Editor {
public SerializedProperty messageProp;
public SerializedProperty fontSizeProp;
public SerializedProperty textsProp;
List<int> numChars = new List<int>();
void OnEnable() {
messageProp = serializedObject.FindProperty("message");
fontSizeProp = serializedObject.FindProperty("fontSize");
textsProp = serializedObject.FindProperty("texts");
}
bool maxChars(string text) {
const float textBoxHeight = 240f;
const float textBoxWidth = 240f;
GUIStyle usedStyle;
usedStyle = new GUIStyle();
usedStyle.font = (Font)Resources.Load("Interface/Dialogues/FreeMono");
usedStyle.normal.textColor = Color.black;
usedStyle.fontSize = fontSizeProp.intValue;
usedStyle.fontStyle = FontStyle.Bold;
usedStyle.wordWrap = true;
string message = text;
GUIContent messageContent = new GUIContent(text);
char[] textArray = message.ToCharArray();
//ENGLISH: We calculate how many chars fit in the box with the current font and style
//Calculamos los caracteres que caben en el dialogImage con la fuente actual
for (int i = 0; (((usedStyle.CalcHeight(messageContent, textBoxWidth)) <= textBoxHeight) && (i < (textArray.Length - 1))); i++) {
messageContent.text += textArray[i];
}
//ENGLISH: Return true if the chars fit, false if they don't
//Si los caracteres caben en el bocadillo devolvemos true, si no, false
if (usedStyle.CalcHeight(messageContent, textBoxWidth) <= textBoxHeight) {
return true;
}
else {
return false;
}
}
public void checkText() {
for (int i = 0; i < textsProp.arraySize; i++) {
try {
//ENGLISH: If true we add 1 to the textArea character limit
//Si true añadimos 1 al numChars de textArea
if (maxChars(textsProp.GetArrayElementAtIndex(i).stringValue)) {
numChars[i] = textsProp.GetArrayElementAtIndex(i).stringValue.Length + 1;
textsProp.GetArrayElementAtIndex(i).stringValue = GUILayout.TextArea(textsProp.GetArrayElementAtIndex(i).stringValue, numChars[i], GUILayout.MaxHeight(70));
}
else {
//ENGLISH: If false user won't be able to write any more chars
//Si false no dejamos que escriba mas
numChars[i] = textsProp.GetArrayElementAtIndex(i).stringValue.Length;
textsProp.GetArrayElementAtIndex(i).stringValue = GUILayout.TextArea(textsProp.GetArrayElementAtIndex(i).stringValue, numChars[i], GUILayout.MaxHeight(70));
}
} catch (System.ArgumentOutOfRangeException) {
//ENGLISH: It allows us to raise the number of textAreas
//Nos permite aumentar el numero de textAreas
numChars.Add(0);
}
}
}
public override void OnInspectorGUI() {
serializedObject.Update();
fontSizeProp.intValue = (int)EditorGUILayout.Slider("Font Size", fontSizeProp.intValue, 19, 25);
EditorList.Show(textsProp, EditorList.EditorListOption.ListSize);
checkText();
serializedObject.ApplyModifiedProperties();
}
}
}