Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 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
0
Question by Smaika · Apr 18 at 11:49 AM · uiinputfieldpositioning

How to position input field characters inside background boxes

So in my game, I allow the player to enter a 10-character long code. I want to position each character inside a background box using the input field. alt text How can I keep each character in its position while at the same time allowing the player to edit and change the entered code?

screenshot-1.jpg (25.8 kB)
Comment
Add comment · Show 5
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 Venividiviciuus · Apr 18 at 12:04 PM 0
Share

But do you want to choose which character can be changed? more information pls so there are Array to achieve something like this [c1, c2, c3] each key of the array has an index can be modifed through a function that get to change that particular index when pressed to insert the character by input there is For loop

avatar image Smaika Venividiviciuus · Apr 18 at 12:18 PM 0
Share

But do you want to choose which character can be changed?

Yes, if the player clicked in a particular box the caret should be in that box character. And as the player is entering the code the caret will jump to the next box, and back if backspace is pressed
avatar image Venividiviciuus Smaika · Apr 18 at 12:22 PM 0
Share

So I gave you the necessities you need if you can't build let me know;) @Smaika

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Venividiviciuus · Apr 18 at 11:39 PM

I wrote a code for you try to see if it is right for you, if you like it and you want to optimize it I can continue to help you. It is necessary: that you add this code to your panel and that all the boxes are InputFields, in charactersID and LV set the value corresponding to your fields

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class AddLevel : MonoBehaviour
 {
     [SerializeField] private InputField[] charactersID;                     //This array contains all "InputField-Text" GameObject in Hierarchy    (Canvas->UI)
     [SerializeField] string[] stringsID;                                    //This one contains the input values we insert in each inputfield that we will add up 
 
     [SerializeField] private InputField[] charactersLV;                     
     [SerializeField] string[] stringsLV;                                 
 
     [Space(20)] [SerializeField] public string ID;                         
     [Space(20)] [SerializeField] public string LEVEL;                      
 
     private int indexID = 0;                                                //This is a reference for the arrays
     private int indexLV = 0;
     private string tempID;                                                  //This is a temporary variable that will contains ours adds of strings. It will be the integer ID
     private string tempLV;
     private bool runOnce;                                                   //This variable is used for run a method once time and not update every frame
 
     
     
 
     private void Awake()
     {
         stringsID = new string[charactersID.Length];                        //Initializing of the strings for each value
         stringsLV = new string[charactersLV.Length];
         runOnce = false;                                                    
     }
 
     private void Update()
     {
         //Update function can be removed if you want to load script from other event
         Application();
     }
 
 
     #region StartProgram
     public void Application()
     {
 
         //START THE FIRST PART WHERE WE INSERT THE ID VALUE
         //Check if ID area isn't already used or completed
         if (indexID < charactersID.Length)
         {
             if(indexID == 0)
             {
                 //Start with first box selected (if-index==0)
                 charactersID[indexID].Select();
             }
 
             //Updated methods
             InsertValueID();
             ReadyToWriteID();
         }
 
         //Level ready check
         else
         {
             if (indexLV == 0)
             {
                 charactersLV[indexLV].Select();               
             }
             InsertValueLevel();
             ReadyToWriteLevel();
         }
 
 
     }
 
     #endregion
 
     #region ID
     private void InsertValueID()
     {
 
         runOnce = true;
         if (indexID < charactersID.Length)
             charactersID[indexID].onValueChanged.AddListener(delegate { NextValueID(); });  //onValueChanged can be found in InputField (addListener used for runtime next method)
 
     
     }
 
     private void NextValueID() //Selects the next input field once you leave one
     {
 
         if (runOnce)
         {
             //Here inserted the value we are going to add it to the array String
             ReplaceAtID(stringsID, indexID);
 
             //check if there are still fields and add index++
             if (indexID < stringsID.Length)
                 indexID++;            
 
             //select the next inputfield
             if (indexID < charactersID.Length)
             {
                 charactersID[indexID].Select();
             }
         }
 
         runOnce = false;
     }
 
     private void ReplaceAtID(string[] strings, int index)
     {
         //We insert the input value with the position in the string point
         strings[index] = charactersID[index].text;  
         
     }
 
     private void ReadyToWriteID()
     {
         
         if (indexID == charactersID.Length - 1)
         {
             //At the end we can call the method to write the id
             charactersID[indexID].onValueChanged.AddListener(delegate { GetID(); });
             
         }
     }
 
     private string GetID()
     {
         for (int i = 0; i < stringsID.Length; i++)
         {
 
             //sum of each index in array
             tempID += stringsID[i];
             Debug.Log("Writing ID" + tempID);
 
             if (i <= stringsID.Length)
             {
                 ID = tempID;
                 Debug.Log("GET ID" + ID);
             }
         }
 
         tempID = "";
         return ID;
         
 
     }
 
     #endregion
 
     //SAME SPEECH
     #region LEVEL
 
     private void InsertValueLevel()
     {
 
         runOnce = true;
         if (indexLV < charactersLV.Length)
             charactersLV[indexLV].onValueChanged.AddListener(delegate { NextValueLevel(); });
 
 
     }
 
     private void NextValueLevel()
     {
 
         if (runOnce)
         {
             ReplaceAtLevel(stringsLV, indexLV);
 
             if (indexLV < stringsLV.Length)
                 indexLV++;
 
             if (indexLV < charactersLV.Length)
             {
                 charactersLV[indexLV].Select();
             }
         }
 
         runOnce = false;
     }
 
     private void ReplaceAtLevel(string[] stringsLV, int indexLV)
     {
 
         stringsLV[indexLV] = charactersLV[indexLV].text;
 
     }
 
     private void ReadyToWriteLevel()
     {
         if (indexLV == charactersLV.Length - 1)
         {
 
             charactersLV[indexLV].onValueChanged.AddListener(delegate { GetLevel(); });
 
         }
     }
 
     private string GetLevel()
     {
         for (int i = 0; i < stringsLV.Length; i++)
         {
 
             tempLV += stringsLV[i];
             Debug.Log("Writing Level: " + tempLV);
 
             if (i <= stringsLV.Length)
             {
                 LEVEL = tempLV;
                 Debug.Log("Get Level: " + LEVEL);
             }
         }
 
         tempLV = "";
         return LEVEL;
 
 
     }
 
     #endregion
 }
 
 




Comment
Add comment · 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

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

231 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 avatar image

Related Questions

inputfield.caretposition in Android not working 0 Answers

Changing just the name of person in UI Text 0 Answers

Hide Textselection in Scrollable Inputfield 1 Answer

Unity UI: I want the InputField to show a placeholder, but clear it when the field is selected 1 Answer

Finding an objects position from one canvas to the other 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