代码之家  ›  专栏  ›  技术社区  ›  Moonixe

如何在SetActive(false)之后再次启动IEnumerator;

  •  0
  • Moonixe  · 技术社区  · 3 年前

    我正试图在我的游戏中创建一个图片功能,我希望相框在几秒钟后消失。之后,当我再次尝试拍摄照片时,它不起作用。我是C#的新手,我很想得到一些帮助^^;

    按下空格后,会显示一张图片,几秒钟后它就会消失。如果再次按空格键,操作将重复。该操作不会重复。

    这是代码:

    using System.Collections;
    using UnityEngine;
    using UnityEngine.UI;
    using UnityEngine.Animations;
    
    
    public class PhotoCapture : MonoBehaviour
    {
        
        [Header("Photo Taker")]
        [SerializeField] private Image photoDisplayArea;
        [SerializeField] private GameObject photoFrame;
    
        [Header("Flash Effect")]
        [SerializeField] private GameObject cameraFlash;
        [SerializeField] private float flashTime;
    
        [Header("Photo Fader Effect")]
        [SerializeField] private Animator fadingAnimation;
    
        private Texture2D screenCapture;
        private bool viewingPhoto;
    
        private void Start()
        {
            screenCapture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
        }
        private void Update()
        {
            if (Input.GetKeyDown(KeyCode.Space))
            {
                if (!viewingPhoto)
                {
                    StartCoroutine(CapturePhoto());
    
    
                }
    
            }
        }
    
        IEnumerator CapturePhoto()
        {
           
                //Camera UI set false
                viewingPhoto = true;
    
                yield return new WaitForEndOfFrame();
    
                Rect regionToRead = new Rect(0, 0, Screen.width, Screen.height);
    
                screenCapture.ReadPixels(regionToRead, 0, 0, false);
                screenCapture.Apply();
                ShowPhoto();
                
    
    
        }
    
        private void ShowPhoto()
        {
            //doflash
            Sprite photoSprite = Sprite.Create(screenCapture, new Rect(0.0f, 0.0f, screenCapture.width, screenCapture.height), new Vector2(0.5f, 0.5f), 100.0f);
            photoDisplayArea.sprite = photoSprite;
    
            photoFrame.SetActive(true);
            fadingAnimation.Play("photoFade");
            StartCoroutine(CameraFlashEffect());
            yield return new WaitForSeconds(2);
            photoFrame.SetActive(false);
    
            
        }
    
        
    
        IEnumerator CameraFlashEffect()
        {
            //play some audio
            cameraFlash.SetActive(true);
            yield return new WaitForSeconds(flashTime);
            cameraFlash.SetActive(false);
        }
    
        void RemovePhoto()
        {
    
            viewingPhoto = false;
            photoFrame.SetActive(false);
            //cameraUI true 
        }
    }
    
    
    0 回复  |  直到 3 年前
        1
  •  1
  •   Michael Urvan    3 年前

    你从来没有把viewingPhoto设置为false,这样你就可以再做一次,因为我看不到从任何地方调用RemovePhoto。