c# - How to use WWW class to change the Sprite? -
i'm struggling change sprite image use www class in unity.
i have picture of profile png on website i'd load in game. i'd change sprite of sprite renderer attached game object new sprite of personal profile.
i think ienumerator can't used event, rearranged below. there no error didn't work.
please let me know how this.
using unityengine; using system.collections; using system.io; using uxlib.user; using uxlib.connect; public class userimage : monobehaviour { uxandroidmanager androidmanager; private spriterenderer spriterenderer; public material mat; void start(){ spriterenderer = getcomponent<spriterenderer> (); androidmanager.startgallery (login.userid); } void awake () { gameobject go = gameobject.find ("androidmanager"); androidmanager = go.getcomponent<uxandroidmanager> (); androidmanager.initandroid (); androidmanager.onand_profileimagechanged += onprofileimagechaned; } void ondestroy() { androidmanager.onand_profileimagechanged -= onprofileimagechaned; } void onprofileimagechaned(string filepath) { uxplayercontroller player = uxplayercontroller.instance; player.changeprofileimage(filepath); string logstring = player.getlastreceiveddata(); profileimage (filepath); } ienumerator profileimage(string filepath){ www www = new www (filepath); yield return www; spriterenderer renderer = gameobject.getcomponent<spriterenderer>(); sprite sprite = new sprite(); sprite = sprite.create(www.texture, new rect(0, 0, 170, 170),new vector2(0, 0),100.0f); renderer.sprite = sprite; renderer.material = mat; } }
call profileimage
in coroutine.
a coroutine function can suspend execution (yield) until given given yieldinstruction finishes.
so, change call in onprofileimagechaned
from:
profileimage (filepath);
to:
startcoroutine(profileimage(filepath));
i recommend take @ unity docs coroutines: http://docs.unity3d.com/scriptreference/coroutine.html http://docs.unity3d.com/manual/coroutines.html