Avatar Does Not Update After Change - Unity 3D

0 votes

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.

asked Dec 30, 2015 in App42 Cloud API-BaaS by null (23 points)
edited Dec 31, 2015 by null

1 Answer

0 votes
I'm going to answer my own question here.  Here is how I was able to update the user avatar and then retrieve it.

1. Get all avatars created under username by using avatarService.GetAllAvatars().

2. Once you retrieve all avatars in an IList, you'll be able to get the names of all avatars.

3.  Before deleting these avatars, you want to add the new avatar WITH A UNIQUE AVATAR NAME (I used Random.Range(100000000, 999999999).ToString()) with avatarService.CreateAvatar().

4.  Now that the new avatar has been added, you can delete each avatar in the IList from step 2 with avatarService.DeleteAvatarByName() where you use the names from the IList in a 'for' loop.

5.  When you want to load the most recent avatar, you can use GetAllAvatars() and retrieve the name of the last avatar in the list.

There you have it.  Problem solved.

Note:  If you just delete the avatar and create a new one with the same avatar name, username, etc., it will just show as the previous avatar.  Why that is, I have no idea.
answered Dec 31, 2015 by null (23 points)
Hello developer,


Apologies for getting late back to you. We have recorded this in our list, once it will
resolve will let you know for the same.

Regards,
Himanshu Sharma
Thanks :) :)
Hi. Any updates on this?
We really need this feature.
Thanks.
Hi Juan,

Could you please check the issue now at your end and let us know if you still face this issue. It will help us to provide better support to you.

Regards
Himanshu sharma
Hi.
thanks you so much, the issues was resolved.
I appreciate your help.
Download Widgets
Welcome to ShepHertz Product line forum, where you can ask questions and receive answers from the community. You can also reach out to us on support@shephertz.com
...