- Home /
Answer by Mmmpies · Mar 09, 2015 at 01:28 PM
You've not really given us much to go on but at least you've made decent attempt at tags so I'm going to assume you mean the new UI, that you want Text on Screen to be displayed when you click a button.
Could be lots of other things you're trying but that's what I'm working towards at the moment.
I've got a very basic input script for a keyboard. You attach it to the canvas. You also have a Text object on your canvas to receive the text. It also handles backspace and shift.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class MyKeyboard : MonoBehaviour {
private bool shiftOn = false;
public Text MyText;
public Image ShiftImage;
public void KeyClicked(string whichKey)
{
if (shiftOn)
MyText.text = MyText.text + whichKey.ToUpper ();
else
MyText.text = MyText.text + whichKey;
}
public void BackClicked()
{
if (MyText.text.Length > 0)
MyText.text = MyText.text.Substring (0, MyText.text.Length - 1);
}
public void ShiftClicked()
{
shiftOn = !shiftOn;
if (shiftOn)
ShiftImage.color = Color.gray;
else
ShiftImage.color = Color.white;
}
}
Put that on your canvas and add the buttons you want, drag the Text onto the public Text Slot and The shift button onto the ShiftImage slot.
Then for each button you add an onClick, drag the canvas onto the slot that appears and from the dropdown select MyKeyboard -> KeyClicked. As that function receives a string a box will appear so for the a key button put a in that box.
You can also set the backspace to call BackClicked and Shift to call ShiftClicked.
Your answer
Follow this Question
Related Questions
Incorrect button event positioning 0 Answers
Click Goes Everywhere 0 Answers
Only want to detect button click event 1 Answer
Detect UI Button Click Event in Update method 3 Answers
How to use button multiple times 1 Answer