我正在使用Unity加载保存在设备中的facebook个人资料图片。代码在Android中运行良好,但不适用于IOS。多年来我一直在遭遇这个问题。所以这是我的代码,请帮助我。
using UnityEngine;
using System.Collections;
using System.IO;
using UnityEngine.UI;
public class UserProfilePicture : MonoBehaviour {
public RawImage profilePicture, testProfile;
private Texture2D texture,text;
void FixedUpdate(){
if (FbSetup.userInfoLoaded) {
if(SPFacebook.instance.userInfo.GetProfileImage(FacebookProfileImageSize.square) != null) {
text = SPFacebook.instance.userInfo.GetProfileImage(FacebookProfileImageSize.square);
}
}
testProfile.texture = text as Texture;
byte[] textureByte = text.EncodeToPNG ();
File.WriteAllBytes (Path.Combine(Application.persistentDataPath,"profilePicture.png"),textureByte);
Debug.Log("Save path: " + Application.persistentDataPath + Path.DirectorySeparatorChar + "profilePicture.png");
if(File.Exists(Application.persistentDataPath + Path.DirectorySeparatorChar + "profilePicture.png")){
Debug.Log("Texture saved");
}
}
void Update(){
StartCoroutine (Control ());
profilePicture.texture = texture as Texture;
}
IEnumerator Control(){
WWW pictureUrl;
pictureUrl = new WWW(System.Uri.EscapeUriString("file://" + Path.Combine(Application.persistentDataPath,"profilePicture.png")));
yield return new WaitForEndOfFrame();
texture = pictureUrl.texture;
Debug.Log("Loaded path: " + Path.Combine(Application.persistentDataPath,"profilePicture.png"));
}
void OnEnable(){
if (GLobal.playAsGuest) {
this.gameObject.SetActive(false);
}
else{
this.gameObject.SetActive(true);
}
}
感谢您的帮助,谢谢!
答案 0 :(得分:1)
事实证明,问题在于以下一行。
yield return new WaitForEndOfFrame();
显然,一帧不足以从磁盘加载文件。你很幸运,它足够你的Android设备。所以你应该将那一行改为
yield return pictureUrl;
所以它会等到它完成加载。