using UnityEngine ;

using System.Collections.Generic ;

[ RequireComponent ( typeof ( GUIText ) ) ] RequireComponentGUIText

public class ScreenPrinter : MonoBehaviour {

public TextAnchor anchorAt = TextAnchor . LowerLeft ;

public int numberOfLines = 5 ;

public int pixelOffset = 5 ;

static ScreenPrinter defaultPrinter = null ;

static bool quitting = false ;

< string > newMessages = < string > ( ) ; ListnewMessages new List

TextAnchor _anchorAt ;

float _pixelOffset ;

< string > messageHistory = < string > ( ) ; ListmessageHistory new List

// static Print method: finds a ScreenPrinter in the project,

// or creates one if necessary, and prints to that.

public static void Print ( object message ) {

if ( quitting ) return ; // don't try to print while quitting

if ( ! defaultPrinter ) {

GameObject gob = GameObject . Find ( "Screen Printer" ) ;

if ( ! gob ) gob = ( "Screen Printer" ) ; gobgob new GameObject

defaultPrinter = gob . GetComponent < ScreenPrinter > ( ) ;

if ( ! defaultPrinter ) defaultPrinter = gob . AddComponent < ScreenPrinter > ( ) ;

}

defaultPrinter . LocalPrint ( message ) ;

}

// member LocalPrint method: prints to this particular screen printer.

// Called LocalPrint because C# won't let us use the same name for both

// static and instance method. Grr. Argh. >:(

public void LocalPrint ( object message ) {

if ( quitting ) return ; // don't try to print while quiting

newMessages . Add ( message . ToString ( ) ) ;

}

void Awake ( ) {

if ( ! guiText ) {

gameObject . AddComponent ( "GUIText" ) ;

transform . position = Vector3 . zero ;

. localScale = ( 0 , 0 , 1 ) ; transform new Vector3

}

_anchorAt = anchorAt ;

UpdatePosition ( ) ;

}

void OnApplicationQuitting ( ) {

quitting = true ;

}

void Update ( ) {

// if anchorAt or pixelOffset has changed while running, update the text position

if ( _anchorAt != anchorAt || _pixelOffset != pixelOffset ) {

_anchorAt = anchorAt ;

_pixelOffset = pixelOffset ;

UpdatePosition ( ) ;

}

// if the message has changed, update the display

if ( newMessages . Count > 0 ) {

for ( int messageIndex = 0 ; messageIndex < newMessages . Count ; messageIndex ++ ) {

messageHistory . Add ( newMessages [ messageIndex ] ) ;

}

if ( messageHistory . Count > numberOfLines ) {

messageHistory . RemoveRange ( 0 , messageHistory . Count - numberOfLines ) ;

}

// create the multi-line text to display

guiText . text = string . Join ( "

" , messageHistory . ToArray ( ) ) ;

newMessages . Clear ( ) ;

}

}

void UpdatePosition ( ) {

switch ( anchorAt ) {

case TextAnchor . UpperLeft :

. position = ( 0 . 0f, 1 . 0f, 0 . 0f ) ; transform new Vector30f, 10f, 00f

guiText . anchor = anchorAt ;

guiText . alignment = TextAlignment . Left ;

. pixelOffset = ( pixelOffset, - pixelOffset ) ; guiText new Vector2pixelOffset,pixelOffset

break ;

case TextAnchor . UpperCenter :

. position = ( 0 . 5f, 1 . 0f, 0 . 0f ) ; transform new Vector35f, 10f, 00f

guiText . anchor = anchorAt ;

guiText . alignment = TextAlignment . Center ;

. pixelOffset = ( 0 , - pixelOffset ) ; guiText new Vector2pixelOffset

break ;

case TextAnchor . UpperRight :

. position = ( 1 . 0f, 1 . 0f, 0 . 0f ) ; transform new Vector30f, 10f, 00f

guiText . anchor = anchorAt ;

guiText . alignment = TextAlignment . Right ;

. pixelOffset = ( - pixelOffset, - pixelOffset ) ; guiText new Vector2pixelOffset,pixelOffset

break ;

case TextAnchor . MiddleLeft :

. position = ( 0 . 0f, 0 . 5f, 0 . 0f ) ; transform new Vector30f, 05f, 00f

guiText . anchor = anchorAt ;

guiText . alignment = TextAlignment . Left ;

. pixelOffset = ( pixelOffset, 0 . 0f ) ; guiText new Vector2pixelOffset, 00f

break ;

case TextAnchor . MiddleCenter :

. position = ( 0 . 5f, 0 . 5f, 0 . 0f ) ; transform new Vector35f, 05f, 00f

guiText . anchor = anchorAt ;

guiText . alignment = TextAlignment . Center ;

. pixelOffset = ( 0 , 0 ) ; guiText new Vector2

break ;

case TextAnchor . MiddleRight :

. position = ( 1 . 0f, 0 . 5f, 0 . 0f ) ; transform new Vector30f, 05f, 00f

guiText . anchor = anchorAt ;

guiText . alignment = TextAlignment . Right ;

. pixelOffset = ( - pixelOffset, 0 . 0f ) ; guiText new Vector2pixelOffset, 00f

break ;

case TextAnchor . LowerLeft :

. position = ( 0 . 0f, 0 . 0f, 0 . 0f ) ; transform new Vector30f, 00f, 00f

guiText . anchor = anchorAt ;

guiText . alignment = TextAlignment . Left ;

. pixelOffset = ( pixelOffset, pixelOffset ) ; guiText new Vector2pixelOffset, pixelOffset

break ;

case TextAnchor . LowerCenter :

. position = ( 0 . 5f, 0 . 0f, 0 . 0f ) ; transform new Vector35f, 00f, 00f

guiText . anchor = anchorAt ;

guiText . alignment = TextAlignment . Center ;

. pixelOffset = ( 0 , pixelOffset ) ; guiText new Vector2, pixelOffset

break ;

case TextAnchor . LowerRight :

. position = ( 1 . 0f, 0 . 0f, 0 . 0f ) ; transform new Vector30f, 00f, 00f

guiText . anchor = anchorAt ;

guiText . alignment = TextAlignment . Right ;

. pixelOffset = ( - pixelOffset, pixelOffset ) ; guiText new Vector2pixelOffset, pixelOffset

break ;

}

}