2019년 1월 12일 토요일

Load Texture2D in c#


www methods changed at each Unity version.
I used from Unity 5.6.3, so summary with this function.


IEnumerator LoadTexture (string filePath) {
    string url;
    if (filePath.StartsWith ("http")) {
        // from WEB
        url = filePath;
    } else {
        // from local
#if UNITY_ANDROID
        url = "file://" + Uri.EscapeUriString (filePath);
#elif UNITY_IOS
        url = "file://" + filePath;
#endif
    }

#if UNITY_2017_2_OR_NEWER
    using (UnityWebRequest www = UnityWebRequestTexture.GetTexture (url)) {
          yield return www.SendWebRequest ();
         if (www.isNetworkError) {
             Debug.Log ("www.error: " + www.error);
         } else {
              Texture2D loadedTexture = DownloadHandlerTexture.GetContent (www);
#elif UNITY_2017_1_OR_NEWER
    using (UnityWebRequest www = UnityWebRequestTexture.GetTexture (url)) {
        yield return www.Send ();
        if (www.isNetworkError) {
            Debug.Log ("www.error: " + www.error);
        } else {
             Texture2D loadedTexture = DownloadHandlerTexture.GetContent (www);
#elif UNITY_5_6_OR_NEWER
    using (WWW www = new WWW (url)) {
         yield return www;
        if (www.error != null) {
            Debug.Log ("www.error: " + www.error);
        } else {
            Texture2D loadedTexture = www.texture;
#endif
            // Do action as you want.
        }
    }
}

댓글 없음:

댓글 쓰기