Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by StarlingSoftworksInteractive · Jan 27, 2017 at 08:13 AM · c#scripting beginnerinputfield

How to print something if a user types in a specific word in the inputfield?

Hey guys, Basically I don't how to state this but here it goes. In the input field how to recognize a list of commands so when the user types in a command it print a sentences e.g. I type in "New Game" and it print something or switch to a new game scene

Sorry if this don't make sense

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Hellium · Jan 27, 2017 at 08:39 AM

Here is a simple script I made. Let me know if you don't understand everything. I am comparing strings, but I think that regular expressions would be better. Especially if you want to play with command arguments.

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections.Generic;
 
 public class CommandPrompt : MonoBehaviour
 {
     // Drag & Drop the inputfield in the inspector
     public InputField inputfield;
     private Dictionary<string, System.Action<string,string>> commands;
 
     protected void Awake()
     {
         commands = new Dictionary<string, System.Action<string,string>>();

         // Add the commands you want to recognise along with the functions to call
         commands.Add( "new game", OnNewGameTyped );
         commands.Add( "hello", OnHelloTyped );

         // Listen when the inputfield is validated
         inputfield.onEndEdit.AddListener( OnEndEdit );
     }
 
     private void OnEndEdit( string input )
     {
         // Only consider onEndEdit if the Submit button has been pressed
         if ( !Input.GetButtonDown( "Submit" ) )
             return;
 
         bool commandFound = false;
 
         // Find the command
         foreach ( var item in commands )
         {
             if ( item.Key.ToLower().StartsWith( input.ToLower() ) )
             {
                 commandFound = true;
                 item.Value( item.Key, input );
                 break;
             }
         }
 
         // Do something if the command has not been found
         if ( !commandFound )
             Debug.Log( "No command found" );
 
         // Clear the input field (if you want)
         inputfield.text = "";
     }
 
     private void OnNewGameTyped( string command, string input )
     {
         Debug.Log( "Starting new game" );
         UnityEngine.SceneManagement.SceneManager.LoadScene( 1 );
     }
 
     private void OnHelloTyped( string command, string input )
     {
         Debug.Log( "Hello my friend !" );
     }
 }
Comment
Add comment · Show 4 · Share
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
avatar image StarlingSoftworksInteractive · Jan 27, 2017 at 08:42 AM 0
Share

Thanks for the fast reply mate, I am going to try and figure out how you did it, let you know if I don't understand somethings. Thanks again for helping me

avatar image StarlingSoftworksInteractive · Jan 27, 2017 at 08:44 AM 0
Share

Is it possible if I don't have a enter button and only run if the enter key is pressed?

avatar image Hellium StarlingSoftworksInteractive · Jan 27, 2017 at 08:57 AM 0
Share

That's the purpose of the following line :

  if ( !Input.GetButtonDown( "Submit" ) )

By default, in the Input manager, Submit is associated to the Enter key, but you could do something like :

  if( !Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Return))


avatar image StarlingSoftworksInteractive · Jan 27, 2017 at 10:44 AM 0
Share

Thank you for you help. one last question do you usually reward people who answer you question good?

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.

Follow this Question

Answers Answers and Comments

8 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Different language scripts - how to avoid trouble? 1 Answer

Anyone know why my InputField is not being disabled? 0 Answers

How to convert text field to input field and back again? 0 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