Changing camera orthographic size via slider and field input.

So I’ve put a slider on my UI to change the orthographic size of the camera and set it. so far so good.

I also whant an Input field that show the value of the camera size But I can figure out

  1. how to get the value of the size in the input text at start.

  2. how to make sure the slider automaticaly set itself a the good place if I choose to change the value via the input field.

note: I can change the value via the input field and it work. but the slider don’t follow and when I start playing, the default value of the cam size doesn’t appear.

thanks y’all

Maybe something like this (pseudocode)?

void Start()
{
    MyInputField.text = MyCamera.orthosize+""; // convert to a string
}

public void OnInputFieldChanged() // you can assign this function in the editor in the input field component
{
    float size = 0f;
 
    if(float.TryParse(MyInputField.text, out size))
    {
        MyCamera.orthosize = size;
    } 
}

what is the parameter. the only thing I dont’ understand in that code. and else with that I might have found a part of what i’m looking for. I’ll keep you update thanks

I just realise I also forgot to use <UnityEngin.UI>… that doesn’t help. XD

Out basically just means it applies the result to the parameter. TryParse already returns a bool (successful or not), so it can’t return a float. So it has to use the out parameter to assign the result. float.TryParse(MyInputField.text, out size) basically just means “try and convert MyInputField.text to a float and assign it to size”.

si it’s like return for a method but there it’s for the parameter instead. thanks.

1 Like

Basically yea :sunglasses:

so you gave me the ONE line I needed to do my script. I do have an other question, but first here’s my script.

public class ScanInputField : MonoBehaviour
{
    public new Camera camera;
    private Slider slider;
    private InputField inputFieldText;

    private void Awake()
    {
        inputFieldText = GetComponent<InputField>();
        slider = GetComponentInParent<Slider>();
      
    }

    private void Update()
    {

        inputFieldText.text = camera.orthographicSize + "";

        if (camera.orthographicSize < 1100 && camera.orthographicSize > 100)
        {
            slider.value = camera.orthographicSize;
        }
       
    }

    public void InputChange(string newInput)
    {
   
            float cameraSize = float.Parse(newInput);
            camera.orthographicSize = cameraSize;
           
    }
}

Now the question: I what the input field to wait for the key entre to be press before applying the change. However everything I tried so far just ended up with me being unable to change the value of the field at all…

also needed to set a min and max value for the input field. don’t know how to integrate that now. I think I need to find a way to wait for a key press first.

up

Create some OnEnterKeyPresed() function, and in that function parse the text value of the input field into the float. Then use Mathf.Clamp on the float (Unity - Scripting API: Mathf.Clamp). That will make it only change when you press enter and will clamp the values.

In the OnEnterKeyPressed() function you might want to ensure that inputField.isFocused is true, otherwise it could trigger whenever the enter key is pressed, even if the user is not typing in the input field.

You can also use Input.GetKeyDown(KeyCode.Return)

yet again you gave me the two line I was missing:

  1. inputField.isFocused

2.mathf.Clamp.

with that I am prety sure i’ll be able to do everything I need to do. thanks mate! you’ve been very veyr helpfull here.

I’ll keep you up to date.

Bingo! and thanks for the help. finaly I didn’t need the OnEnter()… Unity does the job by itself when you call the methode inside OnEndEdit() instead of OnValueChange().

so my bad for this one… still thanks for the help. you gave me couple of line of code that I needed.

see ya. :smile:

1 Like