x


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

more ▼

asked May 13, 2011 at 05:55 PM

HHameline gravatar image

HHameline
222 44 34 46

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first
more ▼

answered May 13, 2011 at 05:57 PM

DaveA gravatar image

DaveA
33.2k 416 335 442

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered May 13, 2011 at 05:57 PM

kilian277 gravatar image

kilian277
564 85 79 92

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered May 13, 2011 at 06:34 PM

HHameline gravatar image

HHameline
222 44 34 46

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

Dec 21, 2013 at 09:57 PM TheGeekyDead

You need System.Collections.Generic in your namespace. In monodevelop, right click on list, and click resolve.

Feb 12 at 11:57 AM eatfrog
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x8735
x861
x128
x11
x7

asked: May 13, 2011 at 05:55 PM

Seen: 6100 times

Last Updated: Feb 12 at 11:57 AM