728x90
[Unity] ํจ์ ํธ์ถ์ ๋ฆ์ถฐ์ฃผ๋ Invoke
๋ฌธ์
using System.IO;
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class CameraCapture : MonoBehaviour
{
[SerializeField]
private Button TakePhotoButton;
[SerializeField]
private GameObject Canvas;
public int fileCounter;
public KeyCode screenshotKey;
private Camera Camera
{
get
{
if (!_camera)
{
// _camera = Camera.main;
_camera = Camera.main;
}
return _camera;
}
}
[SerializeField]
private Camera _camera;
void Awake()
{
TakePhotoButton.onClick.AddListener(TakePhoto);
}
private void TakePhoto()
{
Debug.Log("TakePhoto()" + fileCounter);
// Capture();
Canvas.SetActive(false);
Screenshot();
}
private void Screenshot()
{
Texture2D texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
texture.Apply();
byte[] bytes = texture.EncodeToPNG();
string name = "AR Camera" + System.DateTime.Now.ToString("yyyy-mm-dd_HH-mm-ss") +fileCounter + ".png";
NativeGallery.SaveImageToGallery(bytes, "AR Camera", name);
fileCounter++;
Destroy(texture);
Canvas.SetActive(true);
Debug.Log("Canvas True");
}
}
์ ์ฝ๋๋ฅผ ์คํํ๋ฉด
Button๋ค์ ์์์ ํด๋นํ๋ Canvas๊ฐ SetActive(false) ๋์ง ๋ชปํ๊ณ ๋ณด์ด๊ฒ ๋๋ค.
๋ฐ๋ผ์ ์๊ฐ์ง์ฐ ๊ธฐ๋ฅ ํจ์ Invoke๋ฅผ ์ด์ฉํ๋ค.
Invoke("method name", 2f) // (ํจ์๋ช
, sec)
using System.IO;
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class CameraCapture : MonoBehaviour
{
[SerializeField]
private Button TakePhotoButton;
[SerializeField]
private GameObject Canvas;
public int fileCounter;
public KeyCode screenshotKey;
private Camera Camera
{
get
{
if (!_camera)
{
// _camera = Camera.main;
_camera = Camera.main;
}
return _camera;
}
}
[SerializeField]
private Camera _camera;
void Awake()
{
TakePhotoButton.onClick.AddListener(TakePhoto);
}
private void TakePhoto()
{
Debug.Log("TakePhoto()" + fileCounter);
Canvas.SetActive(false);
Invoke("Screenshot", 1f);
// Screenshot();
}
private void setActiveTrue()
{
Canvas.SetActive(true);
}
private void Screenshot()
{
Texture2D texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
texture.Apply();
byte[] bytes = texture.EncodeToPNG();
string name = "AR Camera" + System.DateTime.Now.ToString("yyyy-mm-dd_HH-mm-ss") +fileCounter + ".png";
NativeGallery.SaveImageToGallery(bytes, "AR Camera", name);
fileCounter++;
Destroy(texture);
// Canvas.SetActive(true);
Invoke("setActiveTrue", 2f);
Debug.Log("Canvas True");
}
}
์์
- Canvas.SetActive(false); Ui๋ฅผ ํ๋ฉด์์ ์ง์ด๋ค
- Invoke("Screenshot", 1f); Screenshotํจ์๋ฅผ 1์ด ํ ํธ์ถ
- Invoke("setActiveTrue", 2f); setActiveTrueํจ์๋ฅผ 2์ดํ ํธ์ถ
'Error' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[apple watch] ์์ ๋๊ธฐํ ์คํจ ์ (0) | 2021.11.21 |
---|---|
[Git] Directory์ ์ ๊ทผ์ด ์๋ ๋ (0) | 2021.09.21 |
[Windows] cmd์์ clear ls ์ฌ์ฉ๋ฒ (0) | 2021.08.30 |
[Linux] omitting directory (0) | 2021.08.24 |
Tistory markdown ๊ธ์ ๊นจ์ง (0) | 2021.08.17 |