Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by zmar0519 · Apr 12, 2011 at 01:00 AM · guiaudiotextstring

Input a string and the computer answers with a command?

Hi, everyone. Sorry about the title, I wasn't sure what to call this question. I am looking for a way to, if the user inputs "Audio.PlaySound(3)" to a text area, then the program will play sound three in array. I want the computer to intelegantly read text, in short. How would I do this?

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

4 Replies

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

Answer by yoyo · Apr 12, 2011 at 09:41 PM

Get your user to type in some javascript code, then use eval() to run it.

Put this javascript in your project, then try messing with the command string ...

var command = "Debug.Log(\"Hello World\")";

function Start() { eval(command); }

Yes, much simpler than my previous C# answer, wish I knew javascript better!!

Comment
Add comment · Show 2 · 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 Wibbs 1 · Apr 12, 2011 at 10:06 PM 1
Share

Depending on what you're trying to acheive here and who your users are there is a potential security issue with this as it will execute ANY code the user inputs.

avatar image yoyo · Apr 12, 2011 at 10:16 PM 0
Share

Absolutely. With C# you could isolate the exposed commands to a single assembly, is there any similar feature in javascript?

avatar image
1

Answer by Macdude2 · Apr 12, 2011 at 02:11 AM

If you wanted it to be really, really simple, couldn't you just check to see if the user has entered a certain string and then run a function based on that? Kind of like checking for a password? I doubt you would be able to do much more than that, unfortunately, with unity.

Comment
Add comment · Show 1 · 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 Justin Warner · Apr 12, 2011 at 02:21 AM 0
Share

Wow... Great idea, I'm totally burned out haha. But this is exactly what you'd have to do... Just compare Strings.

avatar image
0

Answer by Justin Warner · Apr 12, 2011 at 01:46 AM

Intelligently read text...

Um... I'm sorry, but computers never intelligently read text...

Maybe you should look up some things:

IBM's Watson.

Text-based games (Cons of).

And that should help you...

Goto start>run> cmd.exe > now try to use normal speech to make the computer do things =).

Just making a point, sorry...

But I don't know how you'd get input from a user and use it as a command, I'd actually like to know, so +1 =).

Comment
Add comment · Show 1 · 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 Jean-Fabre · Apr 12, 2011 at 06:07 AM 0
Share

Well, I think it's not a very positive/accurate vision. We're writing text on a daily bases that is processed some way or the other and do things. Scripting!?! That's a user Input transformed into a command. I have made system where if you type simple words, it does detect it and do things, very much like mouse gestures, or else. Totally possible for a particular and precise use, but impossible if you want a general all purpose system like the one you mentioned, I give you that.

avatar image
0

Answer by yoyo · Apr 12, 2011 at 05:21 AM

If you don't mind making your user type in actual code, you could use C# reflection to invoke methods on the fly, something like this:

using System; using System.Reflection;

public class MyMethodInvoker { public static void Invoke(string command) { int iPeriod = command.IndexOf("."); int iParen = command.IndexOf("("); string typeName = command.Substring(0, iPeriod); string methodName = command.Substring(iPeriod + 1, iParen - iPeriod - 1); string param = command.Substring(iParen + 1, command.Length - iParen - 2);

     Invoke(typeName, methodName, param);
 }

 public static void Invoke(string typeName, string methodName, params object[] methodParameters)
 {
     Type audioType = Assembly.GetExecutingAssembly().GetType(typeName);
     MethodInfo methodInfo = audioType.GetMethod(methodName, BindingFlags.Static | BindingFlags.Public);
     methodInfo.Invoke(null, methodParameters);
 }

}

For your example, you could then call MyMethodInvoker.Invoke("Audio.PlaySound(3)") and it would parse the string, then look up the Audio type and call the PlaySound method on it. My simple parsing logic above (which could definitely use some error-checking!!) will only parse out a single string parameter and hand it to PlaySound, which could deal with the conversion to integer, or you could make the parser better and handle multiple parameters of different types.

I've tested the code above against this trivial implementation of the Audio class ...

public class Audio
{
    public static void PlaySound(string param)
    {
        Debug.LogError("Here we are in Audio.PlaySound(" + param + ")");
    }
}

For more information on this technique, just google "C# reflection".

Obligatory disclaimers ...

  • error checking code needed
  • this exposes all your global classes -- you would want to put your user-callable methods in a separate pre-compiled assembly
  • won't work with javascript
  • I believe System.Reflection works in a web build, I don't think it works on iPhone
  • probably overkill for what you want, but this sort of thing can come in handy at times
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 yoyo · Apr 12, 2011 at 05:22 AM 0
Share

Or dispense with C# reflection and find or implement your own scripting language. lua would be a good candidate.

avatar image zmar0519 · Apr 12, 2011 at 09:06 PM 0
Share

Is there a way to do this in js? if not, thats ok, but it would be cool to keep everything in one script.

avatar image yoyo · Apr 12, 2011 at 09:35 PM 0
Share

I was going to say "I'm not a Java guy, but I don't think so" ... only if I were a Java guy I would have realized that you can simply call eval("Audio.PlaySound(3)") and it just works! eval() is a built-in method that runs a snippet of Java code.

avatar image yoyo · Apr 12, 2011 at 09:42 PM 0
Share

I just posted a new answer, see what you think.

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

No one has followed this question yet.

Related Questions

Changing 3d text through script 1 Answer

iOS performance: GUItext? want to use text string, but how to instantiate and edit it runtime? 0 Answers

Playing multiple sounds with multiple subtitles 1 Answer

Draw GUI Text if clicked out of webplayer 1 Answer

How do i take a string from a gui and show it in the gameworld c# 1 Answer


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