유니티의 에디터에서 파일을 선택해서 테스트 할때는 'Open File Panel'를 이용하면 됩니다.
(참고 : unity Documentation - EditorUtility.OpenFilePanel)
이 포스팅은 'Open File Panel'을 사용하는 하는 방법을 설명합니다.
(런타임에서는 사용할 수 없음.)
'using UnityEditor;'를 추가한 후 'EditorUtility.OpenFilePanel'를 호출 하면 됩니다.
EditorUtility.OpenFilePanel([다얄로그에 표시할 제목], [처음 표시될 디렉터리], [허용할 확장자]);
이제 직접 사용해 봅시다.
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Text;
public class claOpenFilePanel : MonoBehaviour
{
public string filePath = "";
public Texture2D texture;
public GameObject m_goCube;
// Use this for initialization
void Start ()
{
m_goCube = GameObject.Find("Cube");
}
// Update is called once per frame
void Update ()
{
}
void OnGUI()
{
if (GUI.Button(new Rect(210, 260, 100, 40), "Select Texture"))
{
#if UNITY_EDITOR
filePath = EditorUtility.OpenFilePanel("Overwrite with png"
, Application.streamingAssetsPath
, "png");
#endif
if (filePath.Length != 0)
{
WWW www = new WWW("file://" + filePath);
texture = new Texture2D(64, 64);
www.LoadImageIntoTexture(texture);
m_goCube.renderer.material.mainTexture = texture;
}
}
}
public void OpenFilePanel()
{
}
}
이 기능은 유니티 에디터에서만 사용할 수 있기 때문에 실행파일로 만들면 다음과 같은 에러가 납니다.
The type or namespace name `UnityEditor' could not be found. Are you missing a using directive or an assembly reference?
이 문제를 해결하려면 '디버깅 심볼'을 사용해서 코드를 피해가야 합니다.
#if UNITY_EDITOR
filePath = EditorUtility.OpenFilePanel("Overwrite with png"
, Application.streamingAssetsPath
, "png");
#endif
(참고 : Unity Answers - "UnityEditor" namespace not found...)
그런데 이상하게 '디버깅 심볼'을 써도 에러가 납니다-_-;;;;;;;
디버깅심볼이 동작하도록 뭔가 더 해야 하는걸까요?
문서를 봐도 별다른 내용이 없고 -_-;;;;;
(참고 : unity Documentation - Platform Dependent Compilation)