[Unity3D] Use InputField to make a simple UI and assign a value to the height y of GameObject

some nonsense

I am a Unity newbie who has always had a close relationship with Unity. I have always wanted to implement a function that allows me to input parameters in Unity and change the basic value of an object. This function seems very simple when I think about it, but I give up every time because various bugs are not solved. Today I asked New Bing three questions. I gave it a try and finally succeeded.


Bugs that were stuck before

InputField has updated its class name and cannot be dragged after being referenced.

You can read my previous post about this problem:
[Unity3D] Solution to drag-and-drop assignment of Text, InputField and other components in the inspector panel

Assign a value to a certain coordinate in GameObject's Transform.position individually

Insert image description here
New Bing has already answered very clearly:

To get the Position of GameObject, you can use the Transform.position property, which is a Vector3 value that contains the three components of x, y, and z. For example:

//获取GameObject的Transform组件
Transform transform = gameObject.GetComponent<Transform>();
//获取GameObject的Position
Vector3 position = transform.position;

To assign a new value to y of the GameObject's Position, you can modify the y component of the position variable and then assign position to transform.position. For example:

//给position的y赋新值
position.y = 10f;
//把position赋值给transform.position
transform.position = position;

float.Parse(), format error FormatException

Insert image description here

 public TMP_InputField inputField;
 public TMP_Text inputText;
...
height = float.Parse(inputText.text);

The error reported is FormatException: Input string was not in a correct format.

Insert image description here

Answer the following:

  • The float.Parse() function is used to convert a string to a floating point number, but if the format of the string does not comply with the specifications of floating point numbers, a FormatException will be thrown. For example, this error can result if the string contains non-numeric characters, or if there are extra zeros after the decimal point.
  • To avoid this error, you can use the float.TryParse() function to try to convert a string to a floating point number and return a Boolean value indicating whether it was successful. If it succeeds, you can use the out parameter to get the converted floating point number; if it fails, you can give the corresponding prompt or processing logic.
  • Also, you need to pay attention to where you get the string value from. If you are getting the text value from Unity's InputField component, then you should not directly access the text property in its sub-object Text component, because that is for display to the user and may contain some hidden characters. You should access the text property of the InputField component itself to get the value entered by the user.

Just change it to the following. It is indeed caused by hidden characters.

public TMP_InputField inputField;
public TMP_Text inputText;
...
height = float.Parse(inputField.text);

When you click the button, the value only changes for a moment and then returns to its original state.

The specific reason is not yet understood, but I later placed the function in Update(), and the effect is similar for the time being (to be optimized).

Insert image description here


Complete code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using System;

public class FPSHeightController : MonoBehaviour
{
    
    
    public TMP_InputField inputField;
    public TMP_Text inputText;
    public GameObject fpsc;
    public float height = 1.6f;

    // Start is called before the first frame update
    void Start()
    {
    
    
    }   

    // Update is called once per frame
    void Update()
    {
    
    
        FpsControll();
    }

    public void FpsControll(){
    
    
        height = float.Parse(inputField.text); 
        
        //获取GameObject的Transform组件
        Transform transform = fpsc.GetComponent<Transform>();
        //获取GameObject的Position
        Vector3 position = transform.position;

        //给position的y赋新值
        position.y = height;
        //把position赋值给transform.position
        transform.position = position;
    }
}

Create a new GameObject in the Hierarchy, mount the script, and drag what needs to be dragged to the corresponding location.


postscript

After spending a little time installing plug-ins, waiting for emails, and deleting cookies, I excitedly shared chatGPT with my roommates, probably just like the Chinese college students who struggled to connect to the Internet thirty years ago and had just come into contact with the new world of the Internet...

Guess you like

Origin blog.csdn.net/weixin_41932970/article/details/129456397