Author Topic: UIInput as a Console  (Read 3799 times)

Pirogun

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 11
    • View Profile
UIInput as a Console
« on: April 27, 2013, 11:37:53 PM »
Hello, I am trying to make a console to run commands and I have a problem with actually getting the command/

An example command that I have is to load a level

I know that this way works, but it is sloppy and inefficient:
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class LevelModifier : MonoBehaviour {
  5.        
  6.         public UIInput uii;
  7.        
  8.         void Start () {
  9.                
  10.                 uii = GetComponent<UIInput>();
  11.         }
  12.         void OnSubmit(){
  13.                
  14.                 if(uii.text == "loadLevel 0"){
  15.                         Application.LoadLevel(0);
  16.                        
  17.                 }else if(uii.text == "loadLevel 1"){
  18.                         Application.LoadLevel(1);
  19.                        
  20.                 }else if(etc,etc){
  21.                        
  22.                 }
  23.         }
  24. }
  25.  

I vaguely remember using %i in c++ but I don't think it will apply in this situation.

What would be the best method for going about getting the number in the string?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIInput as a Console
« Reply #1 on: April 27, 2013, 11:54:32 PM »
Application.LoadLevel(uii.text);

Pirogun

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 11
    • View Profile
Re: UIInput as a Console
« Reply #2 on: April 28, 2013, 12:00:43 AM »
But wouldn't this only work for loading levels and not other commands?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIInput as a Console
« Reply #3 on: April 28, 2013, 04:13:22 AM »
Yes. There is no sscanf/sprintf equivalent in C#. The closest is string.Format, and it's used only for output. You have to manually string.Split() your input string, and then do something depending on the values you find. To parse an int, use int.TryParse.

Pirogun

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 11
    • View Profile
Re: UIInput as a Console
« Reply #4 on: April 28, 2013, 12:24:31 PM »
OK thanks for the help!