C# script to set object Active/Inacactive in Morse Code time rythm.

Content not available.
Please allow cookies by clicking Accept on the banner

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);
            }

            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;
        }
    }
}

 

Unity C# Flashing Object In Morse Code
Tagged on:     

2 thoughts on “Unity C# Flashing Object In Morse Code

  • August 9, 2018 at 12:39 am
    Permalink

    This helped a lot, thanks! I changed it to suit my needs, but this saved a me a bunch of time.
    Oh, also you accidentially tripled the dash time in two different places, making dashes six dots long.

    Reply
    • August 9, 2018 at 7:57 pm
      Permalink

      Thank you for finding it, fixed it 🙂 I always make some stupid errors 😛

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

By continuing to use the site, you agree to the use of cookies. You can read more about it the Cookies&Privacy Policy Section Above. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this. You can read more about it the Cookies&Privacy Policy Section.

Close