Say i had an input named 'fire' mapped to a key 'z'. Is there any way to simulate either a button press or input value (within a ngui on screen button) in code rather than creating isolated functions for the fire action?
Same question please, it saves me many lines on a complicated demo scene.
shouldn't your input conect with functions? Just call them. If the input does more than calling one function you should consider wraping all the action of an input key to another function.
Answer by petrnita
May 02, 2015 at 02:52 PM
This is solution:
Download zip file on: http://inputsimulator.codeplex.com
Unzip that to Assets directory with Your script (C#) in Unity project
Reload MonoDevelop (if is openend)
In script on top write: using WindowsInput;
and ... in class You can use this for example: InputSimulator.SimulateKeyPress (VirtualKeyCode.RIGHT); //simulate right arrow press
I tried this code and it works great except I get duplicate characters for each keydown. I'm trying to type in uwebkit (a browser inside unity) so it may have something to do with that. Could someone verify that it did not duplicate characters when they used it?
Unfortunately this doesn't work on a Mac - I get "DllNotFoundException: user32.dll"
Have you find some solution on Mac?
This is almost perfect. it works exactly how I want it in the editor/player but when I build it onto android, it does not work!
Any ideas? I've been working on finding a solution for this all day :(
Answer by zeppike
May 02, 2015 at 10:01 AM
I would suggest this answer
Answer by DRProductions
Jan 18, 2014 at 08:03 PM
The best option is to just add a function for firing but if you don't want to do that you can try:
C# : http://social.msdn.microsoft.com/Forums/windows/en-US/f1b195b7-1568-46f5-83bb-e1e85b188af2/how-to-simulate-a-key-press-in-c?forum=winforms
These solution may or may not working in unity but it is what I found on the internet
I strongly suggest making a function as you may find that you want to fire in many different ways and functions just simplify your code so much.
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
25 People are following this question.
NGUI widget sprite background no longer moves with its button
How do I make a button press transition between phases properly?
GUI.Button's status react to keyboard event?
Placing button at corner using NGUI