Created
June 18, 2014 12:05
-
-
Save keiranlovett/8832726deea9bb7f574f to your computer and use it in GitHub Desktop.
Scrolling GUI Status (Who Killed Who)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import System.Collections.Generic; | |
import System.Linq; | |
import System; | |
class Message | |
{ | |
//Holds the messages being displayed | |
static var messages : List.<Message> = new List.<Message>(); | |
//Holds the messages waiting for display | |
static var queue : List.<Message> = new List.<Message>(); | |
//Internally used to space the messages in time | |
private static var lastTime : float = 0; | |
//The screen Y coordinate that causes a fade | |
static var fadeY : float = 300; | |
//The amount of time before fading | |
static var fadeTime : float = 10; | |
//The position that the bottom message starts at | |
static var messageStartPosition : Vector2 = Vector2(10,400); | |
//The amount of pixels the message scrolls per second | |
static var movePerSecond : float = 40; | |
//The minimum amount of time between messages | |
static var spacingTime : float = 1; | |
//The vertical spacing of messages | |
static var verticalSpacing : float = 25; | |
//Static constructor, fixes the start Y position | |
private static function Message() { | |
messageStartPosition.y -= messageStartPosition.y % verticalSpacing; | |
} | |
//Creates a new message | |
private function Message(newMessage : String) { | |
message = newMessage; | |
position = Vector2(messageStartPosition.x, messageStartPosition.y - (messageStartPosition.y % verticalSpacing)); | |
} | |
//Call this to display a message | |
static function QueueMessage(message : String) { | |
var m : Message = new Message(message); | |
queue.Add(m); | |
} | |
//Draws the current messages | |
static function Display() { | |
for(var m : Message in messages.ToList()) { | |
m.Draw(); | |
//If the message has become invisible | |
//delete it - note the .ToList() above means | |
//that this for loop won't fail | |
if(!m.visible) | |
messages.Remove(m); | |
} | |
//Check if we have a new message and the requisite amount of time | |
//has passed | |
if(queue.Count > 0 && Time.time - lastTime > spacingTime) { | |
//See if we are displaying messages | |
if(messages.Count > 0) { | |
//Is there a message in the bottom slot? | |
if( Mathf.Floor((messages[0].position.y + verticalSpacing-1) / verticalSpacing) | |
== Mathf.Floor(messageStartPosition.y / verticalSpacing)) { | |
//Move the bottom message | |
messages[0].position.y -= Time.deltaTime * movePerSecond; | |
//Reposition the other messages | |
for(var i = 1; i < messages.Count; i++) { | |
messages[i].position.y = messages[i-1].position.y - verticalSpacing; | |
} | |
//Return to stop a new message being added | |
return; | |
} | |
} | |
//get the message at the front of the queue | |
var n = queue[0]; | |
//update the queue | |
queue.RemoveAt(0); | |
//add the new message to the start of the running ones | |
messages.Insert(0,n); | |
//update the times | |
n.startTime = Time.time; | |
lastTime = Time.time; | |
} | |
} | |
//Current position of this message | |
var position : Vector2; | |
//This message text | |
var message: String; | |
//Current alpha value | |
var alpha : float = 0; | |
//Is the message visible? | |
var visible = true; | |
//The time the message was displayed | |
var startTime : float; | |
//Draw this single message | |
private function Draw() { | |
//Store the current GUI color | |
var color = GUI.color; | |
//adjust for alpha fading | |
GUI.color = Color(GUI.color.r, GUI.color.g, GUI.color.b, alpha * color.a); | |
//Draw the actual message | |
DrawGuiString(message, position); | |
//Reset the color | |
GUI.color = color; | |
//Is it time for a fade? | |
if(position.y < fadeY || (Time.time - startTime > fadeTime) ){ | |
//Fade out | |
alpha = Mathf.Clamp01(alpha - Time.deltaTime * 0.25); | |
if(alpha == 0) | |
visible = false; | |
} | |
else { | |
//Fade in | |
alpha = Mathf.Clamp01(alpha + Time.deltaTime * 5); | |
} | |
} | |
//A section of coloured string! | |
class ColorPair | |
{ | |
public var text : String; | |
public var color : Color; | |
} | |
//Call this to draw a coloured string somewhere from an OnGUI | |
static function DrawGuiString( guiString : String, position: Vector2) { | |
//Do we have anything fancy? | |
if(guiString.IndexOf(":")==-1) { | |
//No? Then draw a standard label | |
GUI.Label( Rect(position.x, position.y,1000,1000), guiString); | |
return; | |
} | |
//Save the colour | |
var color = GUI.color; | |
//Create an array of string parts - first split on | | |
var labels = guiString.Split(["|"], System.StringSplitOptions.None).Select(function(s) { | |
//Now split the colour from the text on the : | |
var parts = s.Split([":"], System.StringSplitOptions.None); | |
//Extract the color elements, by splitting on "," and make an array of floats | |
var colorParts = parts[0].Split([","], System.StringSplitOptions.None).Select(function(c) { | |
//Convert it to a float | |
return Convert.ToSingle(c); | |
}).ToArray(); | |
var np = new ColorPair(); | |
np.color = Color(colorParts[0], colorParts[1], colorParts[2], color.a); | |
np.text = parts[1]; | |
return np; | |
}).ToArray(); | |
//Draw the sections | |
for(var label : ColorPair in labels) { | |
//Current color (already adjusted for alpha) | |
GUI.color = label.color; | |
//Draw the string section | |
GUI.Label(Rect(position.x, position.y, Screen.width,100), label.text); | |
//Move the X by the width of what we just drew | |
position.x += GUI.skin.GetStyle("Label").CalcSize(GUIContent(label.text)).x + 2; | |
} | |
//Put the colour back | |
GUI.color = color; | |
} | |
} | |
function Start() { | |
Message.QueueMessage("Hello"); | |
Message.QueueMessage("World"); | |
Message.QueueMessage("From"); | |
Message.QueueMessage("1,0.4,0.5:WhyDoIDoIt|0.3,0.4,1: Can you tell me?|1,1,1: Thought not..."); | |
} | |
function OnGUI() { | |
//Choose your skin of styles for Label here | |
//Only update when we are painting | |
if(Event.current.type == EventType.repaint) | |
Message.Display(); | |
//Dummy button for demo | |
if(GUI.Button(Rect(40,40,200,20), "Push Me")) | |
Message.QueueMessage("Another message"); | |
} |
var Punches : int;
function Start() {
Find.GameObject(themars2011).Animation.Play("PunchInThaFace");
}
function Punch () {
audio.PlayOneShot("Punch");
Get.Component("themars2011Face").eneabled = false;
RayPunch();
}
function RayPunch () {
var : Ray;
DrawRay();
if(Ray.hit = themars2011){
themars2011 = "Troll Face :D"
Punches++;
}
}
function DrawRay () {
Debug.Log("Punches" +Punches);
Hit.themars2011.Face;
Debug.Log("ROFL !");
}
// Here ya Go :D
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
by Mike Talbot 2012
www.whydoidoit.com