So this problem is somewhat peculiar. Here is, in pseudo code, how I'm changing the avatar for AvatarService:
1. Delete original profile picture with username as 'username,' and username as 'avatar name'.
2. Create new avatar with the same username and same avatar name.
3. Download new avatar with username and avatar name only to receive the previously deleted avatar.
Here is the code for the avatar picture change:
public static IEnumerator ChangeProfilePic(Image TheImage)
{
//Delete the avatar.
CreateUserCB CUCB = new CreateUserCB();
UserC UC = Singleton<UserC>.Instance.GetComponent<UserC>();
UserC.avatarService.DeleteAvatarByName(UC.Username, UC.Username, CUCB);
while (!CUCB.Success) { yield return 0; }
//Create the new avatar.
string FileName = "ProfilePic.png";
ImageConversion.TextureToPNGToDisk(FileName, (Texture2D)TheImage.mainTexture);
string AvatarPath = Application.persistentDataPath + "/../" + FileName;
//Upload the new avatar.
UserC.avatarService.CreateAvatar(UC.Username, UC.Username, AvatarPath, "None", CUCB);
while (!CUCB.Success && !CUCB.Failure) { yield return 0; }
}
Here is the callback for these methods:
public class CreateUserCB : App42CallBack
{
public bool Success;
public bool Failure;
//CreateNewUser CNU = Singleton<CreateNewUser>.Instance.GetComponent<CreateNewUser>();
public void OnSuccess(object response) { Success = true; }
public void OnException(Exception e)
{
Failure = true;
Debug.Log("Avatar: " + e);
}
public void Reset ()
{
Success = false;
Failure = false;
}
}
Here is the method I use to download the new avatar:
public static IEnumerator GetImage (Material TheMaterial, string URL)
{
//Download the image.
WWW www = new WWW(URL);
yield return www;
//Create a new texture.
TheMaterial.mainTexture = new Texture2D(www.texture.width, www.texture.height, TextureFormat.RGBA32, false);
//Load the texture into the shit.
www.LoadImageIntoTexture((Texture2D)TheMaterial.mainTexture);
yield return 0;
}
And here is the main function where I call the above method to retrieve the avatar:
IEnumerator SetUC ()
{
//Get all user info.
GetUserInfoCB GUICB = new GetUserInfoCB();
//Get all the avatar stuff.
AvatarJunkCB AJCB = new AvatarJunkCB();
//Set global username.
UC.Username = PlayerPrefs.GetString("CurrentUser");
//Set logged in user.
App42API.SetLoggedInUser(UC.Username);
//Get user information.
userService.GetUser("doug", GUICB);
while (!GUICB.Success && !GUICB.Failure) { yield return 0; }
//Auth the user.
GUICB.Reset();
userService.Authenticate(UC.Username, Encryption.Decrypt(PlayerPrefs.GetString("Info")), GUICB);
Debug.Log(Encryption.Decrypt(PlayerPrefs.GetString("Info")));
while (!GUICB.Success && !GUICB.Failure) { yield return 0; }
//Set email.
UC.Email = GUICB.user.GetEmail();
//Set session ID.
userService.SetSessionId(GUICB.user.GetSessionId());
//Get avatar by name.
avatarService.GetAvatarByName(UC.Username, UC.Username, AJCB);
//avatarService.ChangeCurrentAvatar(UC.Username, UC.Username, AJCB);
//avatarService.GetCurrentAvatar(UC.Username, AJCB);
while (!AJCB.Success && !AJCB.Failure) { yield return 0; }
//Get the avatar URL.
UC.ProfilePicURL = AJCB.avatar.GetUrl();
//Get the texture and wait for it to complete.
yield return StartCoroutine(DownloadImage.GetImage(UC.ProfileMaterial, UC.ProfilePicURL));
//Get the sprite from the new texture.
UC.ProfilePic = Sprite.Create((Texture2D)UC.ProfileMaterial.mainTexture, new Rect(0, 0, UC.ProfilePicTex.width, UC.ProfilePicTex.height), new Vector2(0.5f, 0.5f), 100);
//Set the material from the texture.
UC.ProfilePicTex = (Texture2D)UC.ProfileMaterial.mainTexture;
//Set push stuff.
App42API.SetLoggedInUser(UC.Username);
App42Push.setApp42PushListener(this);
App42Push.registerForPush("654035403540");
//All services must be set in UserC as static variables.
UserC.serviceAPI = serviceAPI;
UserC.userService = userService;
UserC.avatarService = avatarService;
UserC.storageService = storageService;
UserC.pushNotificationService = pushNotificationService;
//Disable the loaders.
if (AuthenticateUserScript.gameObject.activeSelf) AuthenticateUserScript.Downloaded = true;
if (AuthenticateUserScript.gameObject.activeSelf) CreatingUser.Downloaded = true;
//Set keys because, you know, problems.
UserC.SetKeys();
}
I've also manually input a url in place of 'UC.ProfilePicURL = AJCB.avatar.GetUrl();' which was successful, so the problem is not that it's using old textures from previous materials, the problem lies somewhere within my inability to update the avatar correctly.
I'd like to stress that I delete one avatar and upload a new one by the same username and avatar name.
NOTE: Updating the avatar (deleting and creating another with the same parameters) via the console allows the image to be updated.
I would also like to note that the avatar changes in AppHQ, but still downloads the previously used avatar for some reason.
NOTE 1: Now while deleting manually in AppHQ, with the same parameters, the previous image appears instead of the new one, however, a new avatar with different parameters will show a new pic. The problem is with deleting and recreating a new avatar with the same parameters as the previous.
It seems to take some time to update though, so I'll test that out and watch a movie while I wait or something.