【问题标题】:Multiplying the ToString value in Unity C#在 Unity C# 中乘以 ToString 值
【发布时间】:2017-04-05 22:52:27
【问题描述】:

我创建了一个代码,用于获取 localscale 值并将其转换为字符串。据推测,它应该通过将其乘以 10 将其测量单位更改为厘米。问题是,尝试将字符串中的值相乘是行不通的,因为它毕竟是一个字符串。我还计划尝试将其转换为字符串,但不幸的是我变得更加困惑。有什么方法可以添加/减去/乘以我已转换为字符串的 localscale 值?这是我没有公式的香草代码。

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;

 public class ScaleSlider : MonoBehaviour {

     public Slider sliderLength;
     public Slider sliderWidth;
     public Slider sliderHeight;
     public Text textLength;
     public Text textWidth;
     public Text textHeight;

     // Use this for initialization
     void Start () {
         sliderWidth.value = 1f;
         sliderLength.value = 1f;
         sliderHeight.value = 1f;
     }

     void Update () {
         textLength.text = transform.localScale.x.ToString();
         textWidth.text = transform.localScale.y.ToString();
         textHeight.text = transform.localScale.z.ToString();
         transform.localScale = new Vector3(sliderLength.value*1, sliderWidth.value*1, sliderHeight.value*1);

     }

 }

【问题讨论】:

  • int.Parse(string)
  • 你的意思是:(transform.localScale.x * 10).ToString(); ?
  • 谢谢 rvdk。绝对是我需要的 :) 遗憾的是,我无法将您的评论作为答案投票。

标签: c# unity3d int tostring


【解决方案1】:

您需要先将其转换回数字Type。 试试:

float.Parse(transform.localScale.x.ToString()) *= 10f;

或者,如果您想要实现的是将它相乘并然后将其转换为字符串,您可以尝试:

(transform.localScale.x * 10f).ToString();

【讨论】:

    猜你喜欢
    • 2020-10-11
    • 2022-01-17
    • 1970-01-01
    • 2010-12-02
    • 1970-01-01
    • 2019-05-20
    • 1970-01-01
    • 2012-08-04
    相关资源
    最近更新 更多