Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class textEffect : MonoBehaviour {
- [Tooltip("Text to show")]
- public string text;
- [Tooltip("Space between characters. Default: 0.11")]
- public float characterSpacing;
- [Tooltip("Size of the characters")]
- public int fontSize;
- [Space]
- [Tooltip("Distance between the spawn position and the final position")]
- public float zOrigin;
- [Space]
- [Tooltip("Time between spawn and the final position")]
- public float animationDuration;
- [Tooltip("0 means infinite")]
- public float despawnTime;
- [Tooltip("Duration of the despawn effect")]
- public float fadeOutDuration;
- [Tooltip("Time between every character spawn")]
- public float characterDelay;
- [Tooltip("Delay applied to all characters")]
- public float characterOffsetDelay = 0;
- [Space]
- [Range(0,1)]
- [Tooltip("Random variation applied to every char")]
- public float errorRange;
- private float velocityIncrement;
- private Vector3[] localInitPosition;
- private Vector3[] localFinalPosition;
- private GameObject[] charactersGO;
- private float[] characterTime;
- private TextMesh[] characterTextMesh;
- private float time = 0;
- private float generalScale = 0.15f;
- private bool showScreens = true;
- private ScreenAnchor screenAnchor;
- private float videoDisplacement = 10;
- private Vector3 anchorFinalPosition;
- private Vector3 anchorInitPosition;
- private void OnEnable() {
- Setup();
- }
- void Setup() {
- //Moving the screen anchor
- showScreens = false;
- screenAnchor = transform.parent.GetComponent<Room>().screenAnchor.GetComponent<ScreenAnchor>();
- velocityIncrement = 1 / animationDuration;
- float spacingTotal = 0;
- localFinalPosition = new Vector3[text.Length];
- localInitPosition = new Vector3[text.Length];
- charactersGO = new GameObject[text.Length];
- characterTime = new float[text.Length];
- characterTextMesh = new TextMesh[text.Length];
- DestroyAll(true);
- for (int i = 0; i < text.Length; i++) {
- GameObject character = new GameObject("Character " + i);
- character.transform.parent = transform;
- TextMesh char3DText = character.AddComponent<TextMesh>();
- char3DText.fontSize = fontSize;
- char3DText.text = text.Substring(i, 1);
- character.transform.localScale = new Vector3(generalScale, generalScale, generalScale);
- CharacterInfo info;
- char3DText.font.GetCharacterInfo(char3DText.text[0], out info, char3DText.fontSize, char3DText.fontStyle);
- localInitPosition[i] = new Vector3(spacingTotal, 0, zOrigin + spacingTotal * Random.Range(-errorRange/2, errorRange/2));
- localInitPosition[i] += transform.position;
- localFinalPosition[i] = new Vector3(spacingTotal, 0, 0);
- localFinalPosition[i] += transform.position;
- character.transform.position = localInitPosition[i];
- charactersGO[i] = character;
- characterTime[i] = -characterDelay * i;
- characterTime[i] -= characterOffsetDelay;
- characterTextMesh[i] = char3DText;
- characterTextMesh[i].color = new Color(1, 1, 1, 0);
- spacingTotal += info.advance * characterSpacing * generalScale;
- }
- for (int i = 0; i < text.Length; i++) {
- localInitPosition[i].x -= spacingTotal / 2;
- localFinalPosition[i].x -= spacingTotal / 2;
- }
- }
- // Update is called once per frame
- void Update() {
- bool canDestroy = true;
- for (int i = 0; i < text.Length; i++) {
- float easeLerp = 0;
- if (characterTime[i] < 0) {
- easeLerp = 0;
- charactersGO[i].transform.position = Vector3.Lerp(localInitPosition[i], localFinalPosition[i], easeLerp);
- characterTextMesh[i].color = new Color(1, 1, 1, easeLerp);
- }
- else if (characterTime[i] < animationDuration) {
- easeLerp = Mathf.Sin((characterTime[i] / animationDuration) * Mathf.PI / 2);
- charactersGO[i].transform.position = Vector3.Lerp(localInitPosition[i], localFinalPosition[i], easeLerp);
- characterTextMesh[i].color = new Color(1, 1, 1, easeLerp);
- }
- else {
- if (characterTime[i] > despawnTime && despawnTime != 0) { //if despawnTime == 0, don't despawn.
- characterTextMesh[i].color = new Color(1, 1, 1, 1 - (characterTime[i] - despawnTime) / fadeOutDuration);
- showScreens = true;
- }
- }
- characterTime[i] += Time.deltaTime;
- if (despawnTime == 0 || characterTime[i] <= 0 + (despawnTime + fadeOutDuration)) {
- canDestroy = false;
- }
- }
- if (canDestroy) { //Destroy all characters
- DestroyAll(false);
- }
- if (showScreens) {
- screenAnchor.transform.position = Vector3.Lerp(screenAnchor.transform.position, screenAnchor.showingPosition, 0.05f);
- }
- else {
- screenAnchor.transform.position = screenAnchor.hiddenPosition;
- }
- }
- void DestroyAll(bool deactivateSpawner) {
- int tmp = charactersGO.Length - 1;
- while (tmp >= 0) {
- Destroy(charactersGO[tmp]);
- tmp--;
- }
- gameObject.SetActive(deactivateSpawner);
- }
- public void OnRoomExit() {
- DestroyAll(false);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement