英文:
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:
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:
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 instanciated.
/// This function is called when the item is actually clicked on. Momentarely its only logging stuff.
/// </summary>
public virtual void Use()
{
Debug.Log("Hello World" + _name);
}
/// <summary>
/// Simply returns the item description that is doing 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();
}
答案1
得分: 0
以下是已翻译的部分:
我能够以两种不同的方式重现红十字标记。
第一种方式是使用RectTransform的“正常”模式下的锚定预设,第二种方式是在Stretch模式下的锚定预设。
在第一种情况下,当在Rect Transform中设置宽度或高度(或两者兼有)为负数时,红十字标记会出现,如下面的图片所示。
使用正数宽度:
使用负数宽度:
第二种情况稍微复杂一些,但概念是相同的,当左、上、右或下字段使对象与自身重叠时,会出现这种情况,如下的gif所示,以更好地理解。
(同样的概念适用于其他字段)
我能想到可能导致这种情况发生的原因有:
- 预制体设置不正确
- 在实例化对象时发生了错误
- 在代码的某处编辑了宽度或高度/上、下、左或右字段
- 实例化对象的父级Rect Transform存在问题(也许是_inventoryPanel?)
希望这有助于解决问题
英文:
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:
With negative width:
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.
(Same concept applies to the other fields)
The only things that may cause this to happen that I can think of are:
- The prefab is set up wrong
- Something goes wrong when you Instantiate the object
- Somewhere in the code you edit the Width or Height / Top, Bottom, Left or Right fields
- There are problems with the Rect Transform of a parent of the Instantiated object (maybe _inventoryPanel?)
Hope this helps to solve the problem
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论