【问题标题】:how to display text when hovering over game object unity悬停在游戏对象统一时如何显示文本
【发布时间】:2021-05-12 15:57:48
【问题描述】:

我的主要问题是 void OnMouseOver。 Unity 不想承认名称“square”和“i”。我无法为每个游戏对象编写单独的代码,因为我有大约 50 个对象需要应用此代码。当我将鼠标悬停在一个正方形上时,我需要弹出一个特定的数字。我不确定 void OnMouseOver 是否应该在 void Start 中,但是当它在它之外时会出现更多错误。任何意见是极大的赞赏!谢谢!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;

public class HeaterRead : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        using (var reader = new StreamReader(@"C:\Users\labom\Documents\Work 
    Files\Heater_Main_Output.csv"))
        {
            List<float> listTi = new List<float>();
            List<float> listVe = new List<float>();
            List<float> listTe = new List<float>();
            while (!reader.EndOfStream)
            {
                var line = reader.ReadLine();
                var values = line.Split(',');

                listTi.Add(float.Parse(values[0]));
                listVe.Add(float.Parse(values[2]));
                listTe.Add(float.Parse(values[3]));
                line = reader.ReadLine();
            }

           // temperature minimum = 300F;
           // temperature maximum = 720F;

            Mathf.Lerp(0, 1, (listTe[0] / 420F) - 0.71F);


            List<GameObject> listS = new List<GameObject>();

            for (int i = 1; i < 51; i++)
            {
                GameObject square = GameObject.Find("Square" + i.ToString());
                square.GetComponent<SpriteRenderer>().color = new Color(Mathf.Lerp(0, 1, (listTe[i - 1] / 420F) - 0.71F), 0, 0, 1);
                Debug.Log(square.name + "   " + listTi[i-1].ToString() + "(s)   " + listVe[i-1].ToString() + "(10 m/s)   " + listTe[i-1].ToString() + "(F)   " + (Mathf.Lerp(0, 1, (listTe[0] / 420F) - 0.71F)) + "% R");
            }

            void OnMouseOver()
            {
            Debug.Log(square.name + "   " + listTi[i-1].ToString() + "(s)   " + listVe[i-1].ToString() + "(10 m/s)   " + listTe[i-1].ToString() + "(F)   " + (Mathf.Lerp(0, 1, (listTe[0] / 420F) - 0.71F)) + "% R");
            }

        }
    }

    public string stringToEdit = "Heater Dashboard";
    public string stringToEdit2 = "Expected Readings";
    public string stringToEdit3 = "Actual Readings";


    void OnGUI()
    {
        stringToEdit = GUI.TextField(new Rect(300, 10, 115, 20), stringToEdit, 25);
    stringToEdit2 = GUI.TextField(new Rect(10, 125, 120, 20), stringToEdit2, 25);
    stringToEdit3 = GUI.TextField(new Rect(10, 270, 105, 20), stringToEdit3, 25);
    }

}

【问题讨论】:

  • 请使用正确的标签! unityscript 是或更好的是 JavaScript 风格,类似于早期 Unity 版本中使用的自定义语言,现在早已弃用!同样,仅仅因为您使用的是某个 IDE 并不意味着您的问题与该 IDE 有关。

标签: c# unity3d


【解决方案1】:

首先,您创建了一个本地函数 OnMouseOver(),它只能在Start()方法中使用。

其次...您在循环中创建了一个 local square 变量,因此它不能在循环之外使用,与 i 相同,但你试图在 for 循环之外同时调用它们

【讨论】:

  • 那么我需要做的就是从 Start() 中删除 OnMouseOver(),然后全局定义变量?
  • 实际上,了解您的代码是做什么的,以及您究竟想做什么,但如果您只想避免错误,这会有所帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-25
  • 2019-01-23
  • 2017-10-30
  • 1970-01-01
  • 2011-01-27
  • 2017-02-06
相关资源
最近更新 更多