Skip to content

Instantly share code, notes, and snippets.

@markusrt
Created April 25, 2014 23:14
Show Gist options
  • Save markusrt/11306327 to your computer and use it in GitHub Desktop.
Save markusrt/11306327 to your computer and use it in GitHub Desktop.
Unity3D scripts published prior to Ludum Dare 29
using UnityEngine;
using System.Collections;
/// <summary>
/// Published prior to Ludum Dare 29
///
/// Simple Unity3D script to create scrolling credits
///
/// How to use:
///
/// * Create a new scene
/// * Attach this to your camera (or a new empty game object)
/// * Set "CreditText" property accordingly
///
/// Known issues:
///
/// * Does not react to horizontal scaling of your window
/// </summary>
public class Credits : MonoBehaviour
{
public TextAsset CreditsText;
public float Speed = 0.1f;
public int MaxFontSize = 20;
public GUIStyle TextStyle = new GUIStyle();
private GameObject creditHolder;
private GUIText creditText;
private Transform creditHolderTransform;
public void Start()
{
InitializeCreditHolder();
RecalculateFontSize();
PlaceCreditsAtTheScreensBottom();
}
public void Update()
{
RecalculateFontSize();
MoveCreditsTextUntilEndIsReached();
}
private void InitializeCreditHolder()
{
creditHolder = new GameObject ("Credits");
creditText = creditHolder.AddComponent<GUIText> ();
creditText.alignment = TextAlignment.Center;
creditText.anchor = TextAnchor.LowerCenter;
creditText.text = GetCreditsText ();
creditText.fontStyle = TextStyle.fontStyle;
creditHolderTransform = creditHolder.transform;
}
void RecalculateFontSize ()
{
int fontSize = MaxFontSize;
do
{
creditText.fontSize = fontSize;
fontSize--;
} while (creditText.GetScreenRect().width>Screen.width);
}
void PlaceCreditsAtTheScreensBottom ()
{
float screeny=0;
float y=0.0f;
float minScreenY = (-1.0f * creditText.GetScreenRect().height)+Screen.height/2;
do
{
creditHolderTransform.position = new Vector2(0.5f, y);
y -= 0.1f;
screeny = creditText.GetScreenRect().y;
}while(screeny>minScreenY);
}
private string GetCreditsText()
{
if (CreditsText != null)
{
return CreditsText.text;
}
return CreatePlaceHolderText();
}
private string CreatePlaceHolderText ()
{
string placeHolderText = "These credits are only a placeholder\n\n\n";
for (int i = 0; i < 100; i++)
{
placeHolderText+="Please set 'CreditsText' text asset for real credits...\n";
}
placeHolderText += "\n\n\n\n\nThanks for watching placeholder credits this far :)";
return placeHolderText;
}
private void MoveCreditsTextUntilEndIsReached ()
{
if(creditText.GetScreenRect().y > Screen.height*0.35)
{
return;
}
creditHolderTransform.Translate(Vector3.up * Time.deltaTime * Speed);
}
}
using UnityEngine;
using System.Collections;
/// <summary>
/// Published prior to Ludum Dare 29
///
/// Simple Unity3D script to show scores etc. on top of your scene.
/// Scales with your window size.
///
/// How to use:
///
/// * Create a new scene
/// * Attach this to your camera
/// * Update placeholder text accordingly and adjust colum count / sizing as required
///
/// Known issues:
///
/// * GUI.Label size is not calculated automatically
/// </summary>
public class Hud : MonoBehaviour
{
public GUIStyle TextStyle = new GUIStyle();
public int maxFontSize = 16;
private const int MaximumNumberOfCharactersPerLine = 30;
private const int ColumnCount = 4;
private const int FirstColumnStartCharacter = 1;
private const int SecondColumnStartCharacter = 9;
private const int ThirdColumnStartCharacter = 17;
private const int ForthColumnWidth = 4;
public void OnGUI()
{
var emSize = UpdateFontSizeToMatchCurrentDisplay();
var columnSpacing = CalculateColumnSpacing(emSize);
var firstColumnX = emSize.x * FirstColumnStartCharacter;
var secondColumnX = emSize.x * SecondColumnStartCharacter + columnSpacing;
var thirdColumnX = emSize.x * ThirdColumnStartCharacter + columnSpacing*2;
var fourthColumnX = Screen.width - emSize.x * ForthColumnWidth;
var firstRowY = 0f;
var secondRowY = emSize.y;
GUI.Label(new Rect (firstColumnX, firstRowY, 200, 32), string.Format("LIFES x {0:D2}", ExampleGameState.Lives), TextStyle);
GUI.Label(new Rect (thirdColumnX, firstRowY, 140, 64), "WORLD", TextStyle);
GUI.Label(new Rect (fourthColumnX, firstRowY, 120, 32), "TIME", TextStyle);
GUI.Label(new Rect (firstColumnX, secondRowY, 120, 32), string.Format("{0:D5}", ExampleGameState.Score), TextStyle);
GUI.Label(new Rect (secondColumnX, secondRowY, 160, 32), string.Format("COINS x {0:D2}", ExampleGameState.Coins), TextStyle);
GUI.Label(new Rect (thirdColumnX, secondRowY, 120, 32), "1 - 1", TextStyle);
GUI.Label(new Rect (fourthColumnX, secondRowY, 120, 32), string.Format("{0:D3}", ExampleGameState.SecondsLeft), TextStyle);
}
float CalculateColumnSpacing (Vector2 emSize)
{
return (Screen.width - (emSize.x * (MaximumNumberOfCharactersPerLine - ColumnCount))) / 3;
}
Vector2 UpdateFontSizeToMatchCurrentDisplay ()
{
var emSize = Vector2.zero;
var fontSize = maxFontSize;
do
{
TextStyle.fontSize = fontSize;
emSize = TextStyle.CalcSize(new GUIContent("E"));
fontSize--;
} while(emSize.x*MaximumNumberOfCharactersPerLine>Screen.width && fontSize>3);
return emSize;
}
}
public class ExampleGameState
{
public static int Score = 0;
public static int Lives = 3;
public static int Coins = 0;
public static int SecondsLeft = 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment