Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 ib970712 · Mar 15 at 07:25 AM · uiinputfield

How to disable character movement while editing InputField?

Sorry for the poor translation.

I want to prevent character movement while editing inputfield

At first, I blocked keyboard input by giving a bool value as IsActive Function , but another problem occurred, so this method cannot be used.

As shown in the picture, the character also moves during inputfiled input.

How to I solve it?

Thank you. alt text

ehdna.png (164.9 kB)
Comment
Add comment · Show 1
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 Funk465 · Mar 17 at 09:19 AM 0
Share

I focus on the input box when the user presses 'tab'. Now in the built in game player this works and I can type into the input box fine and it does not affect the player. However when I build the game despite being focused on the input box the first person controller moves around and jumps when I type into the input box. ePayitonline

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by asafsitner · Mar 16 at 01:07 PM

If you're using the UGUI system, you can use the EventSystem to find the currently selected UI object, like so: var selectedObject = FindObjectOfType<EventSystem>().currentSelectedGameObject;

Then if selectedObject is not null, you can check that it has an InputField component with GetComponent<InputField>().

You could even write an extension method like this:

 public static class EventSystemExtensions
 {
     public static bool IsTypeFocused<T>(this EventSystem self)
     {
         return self.currentSelectedGameObject.GetComponent<T>() != null;
     }
 }

and then do the check with if(FindObjectOfType<EventSystem>().IsTypeFocused<InputField>()) { /* code here */ }

Comment
Add comment · Show 7 · 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 ib970712 · Mar 16 at 02:01 PM 0
Share

I'm using UGUI System And, I've tried SetSelectedGameObject and ActivateInputField but both are isFocused Function gives false value

avatar image ib970712 · Mar 16 at 02:10 PM 0
Share

I checked that isFocused Function became true after a while because SetSelectedGameObject was not applied immediately, but the character can move even when isFocus is true.

avatar image asafsitner · Mar 16 at 03:19 PM 0
Share

I've setup a simple scene to test this out (and edited my answer's code to fix my type comparison mistake (currentSelectedGameObject always returns the gameObject):

 ----Canvas
 --------InputField
 --------Button
 ----EventSystem
 ----Camera

Then using a simple test script, I'm able to discern whether or not the input field is focused: using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI;

 public class UIFOcusTester : MonoBehaviour
 {
     public EventSystem events;
 
     // Start is called before the first frame update
     void Start()
     {
         events = FindObjectOfType<EventSystem>();
     }
 
     private void OnGUI()
     {
         if (events.currentSelectedGameObject)
         {
             InputField field = events.currentSelectedGameObject.GetComponent<InputField>();
             if (field)
             {
                 GUILayout.Box(field.name + " > " + field.isFocused);
             }
         }
     }
 }
avatar image ib970712 · Mar 17 at 04:41 AM 0
Share

Yeah, I checked that it is isfocused, but the character is still moving while editing InputField

alt text alt text

debug2.png (8.9 kB)
debug.png (31.3 kB)
avatar image asafsitner ib970712 · Mar 17 at 07:11 AM 0
Share

Just to make sure - now that you can tell whether an InputField has focus or not, you're sure to use it to block character movement, right? Like

 if(events.currentSelectedGameObject.GetComponent<InputField>().isFocused) { return; }
 else { /* character movement etc. here */ }

right?


Regarding your isFocused delay - if you never call ActivateInputField, does isFocused ever turn true? I was assuming all this time that you're clicking the input field, but that was a baseless presumption on my end.
Can you confirm how you're using the input field?

avatar image ib970712 · Mar 17 at 08:47 AM 0
Share

I understand that when using an inputfield it tells the character to stop moving.

However, I have already tried before and found it difficult.

If I set the character's speed to 0 immediately, the animation will be unnatural.

If I block the Move function, the animation of walking in place is executed without changing the speed.

Also I'm using GetAxis for character movement, I couldn't find a way to just block keyboard input.

So I was trying to figure out how to apply keyboard input only to UI when Inputfield is in use.

Also, I'm trying to use inputfield as a chatiing during the game, so inputfield is activated when I press enter instead of clicking to use the inputfield.

I'm sorry I couldn't explain in advance, and thank you for trying to help.

For reference, my character's behavior is like this: alt text

avatar image asafsitner ib970712 · Mar 17 at 11:53 AM 0
Share

Unfortunately the image is broken, but I can try to imagine what you're trying to do.
In that case, you'll want to have a method on your character script that gracefully stops the animation, and call that method when the user presses 'Enter' to start the chat. Exactly how to do that is entirely up to your specifications, but for example you can use Mathf.Lerp to gradually bring the character's speed to 0.


You can then subscribe to the chat input field's onEndEdit event, and resume control of the character. Always remember to unsubscribe from events before disabling/destroying the object, or you'll get null reference error. Something like:

     private void OnEnable()
     {
         /*
          * other code here
         */
 
         // subscribe to input field event, assuming the reference to it is already set earlier or in the inspector
         chatInputField.onEndEdit.AddListener(ResumeCharacterControl);
     }
 
     private void OnDisable()
     {
         /*
          * other code here
          */
 
         // it's important to unsubscribe from events or we'll get errors
         chatInputField.onEndEdit.RemoveListener(ResumeCharacterControl);
     }
 
     private void ResumeCharacterControl(string inputValue)
     {
         /*
          * code here
          */
     }


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

230 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 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 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

Click UI button while Android keyboard visible 0 Answers

Why does my TMP inputfield text disappears when I rotate It, why using a world space camera 0 Answers

adding a image inside of a muilti line Input Field? 0 Answers

How to insantiate an Inputfield which is populated with numerical values and to add the values of the field to get an output values? 0 Answers

How can I create a Multi line Input Field with Scrollbar? 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