Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当玩家与物体碰撞时,我们如何显示图像?_C#_Image_Unity3d_2d Games_Inventory - Fatal编程技术网

C# 当玩家与物体碰撞时,我们如何显示图像?

C# 当玩家与物体碰撞时,我们如何显示图像?,c#,image,unity3d,2d-games,inventory,C#,Image,Unity3d,2d Games,Inventory,我正在开发2D Unity游戏,我想知道当玩家与物体碰撞时,如何在我的物品清单上显示图像。例如,如果玩家与鼓槌碰撞,鼓槌图像将出现在库存菜单上 致以最良好的祝愿 Yacine TAZDAIT.方法1 有许多不同的方式来显示图像。例如,您可以拥有一个包含图像组件的图像,并在希望图像出现/消失时打开和关闭该组件。您可以使用类似于以下内容的代码执行此操作: using UnityEngine; using System.Collections; using UnityEngine.UI; // Req

我正在开发2D Unity游戏,我想知道当玩家与物体碰撞时,如何在我的物品清单上显示图像。例如,如果玩家与鼓槌碰撞,鼓槌图像将出现在库存菜单上

致以最良好的祝愿

Yacine TAZDAIT.

方法1 有许多不同的方式来显示图像。例如,您可以拥有一个包含图像组件的图像,并在希望图像出现/消失时打开和关闭该组件。您可以使用类似于以下内容的代码执行此操作:

using UnityEngine;
using System.Collections;
using UnityEngine.UI; // Required when Using UI elements.

public class Example : MonoBehaviour
{
    public Image drumstick;

    public void Start()
    {
        toggleDrumstick(); // This will toggle the drumstick. For example, if the drumstick is not being shown at the time, the drumstick will show on the screen. The opposite is true.
    }

    public void toggleDrumstick() {
        drumstick.enabled = !drumstick.enabled;
    }
}
方法#2 上面的代码是一个很好的解决方案,但是有一种更模块化的方法来实现

using UnityEngine;
using System.Collections;
using UnityEngine.UI; // Required when Using UI elements.

public class Drumstick : MonoBehaviour
{  
    public static bool enabled = this.image.enabled;
}
我推荐上述方法。这是因为每个脚本现在都可以访问drumstick的状态。例如,您的播放器脚本可以做到这一点

using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour
{  
    void doSomething () {
        Drumstick.enabled = true; // make the image appear.
    }
}
要使这些方法中的任何一种起作用,请确保drumstick使用图像组件

编辑: 为了进一步回答您的问题,这里有一种在代码中实现方法2的方法。在您的播放器脚本中,您可以使用
OnCollisionCenter
和上面的方法来显示鼓槌

using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour
{  
    void OnCollisionEnter (Collision collision)
    {
        if (collision.gameObject.tag == "drumstick") Drumstick.enabled = false;
    }
}

为此,请确保鼓槌上有标签
“鼓槌”

。请进行参观并阅读指南。现在看来,你的问题太宽泛了。还可以尝试事先研究和咨询教程