在Unity中暂时隐藏图像的视野

Hide vision of image temporarily in Unity

我会在受到攻击时启用血屏然后禁用它两秒钟。

我尝试的第一种方法是使用 SetActive (true/flase) .

它可以工作一次,但再也不能工作了。

我发现 google 的问题,似乎该函数将禁用该组件的所有内容。

所以我找到了第二种方法,即使用 Image Class 来做到这一点。

但我不确定我使用的方式是否正确。

错误消息:Object reference not set to an instance of an object

First Way (working once)

public class GameControllerScript : MonoBehaviour { 

public GameObject bloodyScreen; 

void Start () { 
    bloodyScreen.gameObject.SetActive (false);  // disable at first
} 

void Update () { 

} 

public void zombieAttack(bool zombieIsThere ){ 

    bloodyScreen.gameObject.SetActive (true); 
    StartCoroutine(WaitTwoSeconds()); 
} 

IEnumerator WaitTwoSeconds(){ 

    yield return new WaitForSeconds (2f);
    bloodyScreen.gameObject.SetActive (false); 
} }   

Second Way (not working)

public class GameControllerScript : MonoBehaviour { 

    public Image image;

    void Start () { 
        GameObject go = GameObject.Find("Canvas");
        if (!go)
            return;
        image = go.GetComponent<Image>();
        image.enable = false;

    } 

    void Update () { 

    } 

    public void zombieAttack(bool zombieIsThere ){ 

        image.enabled = true;
        StartCoroutine(WaitTwoSeconds()); 
    } 

    IEnumerator WaitTwoSeconds(){ 

        yield return new WaitForSeconds (2f);
        image.enabled = false;
    } } 

对不起我的英语,如果有什么不清楚的话。

What my object likes https://i.stack.imgur.com/DChuS.png

BLOODY SCREEN 有很多种方法。

1.)

第 1 步

制作一个纹理数组,然后将所有纹理放在那里

第 2 步

在您的 OnTriggerEnterOnCollisionEnter 或您想要的任何功能上并尝试实现此代码

Texture[] arrayOfTexture = new Texture

 foreach(Texture textures in arrayOfTexture){
    //set the alpha here from 1-0/0-1
 }

2.)

您也可以在 Update() 函数中执行此操作并将其放在计时器上 您可以像

float timer = 10f;

void Update(){
   timer -= time.DeltaTime;
   if(timer < 1){
      //reset timer here

   }else{
      //do the alpha thing here
   }
}

 void ResetTimer(){
    timer = 10f;
 }