我的游戏对象在游戏中不显示,就像我在GUI上设计的那样(Unity和C#)。

huangapple go评论65阅读模式
英文:

My game object does not show in the game as I have designed it on gui (Unity and C#)

问题

I am learning Unity and coding a lot with it lately. Following a toutorial I found on youtube, I made an inventory system so that I can randomly create items and hold them. I also want the player to be able to see an item description as well as the item name using a tooltip.

Following said toutorial, I have made the tooltip but when I play the game the UI design I have created does not carry over to the tooltip. It only shows me the text, where there should be a box. How this looks is shown in the following screenshot:

我的游戏对象在游戏中不显示,就像我在GUI上设计的那样(Unity和C#)。

There red square is where the window should appear like in the prefab that is created in the lower left of the screen.

I have been using the following code. The itemInfo that is set on the itemText prefab:

namespace Code.Inventory
{
    public class ItemInfo : MonoBehaviour
    {
        public Text _itemName;
        public Text _itemDescription;

        public void SetUp(string name, string description)
        {
            _itemName.text = name;
            _itemDescription.text = description;    
        }
    }
}

The following code represents the item where I write a description for an item:

public class Item : ScriptableObject
{
    public string _name = "Default Item";
    public Sprite _icon = null;
    public string _itemDescription = "Used to progress the story";

    /// <summary>
    /// Virtual means I can overwrite this function in another class once instantiated.
    /// This function is called when the item is actually clicked on. Momentarily it's only logging stuff.
    /// </summary>
    public virtual void Use()
    {
        Debug.Log("Hello World" + _name);
    }

    /// <summary>
    /// Simply returns the item description that is going to be created on the item class itself.
    /// </summary>
    /// <returns></returns>
    public virtual string GetItemDescription()
    {
        return _itemDescription;
    }
}

And the following is where I use the GameManager to create and destroy the GameObject:

/// <summary>
/// Displays item information when the mouse is hovered over an item.
/// </summary>
/// <param name="itemName"></param>
/// <param name="itemDescription"></param>
/// <param name="buttonPos"></param>
public void DisplayItemInfo(string itemName, string itemDescription, Vector2 buttonPos)
{
    if (_currentItemInfo != null)
    {
        Destroy(_currentItemInfo.gameObject);
    }

    // Create variables so that the tooltip window is moved to a different direction and not simply closed
    buttonPos.x -= 80;  
    buttonPos.y += 40;

    _currentItemInfo = Instantiate(_itemInfoPrefab, buttonPos, Quaternion.identity, _inventoryPanel);
    _currentItemInfo.GetComponent<ItemInfo>().SetUp(itemName, itemDescription);
}

/// <summary>
/// Destroy Item info so it's not staying behind when the mouse is leaving.
/// </summary>
public void DestroyItemInfo()
{
    if (_currentItemInfo != null)
    {
        float delayInSeconds = 0.5f; // Set the delay in seconds

        // Delay the destruction of the _currentItemInfo game object
        StartCoroutine(DelayedDestroy(_currentItemInfo.gameObject, delayInSeconds));

        _currentItemInfo = null; // Clear the _currentItemInfo variable
    }
}

IEnumerator DelayedDestroy(GameObject gameObjectToDestroy, float delayInSeconds)
{
    // Wait for the specified delay
    yield return new WaitForSeconds(delayInSeconds);

    // Destroy the game object
    Destroy(gameObjectToDestroy);
}

Last but not least, here I call said functions to actually do the work:

/// <summary>
/// Call item description when moused over an item.
/// </summary>
public void OnCourserEnter()
{
    // Call the data from the GameManager script
    GameManager.instance.DisplayItemInfo(_item.name, _item.GetItemDescription(), transform.position);
}

/// <summary>
/// Close item description when mouse leave the item.
/// </summary>
public void OnCourserExit()
{
    GameManager.instance.DestroyItemInfo();
}
英文:

I am learning Unity and coding a lot with it lately. Following a toutorial I found on youtube, I made an inventory system so that I can randomly create items and hold them. I also want the player to be able to see an item description as well as the item name using a tooltip.

Following said toutorial, I have made the tooltip but when I play the game the UI design I have created does not carry over to the tooltip. It only shows me the text, where there should be a box.
How this looks is shown in the following screenshot:

我的游戏对象在游戏中不显示,就像我在GUI上设计的那样(Unity和C#)。

There red square is where the window should appear like in the prefab that is created in the lower left of the screen.

I have been using the following code. The itemInfo that is set on the itemText prefab:

namespace Code.Inventory
{
    public class ItemInfo : MonoBehaviour
    {
        public Text _itemName;
        public Text _itemDescription;

        public void SetUp(string name, string description)
        {
            _itemName.text = name;
            _itemDescription.text = description;    
        }
    }
}

The following code represents the item where I write a description for an item:

public class Item : ScriptableObject
    {
        public string _name = &quot;Default Item&quot;;
        public Sprite _icon = null;
        public string _itemDescription = &quot;Used to progress the story&quot;;
    
        /// &lt;summary&gt;
        /// Virtual means I can overwrite this function in another class once instanciated.
        /// This function is called when the item is actually clicked on. Momentarely its only logging stuff.
        /// &lt;/summary&gt;
        public virtual void Use()
        {
            Debug.Log(&quot;Hello World&quot; + _name);
        }
    
        /// &lt;summary&gt;
        /// Simply returns the item description that is doing to be created on the item class itself.
        /// &lt;/summary&gt;
        /// &lt;returns&gt;&lt;/returns&gt;
        public virtual string GetItemDescription()
        {
            return _itemDescription;
        }
    }

And the following is where I use the GameManager to create and destroy the GameObject:

/// &lt;summary&gt;
        /// Displays item information when the mouse is hovered over an item.
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;itemName&quot;&gt;&lt;/param&gt;
        /// &lt;param name=&quot;itemDescription&quot;&gt;&lt;/param&gt;
        /// &lt;param name=&quot;buttonPos&quot;&gt;&lt;/param&gt;
        public void DisplayItemInfo(string itemName, string itemDescription, Vector2 buttonPos)
        {
            if (_currentItemInfo != null)
            {
                Destroy(_currentItemInfo.gameObject);
            }
        
            // Create variables so that the tooltip window is moved to a different direction and not simply closed
            buttonPos.x -= 80;  
            buttonPos.y += 40;
            
            _currentItemInfo = Instantiate(_itemInfoPrefab, buttonPos, Quaternion.identity, _inventoryPanel);
            _currentItemInfo.GetComponent&lt;ItemInfo&gt;().SetUp(itemName, itemDescription);
        }
        
        /// &lt;summary&gt;
        /// Destroy Item info so it&#39;s not staying behind when the mouse is leaving.
        /// &lt;/summary&gt;
        public void DestroyItemInfo()
        {
            if (_currentItemInfo != null)
            {
                float delayInSeconds = 0.5f; // Set the delay in seconds
        
                // Delay the destruction of the _currentItemInfo game object
                StartCoroutine(DelayedDestroy(_currentItemInfo.gameObject, delayInSeconds));
        
                _currentItemInfo = null; // Clear the _currentItemInfo variable
            }
        }

        IEnumerator DelayedDestroy(GameObject gameObjectToDestroy, float delayInSeconds)
        {
            // Wait for the specified delay
            yield return new WaitForSeconds(delayInSeconds);
            
            // Destroy the game object
            Destroy(gameObjectToDestroy);
        }

Last but not least, here I call said functions to actually do the work:

/// &lt;summary&gt;
        /// Call item description when moused over an item.
        /// &lt;/summary&gt;
        public void OnCourserEnter()
        {
            // Call the data from the GameManager script
            GameManager.instance.DisplayItemInfo(_item.name, _item.GetItemDescription(), transform.position);
        }
        
        /// &lt;summary&gt;
        /// Close item description when mouse leave the item.
        /// &lt;/summary&gt;
        public void OnCourserExit()
        {
            GameManager.instance.DestroyItemInfo();
        }

答案1

得分: 0

以下是已翻译的部分:

我能够以两种不同的方式重现红十字标记。

第一种方式是使用RectTransform的“正常”模式下的锚定预设,第二种方式是在Stretch模式下的锚定预设。

在第一种情况下,当在Rect Transform中设置宽度或高度(或两者兼有)为负数时,红十字标记会出现,如下面的图片所示。

使用正数宽度:

我的游戏对象在游戏中不显示,就像我在GUI上设计的那样(Unity和C#)。

使用负数宽度:

我的游戏对象在游戏中不显示,就像我在GUI上设计的那样(Unity和C#)。

第二种情况稍微复杂一些,但概念是相同的,当左、上、右或下字段使对象与自身重叠时,会出现这种情况,如下的gif所示,以更好地理解。

我的游戏对象在游戏中不显示,就像我在GUI上设计的那样(Unity和C#)。

(同样的概念适用于其他字段)

我能想到可能导致这种情况发生的原因有:

  1. 预制体设置不正确
  2. 在实例化对象时发生了错误
  3. 在代码的某处编辑了宽度或高度/上、下、左或右字段
  4. 实例化对象的父级Rect Transform存在问题(也许是_inventoryPanel?)

希望这有助于解决问题 我的游戏对象在游戏中不显示,就像我在GUI上设计的那样(Unity和C#)。

英文:

I was able to reproduce the red cross in 2 different ways.

The first is with the RectTransform's anchor presets in "normal" mode, and the second when the anchor presets are in Stretch mode.

In the first case the red cross happens when either the Width or Height (or both) are set as negative numbers in the Rect Transform, as shown in the pictures below.

With positive Width:

我的游戏对象在游戏中不显示,就像我在GUI上设计的那样(Unity和C#)。

With negative width:

我的游戏对象在游戏中不显示,就像我在GUI上设计的那样(Unity和C#)。

The second case is a bit more complicated to explain, but the concept is the same,and it happens when the Left, Top, Right or Bottom fields, make the object overlap with itself, as shown in the gif below to better understand it.

我的游戏对象在游戏中不显示,就像我在GUI上设计的那样(Unity和C#)。

(Same concept applies to the other fields)

The only things that may cause this to happen that I can think of are:

  1. The prefab is set up wrong
  2. Something goes wrong when you Instantiate the object
  3. Somewhere in the code you edit the Width or Height / Top, Bottom, Left or Right fields
  4. There are problems with the Rect Transform of a parent of the Instantiated object (maybe _inventoryPanel?)

Hope this helps to solve the problem 我的游戏对象在游戏中不显示,就像我在GUI上设计的那样(Unity和C#)。

huangapple
  • 本文由 发表于 2023年4月4日 04:06:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75923387.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定