- Home /
Making Credits Scroll Upwards on Screen
Hello, I'm trying to make very simple credits for my game, and I was thinking about just making a GUIText prefab (creditTextItem), and then read items in from my credits text file, instantiate a new GUIText prefab set its position to off-screen add it to a list of GUIText prefabs, then each update cycle through the list update the position in the y axis a little bit. My code is below.
using UnityEngine; using System.Collections; using System.IO; using System.Collections.Generic;
public class CreditsScript : MonoBehaviour { public GUIText creditTextItem; private List<GUIText> Credits = new List<GUIText>(); private TextReader tr; public string path; private List<string> credits = new List<string>();
// Use this for initialization
void Start ()
{
// Set the path for the credits.txt file
path = "Assets/Resources/Credits.txt";
// Create reader & open file
tr = new StreamReader(path);
string temp;
while((temp = tr.ReadLine()) != null)
{
// Read a line of text
credits.Add(temp);
}
// Close the stream
tr.Close();
CreateCredits();
}
// Update is called once per frame
void Update ()
{
if (Credits.Count > 0)
{
for (int i = 0; i < Credits.Count; i++)
{
Credits[i].transform.Translate(new Vector3(0f, 1f, 0f));
}
}
}
void CreateCredits()
{
for (int i = 0; i < credits.Count; i++)
{
string c = credits[i];
Instantiate(creditTextItem);
creditTextItem.transform.position = GameObject.Find("CreditStart").transform.position;
creditTextItem.text = c;
Debug.Log(c);
Credits.Add(creditTextItem);
}
}
}
It's reading in the text file properly and updating the list's correctly. But is isn't displaying the items on screen any ideas?
Thanks, Hans
Answer by DaveA · May 13, 2011 at 05:57 PM
You could try this: http://answers.unity3d.com/questions/44728/how-do-i-make-credits
Answer by kilian277 · May 13, 2011 at 05:57 PM
What is just do is a make a very long cube or plane long in the Y direction, and i just put 3D text infront of the cube or plane.
And then i make the camera go up or down using a animation attached on the camera.
it's simple but it works.
Answer by HHameline · May 13, 2011 at 06:34 PM
I did some tinkering and it turned out being even easier just using the OnGui() function. Your answer did help me figure it out @ dave so you get the credit :D
public class CreditsScript : MonoBehaviour { public GUISkin creditSkin; public float creditSpeed; private TextReader tr; private string path; private List credits = new List (); private List positionRect = new List ();
// Use this for initialization
void Start ()
{
// Set the path for the credits.txt file
path = "Assets/Resources/Credits.txt";
// Create reader & open file
tr = new StreamReader(path);
string temp;
int count = 0;
while((temp = tr.ReadLine()) != null)
{
// Read a line of text
credits.Add(temp);
positionRect.Add(new Rect(200, 790 + (30 * count), 300, 100));
Debug.Log(temp);
count++;
}
// Close the stream
tr.Close();
}
// Update is called once per frame
void OnGUI()
{
GUI.skin = creditSkin;
for (int i = 0; i < credits.Count; i++)
{
GUI.Label(positionRect[i], credits[i], "item");
Rect tempRect = positionRect[i];
tempRect.y = tempRect.y - creditSpeed;
positionRect[i] = tempRect;
}
}
}
Thanks for the answers.
Hello, i am trying to implement the same thing, i tried your code but i don't think it works on the free version, or i F'ed it up lol. Can you give me a pointer on what i missed?
Im trying to learn using examples on here i am getting better but this puzzles me ;)
CS0246: The type or namespace name `List' could not be found
You need System.Collections.Generic in your namespace. In monodevelop, right click on list, and click resolve.
I wondered where the script should be linked to? @HHameline
I wondered where the script should be linked to? @HHameline