Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: Pirogun on April 27, 2013, 11:37:53 PM

Title: UIInput as a Console
Post by: Pirogun 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?
Title: Re: UIInput as a Console
Post by: ArenMook on April 27, 2013, 11:54:32 PM
Application.LoadLevel(uii.text);
Title: Re: UIInput as a Console
Post by: Pirogun on April 28, 2013, 12:00:43 AM
But wouldn't this only work for loading levels and not other commands?
Title: Re: UIInput as a Console
Post by: ArenMook 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.
Title: Re: UIInput as a Console
Post by: Pirogun on April 28, 2013, 12:24:31 PM
OK thanks for the help!