我最近在学习Unity并用它编写了很多代码。在YouTube上找到一个toutorial之后,我做了一个库存系统,这样我就可以随机创建物品并保存它们。我还希望玩家能够看到物品描述以及使用工具提示的物品名称。
下面说toutorial,我做了工具提示,但当我玩游戏的UI设计,我已经创建不结转到工具提示.它只显示我的文本,那里应该有一个框.这看起来是如何在下面的截图显示:
红色方块是窗口应该出现的地方,就像在屏幕左下角创建的预制件一样。
我一直在使用下面的代码。itemInfo在itemText预置上设置:
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;
}
}
}
下面的代码表示我为项目编写描述的项目:
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;
}
}
下面是我使用GameManager创建和销毁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);
}
最后但并非最不重要的是,在这里我调用上述函数来实际完成工作:
/// <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条答案
按热度按时间ny6fqffe1#
我可以用两种不同的方式复制红十字。
第一个是Transform的锚预设处于“正常”模式,第二个是锚点预设处于拉伸模式。
在第一种情况下,当宽度或高度(或两者)在矩形变换中设置为负数时,会出现红色交叉,如下图所示。
具有正宽度:
负宽度:
第二种情况解释起来有点复杂,但概念是相同的,当左,上,右或下字段使对象与自身重叠时,它会发生,如下面的gif所示,以更好地理解它。
(Same适用于其他领域)
我能想到的唯一可能导致这种情况发生的原因是:
1.预制件设置错误
1.示例化对象时出错
1.在代码中的某个地方,您可以编辑宽度或高度/顶部、底部、左侧或右侧字段
希望这有助于解决问题:)