- Home /
Gui , Message , Text, Touch and Question ?
PS- Sorry for the title of the question. So, i am trying to make a message box seen in rpgs and visual novels , i am working on Android platform and i have this script:
so what problem is , when i touch the gui on the screen , it skips the line "man,,." and everything that would come in between to the last one that is "i am getting late for the battle" So anyone got something i can do ? Thanx
Answer by ks13 · Dec 28, 2011 at 08:17 AM
Why not use an array, then cycle trough your lines when the touchCont is touched?
Edit : ok, here's an exemple (just to let you know i code in C# usually so the code might not be right but the algorythm is) :
var touchCont : GUITexture; var line1 : GUIText; var line2 : GUIText; var line3 : GUIText; var lines = new Array; lines[0] = "man..."; lines[1] = "i am going to be late for the\nbattle"; var line : int = 0;
function FixdUpdate() { for (var touch : Touch in Input.touches) { if (touchCont.HitTest(touch.position)) line++; } AssignLine(); }
function AssignLine() { var linetab = lines[line].split("\n"); for (var i = 0; i < linetab.length; i++) { if (i == 0) line1 = linetab[i]; if (i == 1) line2 = linetab[i]; if (i == 2) line3 = linetab[i]; } }
Didn't write it properly, but the the idea is there :
Use an array to put all your sentences in, then cycle trough the array on each touch. Use another function to split the sentences into the lines as you see fit.
Edit : Below is the C# version of the code (JS and C# don't mix well in Unity so you better stick to only one of those) :
using UnityEngine; using System; using System.Collections; using System.Collections.Generic;
public class LineManager : MonoBehaviour //Symbolic name, change to whatever you want { //The texture to touch and change the lines public GUITexture touchCont; //The array containing the line to show private GUIText[] shownLines = new GUIText[3]; //The array containing the lines to cycle trough private string[] charLines = new string[] { "man...", "i am going to be late for the\nbattle", "some other line"}; //The current line number private int currentLine = 0;
void FixedUpdate()
{
//Check for each touch if it's on the button(?), and launch the line showing if it is
foreach (var item in Input.touches)
{
if (touchCont.HitTest(item.position))
FillLine();
}
}
void FillLine()
{
//Split the sentence in lines if there is more than 1 line
var tmp = charLines[currentLine].Split(new char[] { '\n' });
//A safety check
if (tmp != null)
//Go from first to last line and put in the array to show on screen
for (int i = 0; i < tmp.Length; i++)
{
shownLines[i].text = tmp[i];
}
//If the next line number is inferior to the max lines number we go to the next, othewise go to the begining
if (currentLine == (charLines.Length - 1))
currentLine++;
else
currentLine = 0;
}
}
it's the "backslash"n, need to change it to 'backslash'n, i think. (doesn't want to show the actual character)
As i said, i wrote it fast so there may be mistakes.
Once more, i did the code in haste and from what little i know of javascript, so you'll need to adapt it. From what i know, split is a member of string, so you have to use a string.
Edit : It doesn't recognize the type of the variable because i used var lines = new Array; without specifying the type of the variable.
Hmm, the problem is i don't know how you manage the selection fo the GUIText. The code i wrote is for dynamically loading the sentences in an array. Since the array adresses don't change, the text should change when it's loaded in. So i suppose you should replace in your code line1, line2 and line3 by shownLines[0], shownLines[1] and shownLines[2].
Oh, and don't forget to validate the answer once it works (too many people leave to never come back forgetting to do this).
Answer by Julien-Lynge · Dec 28, 2011 at 03:00 AM
You're overwriting the text later on. You have two for loops, both identical, and two if statements, both identical, so when one is true the other will be true. You assign to line1.text, and then assign again to line1.text immediately afterward. Did you mean to do the following?
line1.text = "man,,.";
line2.text = "i am going late for the"; line3.text = "battle";
i think you got the most of it , what i want is on the first touch "man,,." should come on line1.text and on the seconds touch , line1.text will be "i am going late for " like in RPG they tell the story , so player read it then presses the button to see the next text