• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by KnightRiderGuy · Feb 01, 2016 at 07:15 PM · c#uiinputfieldcodepagesubmit

Convert Message to Text Field Input

I found this very cool code in the forum for a Morse Code Generator, I'm trying to figure out how to make it so that I can use a UI Text input Field to type in Morse Code messages and then have them play on submit.

 using UnityEngine;
 using System.Collections;
 using System.Text.RegularExpressions;
 using UnityEngine.UI;
 
 public class MorseCodePlayer : MonoBehaviour {
 
     public InputField inputFieldMCode;
     
 
     public AudioClip dotSound;
     public AudioClip dashSound;
     public float spaceDelay;
     public float letterDelay;
     
     // International Morse Code Alphabet
     private string[] alphabet = 
     //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
      "...-", ".--", "-..-", "-.--", "--..",
     //0        1        2        3        4        
      "-----", ".----", "..---", "...--", "....-", 
     //5        6        7        8        9     
      ".....", "-....", "--...", "---..", "----."};
 
     // Use this for initialization
         void Start () 
     {
         inputFieldMCode = GameObject.Find ("InputField").GetComponent<InputField> ();
         // H   E  L    L   O       W  O    R   L   D
         //.... . .-.. .-.. ---    .-- --- .-. .-.. -..
                 //PlayMorseCodeMessage("Hello World.");
 
     }
                 
             //My UI Text Input Field For typed In Morse Code Message
         public void readButton (string c){
                 
                 inputFieldMCode.text += c;
                 //Play Morse Code Message on Submit
         }
 
     
     
         public void PlayMorseCodeMessage(string message)
     {
         StartCoroutine("_PlayMorseCodeMessage", message);
     }
     
         private IEnumerator _PlayMorseCodeMessage(string message)
     {
         // Remove all characters that are not supported by Morse code...
         Regex regex = new Regex("[^A-z0-9 ]");
         message = regex.Replace(message.ToUpper(), "");
         
         // Convert the message into Morse code audio... 
         foreach(char letter in message)
         {
             if (letter == ' ')
                 yield return new WaitForSeconds(spaceDelay);
             else
             {
                 int index = letter - 'A';
                 if (index < 0)
                     index = letter - '0' + 26;
                 string letterCode = alphabet[index];
                 foreach(char bit in letterCode)
                 {
                     // Dot or Dash?
                     AudioClip       sound = dotSound;
                     if (bit == '-')    sound = dashSound;
                     
                     // Play the audio clip and wait for it to end before playing the next one.
                     GetComponent<AudioSource>().PlayOneShot(sound);
                     yield return new WaitForSeconds(sound.length + letterDelay);
                 }
             }
         }
     }
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

80 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to restrict user from typing certain numbers in input field 3 Answers

Need alternative for onEndEdit on InputField 2 Answers

Detecting if is currently entering text on any component. 0 Answers

Handling Multiple U.I. Input Fields 0 Answers

How to make Input Field with middle text align? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges