摩斯电码效果制作Unity教程 VR资源

manew_JR 2017-10-12 16:01:18
C#脚本设置对象在Morse Code的时间节奏下的Active / Inactive【闪烁】,如下所示:


 
 
 
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
// © 2017 TheFlyingKeyboard
// theflyingkeyboard.net
public class FlashInMorseCode : MonoBehaviour {
    [SerializeField] private float dotTime;
    [SerializeField] private GameObject objectToUse;
    [SerializeField] private bool flashOnStart = false;
    [SerializeField] private bool repeat = false;
    [TextAreaAttribute(4, 15)]
    [SerializeField] private string textToShow;
    private float dashTime;
    private char[] letters = { ' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' };
    private string[] morseLetters = { "    ", ". ___", "___ . . .", "___ . ___ .", "___ . .", ".", ". . ___ .", "___ ___ .", ". . . .", ". .", ". ___ ___ ___", "___ . ___", ". ___ . .", "___ ___", "___ .", "___ ___ ___", ". ___ ___ .", "___ ___ . ___", ". ___ .", ". . .", "_", ". . ___", ". . . ___", ". ___ ___", "___ . . ___", "___ . ___ ___", "___ ___ . .", ". ___ ___ ___ ___", ". . ___ ___ ___", ". . . ___ ___", ". . . . ___", ". . . . .", "___ . . . .", "___ ___ . . .", "___ ___ ___ . .", "___ ___ ___ ___ .", "___ ___ ___ ___ ___" };
    private void Start()
    {
        if (flashOnStart)
        {
            StartCoroutine(Flash(objectToUse, textToShow, dotTime));
        }
    }
    private IEnumerator Flash(GameObject objectToFlash, string textToConvert, float timeOfDot)
    {
        string textInMorse = "";
        ConvertTextToMorseCode(textToConvert, out textInMorse);
        for (int i = 0; i < textInMorse.Length; i++)
        {
            if (textInMorse[i] == ' ')
            {
                objectToFlash.SetActive(false);
                yield return 0;
                yield return new WaitForSeconds(timeOfDot);
            }
            else if (textInMorse[i] == '.')
            {
                objectToFlash.SetActive(true);
                yield return 0;
                yield return new WaitForSeconds(timeOfDot);
            }
            else if(textInMorse[i] == '_')
            {
                objectToFlash.SetActive(true);
                yield return 0;
                yield return new WaitForSeconds(timeOfDot * 3.0f);
            }
            if (repeat)
            {
                if(i == textInMorse.Length - 1)
                {
                    i = 0;
                }
            }
        }
    }  
    private void ConvertTextToMorseCode(string textToConvert, out string convertedText)
    {
        convertedText = "";
        textToConvert = textToConvert.ToLower();
        for (int i = 0; i < textToConvert.Length; i++)
        {
            for (short j = 0; j < 37; j++)
            {
                if (textToConvert[i] == letters[j])
                {
                    convertedText += morseLetters[j];
                    convertedText += "   ";
                    break;
                }
            }
        }
    }
    public void WriteText(GameObject newObject = null, string newTextToShow = null, float newDotTime = -1.0f)
    {
        if (newObject != null && newTextToShow != null && newDotTime > 0.0f)
        {
            StartCoroutine(Flash(newObject, newTextToShow, newDotTime));
            return;
        }
        if (newObject != null && newTextToShow != null)
        {
            StartCoroutine(Flash(newObject, newTextToShow, dotTime));
            return;
        }
        if (newObject != null && newDotTime > 0.0f)
        {
            StartCoroutine(Flash(newObject, textToShow, newDotTime));
            return;
        }
        if (newTextToShow != null && newDotTime > 0.0f)
        {
            StartCoroutine(Flash(objectToUse, newTextToShow, newDotTime));
            return;
        }
        if (newTextToShow != null)
        {
            StartCoroutine(Flash(objectToUse, newTextToShow, dotTime));
            return;
        }
        if (newDotTime > 0.0f)
        {
            StartCoroutine(Flash(newObject, textToShow, newDotTime));
            return;
        }
    }
99VR视界二维码
热门推荐
Hot Recommended
在线客服