Advertisement

Scrolling text area

Started by April 27, 2015 07:43 PM
1 comment, last by rogerdv 9 years, 9 months ago

I want to create a zone were my game displays text messages. I googled and managed to make a quick test, but it only displays a line, and I cant find how to modify it to add text lines from any place on the game (the player class, the gameplay class, etc):


public class MsgList : MonoBehaviour {
	private Vector2 scrollPosition;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {

	}

	void OnGUI() {
		scrollPosition = GUILayout.BeginScrollView (scrollPosition, GUILayout.Width (200), GUILayout.Height (50));
		GUILayout.Label ("Test message");
		GUILayout.EndScrollView ();
	}
}

How can I do this?

By guess is that you will need to make a variable for the text, and then have setText and getText functions. I will make it easy for you:


public class MsgList : MonoBehaviour {
	private Vector2 scrollPosition;
        private String text;
	// Use this for initialization
	void Start () {
	    text="";
	}
	
	// Update is called once per frame
	void Update () {

	}

	void OnGUI() {
		scrollPosition = GUILayout.BeginScrollView (scrollPosition, GUILayout.Width (200), GUILayout.Height (50));
		GUILayout.Label (text);
		GUILayout.EndScrollView ();
	}
        public void setText(string Text){
            text=Text;
        }
}

Assuming this is C# (This looks like Unity, am I right?)

Now all you need to do from the player is, in unity, do something like FindObjectsOfType, and either call setText on it or send a message. Eventually, you will want a more complicated system that keeps strings in a cage while making sure each one is shown for enough time, and you might also want play a sound or visual effect.

My CVMy money management app: ELFSHMy game about shooting triangles: Lazer of Death
Advertisement

Thanks, can you please explain how to do the setText call or message sending? Im tring to get it via FindObjectsOfType, but Im getting errors about Object[] not haveing setText, Im probably doing it the wrong way.

This topic is closed to new replies.

Advertisement