Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This is the function using the split and sending it to the chat
- public void AddLine( string sender, string text, Color color )
- {
- if( logs.Count > chatLength )
- logs.RemoveRange(0, chatLength/2 );
- List<string> split = SplitLine( text );
- if( split.Count > 0 )
- foreach( string s in split )
- logs.Add( new ChatEntry( s, style, color ) );
- }
- // Split the line into a list, depending on the chat width
- private List<string> SplitLine( string inStr )
- {
- lineHeight = Mathf.FloorToInt( chatRect.height /
- GUIManager.instance.skin.label.CalcHeight( new GUIContent("Cornichon"), 100) );
- lineWidth = (int)chatRect.width-40;
- List<string> list = new List<string>();
- while( inStr.Length > 0 )
- {
- // La ligne rentre dans le chat, on sort de suite
- if( style.CalcSize(new GUIContent(inStr)).x < lineWidth ){
- list.Add( inStr );
- break;
- }
- int i = 0;
- string result = GetSubStringOfSize( inStr, lineWidth, style, 10, 0, ref i );
- // Recherche du dernier espace pour ne pas couper un mot au milieux.
- int LastIndexOf = result.LastIndexOf(' ');
- if( LastIndexOf > 0 && LastIndexOf < result.Length ){
- list.Add( result.Substring( 0, LastIndexOf ) );
- inStr = inStr.Substring( LastIndexOf, inStr.Length-LastIndexOf );
- }
- else{
- list.Add(result);
- inStr = inStr.Substring( i, inStr.Length-i );
- }
- }
- return list;
- }
- // Recursive function to get a string of given size for a given style. If in the middle of word, return the end of the last.
- private string GetSubStringOfSize( string inStr, float size, GUIStyle style, int kernel, int start, ref int index )
- {
- string subStr = "";
- index = start;
- float length = 0f;
- do
- {
- index += kernel;
- index = index > inStr.Length ? inStr.Length : index;
- subStr = inStr.Substring( 0, index );
- length = style.CalcSize(new GUIContent(subStr)).x;
- if( index == inStr.Length || length >= size )
- {
- if( kernel == 1 )
- return subStr;
- /*return subStr = GetSubStringOfSize( subStr, size,
- style, kernel / 2, index-kernel, ref index );*/
- int space = index-kernel;
- string checkForSpaces = subStr.Substring( space, index-space );
- int i = checkForSpaces.IndexOf(' ');
- bool atLeastTwoSpaces = i > 0 ? checkForSpaces.IndexOf(' ', i) > 0 : false;
- if( atLeastTwoSpaces )
- return subStr = GetSubStringOfSize( subStr, size,
- style, kernel / 2, index-kernel, ref index );
- else{
- index = index-kernel;
- return subStr;
- }
- }
- }
- while( length < size );
- return subStr;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement