• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Aparker · Aug 05, 2013 at 03:25 PM · boxlogchatprint

I want to create an in game log which will print actions that the player is making

This picture has an example of what i want to create:

http://www.dadsgamingaddiction.com/wp-content/uploads/2012/10/NEO1.jpg

At the bottom of the screen is a 5-10 line box which updates every time the player makes an action such as using items and then displays this action back to the player.

How would I go about starting this in unity, I have tried searching but I couldnt find any tutorials or guides to making this type of log.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Wiki

Answer by Kevin002 · Aug 05, 2013 at 05:18 PM

Create a new script named PlayerLog

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class PlayerLog : MonoBehaviour 
 {
     // Private VARS
     private List<string> Eventlog = new List<string>();
     private string guiText = "";
 
     // Public VARS
     public int maxLines = 10;
     
     void OnGUI()
     {
         GUI.Label(new Rect(0, Screen.height - (Screen.height / 3), Screen.width, Screen.height / 3), guiText, GUI.skin.textArea);
     }
 
     public void AddEvent(string eventString)
     {
         Eventlog.Add(eventString);
 
         if (Eventlog.Count >= maxLines)
             Eventlog.RemoveAt(0);
 
         guiText = "";
 
         foreach (string logEvent in Eventlog)
         {
             guiText += logEvent;
             guiText += "\n";
         }
     }
 }
 

then simply get the component and call the AddEvent Function to add the string you wish displayed. For example:

     private PlayerLog eventLog;
 
     void Start()
     {
         eventLog = GetComponent<PlayerLog>();
     }
     
     void Update () 
     {
         if (Input.GetKey(KeyCode.LeftArrow))
             eventLog.AddEvent("Player Moves Left");
 
         if (Input.GetKey(KeyCode.RightArrow))
             eventLog.AddEvent("Player Moves Right");
     }

the result is this

alt text


guitexteventlog.png (20.9 kB)
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kevin002 · Aug 05, 2013 at 05:36 PM 0
Share

Agreed. I've updated my answer to add Scrolling effect and Swapped TextArea for Label with the Skin of a text area to keep visual.

avatar image getyour411 · Aug 05, 2013 at 05:50 PM 0
Share

This solution does not appear to do anything when the number of lines exceeds the size of the box

avatar image Kevin002 · Aug 05, 2013 at 06:19 PM 0
Share

I appreciate the advice. I didn't take much time to properly set up the code I wrote thinking it was just an example and not something that would be integrated line for line. I didn't think about the fact that most people simply copy/paste the code they see on these answers.

I do agree however that it should be done properly. I've reformatted the code to only rebuild the string when adding an event. I've also removed direct access to the list and added the AddEvent function ins$$anonymous$$d.

avatar image getyour411 · Aug 05, 2013 at 07:22 PM 0
Share

I was translating robertbu's example to C# in the interim which uses a similar approach but also might serve as a basic example of using Queue ins$$anonymous$$d of List:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
      
 public class playerLog : $$anonymous$$onoBehaviour {
     
     public int maxLines = 8;
     private Queue<string> queue = new Queue<string>();
     private string $$anonymous$$ytext = "";
      
     public void NewActivity(string activity) {
         if (queue.Count >= maxLines)
             queue.Dequeue();
         
           queue.Enqueue(activity);
         
         $$anonymous$$ytext = "";
         foreach (string st in queue)
                $$anonymous$$ytext = $$anonymous$$ytext + st + "\n";
         }
         
      
     void OnGUI() {
 
      GUI.Label(new Rect(5,                             // x, left offset
                   (Screen.height - 150),            // y, bottom offset
                   300f,                                // width
                   150f), $$anonymous$$ytext,GUI.skin.textArea);    // height, text, Skin features
         
     }
 }

Thank you both $$anonymous$$evin and Robertbu, from this post and your examples, I've now got a working game log which has been on my ToDo list for some time, I hope AParker does too.

avatar image Crixu · Apr 29, 2015 at 04:42 PM 0
Share

I want to clear my log, but every time it writes a new line it has all of the old lines. Anyone has an idea how to fix it?

avatar image
1

Answer by robertbu · Aug 05, 2013 at 04:47 PM

My depth with GUI is not great, so there might be a better way. But here is a bit of sample code that displays some lines of text. Attach script to a game object and hit play:

 #pragma strict
 import System.Collections.Generic;
 
 var maxLines = 5;
 
 private var queue = new Queue.<String>();
 private var testStrings = ["Eating crackers", "Stomping foot", "Trying lock", "Drinking potion", "Picking up dagger", "Hitting the wall", "Turning off lights"];
 private var text = "";
 
 function Start() {
     InvokeRepeating("Test", 0, 2);
 }
 
 function Test() {
     NewActivity(testStrings[Random.Range(0,testStrings.Length)]);
 }
 
 function NewActivity(activity : String) {
     if (queue.Count >= 5)
         queue.Dequeue();
     queue.Enqueue(activity);
     
     text = "";
     for (var st : String in queue)
         text = text + st + "\n";
 }
 
 function OnGUI() {
     GUI.Label(Rect(0,0,500,300), text);
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Debug.log,print not working when run from mobile android 0 Answers

Turning RPC logging off 1 Answer

What's the difference between print() and Debug.Log() 1 Answer

How do you get console messages in order? 0 Answers

Health value isn't subtracting correctly 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges