TextController.cs
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class TextController : MonoBehaviour
{
[SerializeField]
Text uiText;
[SerializeField]
[Range(0.001f, 0.3f)]
float intervalForCharacterDisplay = 0.05f;
private string currentText = string.Empty;
private float timeUntilDisplay = 0;
private float timeElapsed = 1;
private int currentLine = 0;
private int lastUpdateCharacter = -1;
public StartConversation Contact;
public GameObject Player;
private string[] scenarios = {
"あいうえお", //ここにはセリフを入れる。””で囲まれている部分が一回で表示されるセリフ。
"かきくけこ",
"さしすせそ",
"たちつてと"
};
// 文字の表示が完了しているかどうか
public bool IsCompleteDisplayText
{
get { return Time.time > timeElapsed + timeUntilDisplay; }
}
void Start()
{
}
void Update()
{
if (Contact.contact == false)
{ currentLine = 0; }
// 文字の表示が完了してるならクリック時に次の行を表示する
if (IsCompleteDisplayText)
{
if (currentLine < scenarios.Length && Input.GetMouseButtonDown(0))
{
SetNextLine();
}
}
else
{
// 完了してないなら文字をすべて表示する
if (Input.GetMouseButtonDown(0))
{
timeUntilDisplay = 0;
}
}
int displayCharacterCount = (int)(Mathf.Clamp01((Time.time - timeElapsed) / timeUntilDisplay) * currentText.Length);
if (displayCharacterCount != lastUpdateCharacter)
{
uiText.text = currentText.Substring(0, displayCharacterCount);
lastUpdateCharacter = displayCharacterCount;
}
}
public void SetNextLine()
{
currentText = scenarios[currentLine];
timeUntilDisplay = currentText.Length * intervalForCharacterDisplay;
timeElapsed = Time.time;
currentLine++;
lastUpdateCharacter = -1;
}
}
StartConversation.cs
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityStandardAssets.Characters.ThirdPerson;
public class StartConversation : MonoBehaviour
{
public GameObject TextController;
public TextController textcontroller;
public bool contact;
public GameObject canvas;
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnTriggerEnter(Collider collision)
{
if (collision.gameObject.tag == "Ball2")
{
contact = true;
textcontroller.SetNextLine();
canvas.SetActive(true);
}
}
void OnTriggerExit(Collider collision){
contact = false;
canvas.SetActive(false);
}
}