Posts Tagged gui

Who Killed Who – Multicolor scrolling Unity Messages


Hey here’s my script for multi-color scrolling Who Killed Who messages.

Installation

Download the package and import it.  You are going to want to modify this file though, it contains examples.

In Use

Note the whole thing is driven off statics so you don’t need references you can just access the features through Messages.Message from your script.

Configuration

  • Configure the screen location you want by changing Messages.Message.messageStartPosition either in the code or at runtime. Note: start positions will be rounded to a multiple of verticalSpacing on start up. If you move it, perhaps to account for screen resolution, make sure that it is a multiple of verticalSpacing or the results will not be pretty.
  • Set the Y coordinate where fades will start irrespective of time. It’s Messages.Message.fadeY
  • Set the spacing of messages. Messages.Message.verticalSpacing
  • Choose your GUI skin/style for Label in the OnGUI call.

Displaying a Message

To display a message:

  • Call Messages.Message.QueueMessage(“Your message”); from any script
  • Messages support multiple color strings by using some custom formatting in the string:
  • Either a plain string or red,green,blue:My string|red,green,blue:Another string

You can have any number of those combinations: an example is “1,0.4,0.5:Hello |1,1,1:World”

  • You can also display a multi colour string from any OnGUI by using

Messages.Message.DrawGuiString(message, position)

Where position is a Vector2

I’ve included some examples to see it working…

, , ,

Leave a comment

Fixing the width of strings displayed in the Unity GUI


Firstly, I know what you are going to say – you should just use two aligned labels, rather than fiddling around with string formatting – but sometimes that just doesn’t work – like when you want the items in a popup to have columns like here:

No chance for using labels in that component.  So there is a way of effectively padding text to a given width.  You basically work out the width of a ” “, a tab and the string, then use that combination to make a new string which is the correct width.

Just using string.PadRight(20) doesn’t work due to the variable width of characters in the font.


public static class TextHelper
{
	public static string FixTo(this string str, float width, string type="label")
	{
	    var widthOfTab = GUI.skin.GetStyle(type).CalcSize(new GUIContent("\t")).x;
		var widthOfSpace = GUI.skin.GetStyle(type).CalcSize(new GUIContent(" ")).x;
		var widthOfString = GUI.skin.GetStyle(type).CalcSize(new GUIContent(str)).x;
	    return str + new String(' ', (int)((width-widthOfTab)/widthOfSpace)+1) + "\t";
	}
}

You basically use myString.FixTo(150) to fix it based on the width of a “label” in the skin or you can override it to set a different font by using myString.FixTo(200, “box”).

Should work fine in javascript so long as you make a .cs file out of this and put it in your plugins folder.

, , , ,

1 Comment