Search Unity

[Script] Console Rich Text Messages.

Discussion in 'Immediate Mode GUI (IMGUI)' started by IsGreen, Jun 25, 2014.

  1. IsGreen

    IsGreen

    Joined:
    Jan 17, 2014
    Posts:
    206



    Send lines of text in Rich Text format.

    The lines are shown from a GUI.BeginScrollView element that allows us to move through the list.

    This would be the console code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3.  
    4. public class Console : MonoBehaviour {
    5.  
    6.     public Font font;
    7.     public int sizeFont = 15;
    8.     public FontStyle styleFont = FontStyle.Normal;
    9.     public Color colorFont = Color.grey;
    10.     public TextAnchor aligment = TextAnchor.UpperLeft;
    11.     public float heightLine = 20f;
    12.     public Rect rectConsole = new Rect(10f,10f,400f,100f);
    13.  
    14.     List<string> linesConsole = new List<string>();
    15.     int numLines;
    16.  
    17.     GUIStyle style = new GUIStyle();
    18.     Rect rectLine, viewScroll;
    19.     Vector2 positionScroll = Vector2.zero;
    20.  
    21.  
    22.     void Start(){
    23.  
    24.         this.style.font = this.font;
    25.         this.style.fontSize = this.sizeFont;
    26.         this.style.fontStyle = this.styleFont;
    27.         this.style.normal.textColor = this.colorFont;
    28.         this.style.alignment = this.aligment;
    29.         this.style.richText = true;
    30.         this.numLines = this.linesConsole.Count;
    31.         this.rectLine = new Rect(this.rectConsole.x+5f,0f,this.rectConsole.width-10f,20f);
    32.         this.viewScroll = new Rect(0,0,this.rectConsole.width-25f,this.rectConsole.height);
    33.     }
    34.  
    35.     void Update(){
    36.  
    37.         if(this.numLines!=this.linesConsole.Count){
    38.  
    39.             this.numLines = this.linesConsole.Count;
    40.             this.viewScroll.height = Mathf.Max(this.rectConsole.height,(this.numLines+1)*this.heightLine);
    41.             this.viewScroll.y = this.rectConsole.height-this.viewScroll.height;
    42.             this.positionScroll.y = this.viewScroll.height-this.rectConsole.height;
    43.  
    44.         }
    45.  
    46.     }
    47.  
    48.     void OnGUI(){
    49.  
    50.         GUI.Box(this.rectConsole,"");
    51.  
    52.         this.positionScroll = GUI.BeginScrollView(this.rectConsole,this.positionScroll,this.viewScroll);
    53.  
    54.         float LowMargin = this.rectConsole.y + this.rectConsole.height - this.heightLine;
    55.         float UpLimit = this.positionScroll.y + this.viewScroll.y - this.heightLine;
    56.         float LowLimit = UpLimit + this.rectConsole.height;
    57.  
    58.         for(int i=1;i<=this.linesConsole.Count;i++){
    59.  
    60.             this.rectLine.y = LowMargin-i*this.heightLine;
    61.             if(this.rectLine.y<UpLimit) break;
    62.             if(this.rectLine.y>LowLimit) continue;
    63.             GUI.Label(this.rectLine,this.linesConsole[this.linesConsole.Count-i],this.style);
    64.  
    65.         }
    66.  
    67.         GUI.EndScrollView();
    68.  
    69.     }
    70.  
    71.     public void AddLine(string linea){
    72.  
    73.         this.linesConsole.Add(linea);
    74.  
    75.     }
    76.  
    77. }
    78.  

    After, from other scripts you can add lines to the console by AddLine (string line) function, for example:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class controlConsole : MonoBehaviour {
    5.  
    6.     public Console console;
    7.     public Color colorPlayer = Color.red;
    8.     public string namePlayer = "Player";
    9.     public Rect rectTextField = new Rect(100f,500f,500f,30f);
    10.     public int maxCharactersTextField = 50;
    11.  
    12.     string line="";
    13.  
    14.     void OnGUI(){
    15.  
    16.         string str = GUI.TextField(this.rectTextField,this.line,this.maxCharactersTextField,GUI.skin.textField);
    17.  
    18.         if(str.Contains("\n")){
    19.  
    20.             if(this.line.Length>0){
    21.  
    22.                 console.AddLine("<size=12><color=#"+RGBToHex(this.colorPlayer)+">["+this.namePlayer+"]</color></size>"+this.line);
    23.                 this.line="";
    24.  
    25.             }
    26.  
    27.         } else this.line = str;
    28.  
    29.     }
    30.  
    31.     static string RGBToHex (Color color) {
    32.  
    33.         string Hex = "0123456789abcdef";
    34.  
    35.         int red   = (int)(color.r * 255f);
    36.         int green = (int)(color.g * 255f);
    37.         int blue  = (int)(color.b * 255f);
    38.         int alfa  = (int)(color.a * 255f);
    39.  
    40.         return (Hex[red/16]+Hex[red%16]+Hex[green/16]+Hex[green%16]+Hex[blue/16]+Hex[blue%16]+Hex[alfa/16]+Hex[alfa%16]).ToString();
    41.  
    42.     }
    43.  
    44. }
    Download link.
     
    Last edited: Jun 25, 2014