GUI中的Unity,IntField?

时间:2017-03-14 20:10:33

标签: c# unity3d

如何编辑带有GUI.TextField的Int值或任何用于编辑的内容在GUI中使用Field for Int?
我无法使用代码为PlayerControl.Spawn(Int X, Int Y, Int Z)的字符串 我的代码是

namespace MonoInjector
{
using AssemblyCSharp;
using System;
using System.Net.Sockets;
using System.Reflection;
using UnityEngine;

public class CheatGUI3 : MonoBehaviour
{
    private bool ChatSpamTextOn = false;
    private bool DrawGUIOn = false;
    private int CoordinateX = 0;
    private int CoordinateY = 0;
    private int CoordinateZ = 0;
    private static CheatGUI2 cheatgui;
    private static Rect windowRect = new Rect(10f, 350f, 150f, 180f);
    private void DrawGUI()
    {
        windowRect = GUI.Window(0, windowRect, new GUI.WindowFunction(this.DrawGUIWindow), "Black3D");
    }
    private void DrawGUIWindow(int id)
    {
        int num = 0x16;
        GUI.Label(new Rect(2f, (float)num, 125f, 20f), "Coordinate X");
        num += 0x16;
        CoordinateX = GUI.TextField(new Rect(10, 10, 200, 20), CoordinateX, 40);
        num += 14;
        GUI.Label(new Rect(2f, (float)num, 125f, 20f), "Coordinate Y");
        num += 0x16;
        CoordinateY = GUI.TextField(new Rect(10, 10, 200, 20), CoordinateY, 40);
        num += 14;
        GUI.Label(new Rect(2f, (float)num, 125f, 20f), "Coordinate Z");
        num += 0x16;
        CoordinateZ = GUI.TextField(new Rect(10, 10, 200, 20), CoordinateZ, 40);
        num += 14;
    }
    private void HandleTeleport()
    {
        PlayerControl.Spawn(CoordinateX, CoordinateY, CoordinateZ);
    }

有错误

  

错误161最佳重载方法匹配' UnityEngine.GUI.TextField(UnityEngine.Rect,string,int)'有一些无效的论点

和错误

  

错误165参数2:无法转换为' int'到'字符串'

和错误

  

错误163参数3:无法转换为' int'到UnityEngine.GUIStyle'

编辑:我不能将String用于CoordinateX等

2 个答案:

答案 0 :(得分:2)

关于从public class Fraction extends Number{ private int numerator; private int denominator; public Fraction(String s){ this.numerator = Integer.parseInt(s.substring(0, s.indexOf("/") - 1)); this.denominator = Integer.parseInt(s.substring (s.indexOf("/") + 1, s.length() - 1)); } public String getFraction(){ String output = this.numerator + "/" + this.denominator; return output; } ///Methods for retrieving and changing both the numerator and the denominator public int getNum(){ return this.numerator; } public int getDenum(){ return this.denominator; } public void setNum(int num){ this.numerator = num; } public void setDenum(int denum){ this.denominator = denum; } public int intValue(){ return (Integer) this.numerator/this.denominator; } public double doubleValue(){ return this.numerator/this.denominator; } public long longValue(){ return this.numerator/this.denominator; } public short shortValue(){ return 0; } public float floatValue(){ return 0.0f; } } if (isset($_POST['submitted'])){ unset($_POST['submitted']); //sanitise those inputs. $username = $db->quote($_POST['username']); //select the user with the correct name $q = "select users.*, customers.* from users INNER JOIN customers on customers.username=users.username WHERE users.username = $username || customers.username = $username"; $rows = $db->query($q); //if the password matches then redirect foreach ($rows as $row) { if ($row["password"] == $_POST['password']) { //password matches //set some session variables for using later $_SESSION['username'] = $_POST['username']; $_SESSION['userid'] = $row['userid']; $_SESSION['customerid'] = $row['customerid']; $_SESSION['role'] = $row['role']; //redirect to either the staff or user home page based on the login if ($row["role"] == "customer"){ header( 'Location: customerHome.php' ); } else if ($row["role"] == "staff" || $row["role"] == "admin" ) { header( 'Location: staffHome.php' ); } else { header( 'Location: customerHome.php' ); } } } //if the redirect doesn't happen then there was an error with the password, so display this and the form //echo '<h1> There was a problem with your user name or password </h1>'; echo"<script language='javascript'> window.onload = function(){ var divs = document.getElementById('error'); divs.innerHTML = '<p style = \"color:#FF0000\">There was a problem with your username or password, please try again!</p>'; } </script>"; } 的转换的第一条错误消息可以很容易地解决,如下所示:

int

问题是string的类型为CoordinateX = GUI.TextField(new Rect(10, 10, 200, 20), CoordinateX.ToString(), 40); ,您将其传递给期望CoordinateX的方法。

最后但同样重要的是,您还必须将int的结果投射到string,因为它的类型是字符串。

GUI.TextField

答案 1 :(得分:1)

你可以做到

 CoordinateX = Mathf.RoundToInt(GUI.HorizontalSlider(new Rect(10, 10, 200, 20), CoordinateX , minValue, maxValue));