Forum rules - please read before posting.

Displaying a character speaking portraitIcon in a image

edited March 2019 in Technical Q&A

Hello, I need to get the portraitIcon (both animated and not animated) of the characters and display it in an UI element like a picture showing the portraitIcon (but not in a subtitle menu). I tried using an UI Image but I don't know how to use the script to call the portraitIcon. I did:

UI.Image characterPortrait = KickStarter.player.portraitIcon.texture;

But it gives an error:

Cannot implicitly convert type 'UnityEngine.Texture' to 'UnityEngine.UI.Image'

What UI object should I use to hold the portraitIcon or what script should I be using?

Thanks.

Comments

  • Use a RawImage component - it allows you to set the graphic from a texture. If you use Image, you'll have to convert it into a sprite:

    https://docs.unity3d.com/ScriptReference/UI.RawImage.html

    public UI.RawImage characterPortrait; // This is a public field, not inside your functions, so you can assign it
    
    public void MyFunction()
    {
        characterPortrait.texture = KickStarter.player.portraitIcon.texture;
    }
    
  • edited March 2019

    Thanks, it works for a static characterPortrait. But when I make the characterPortrait animated, the RawImage still only show the static characterPortrait. Should I use other scripts for the animated ones?

  • See the Scripting Guide's entry on the CursorIconBase class, which is what portraitIcon's type is.

    public void Update()
    {
        characterPortrait.texture = KickStarter.player.portraitIcon.GetAnimatedTexture ();
    }
    
  • edited March 2019

    I tried but no luck. When doing

    Debug.Log(KickStarter.player.portraitIcon.GetAnimatedRect());

    it shows in order:
    (x:0.00, y:-1.00, width:-1.00, height:-1.00)
    (x:-1.00, y:-1.00, width:-1.00, height:-1.00)
    (x:0.00, y:0.00, width:-1.00, height:-1.00)
    (x:-1.00, y:0.00, width:-1.00, height:-1.00)

    so I guess it is trying to animate. However,

    Debug.Log(KickStarter.player.portraitIcon.GetAnimatedTexture());

    always shows the texture name. And

    characterPortrait.texture = KickStarter.player.portraitIcon.GetAnimatedTexture ();

    always show the same full texture.

    I did put it in Update(), tried checking and unchecking Always Animate?, but still no luck.

    The characterPortrait UV Rect also doesn't change at all.

    What may I do?

  • Try inserting this:

    private void Start ()
    {
        KickStarter.player.portraitIcon.Reset ();
    }
    
  • edited March 2019

    Tried putting the script in Start() and OnEnable() but still no luck..

    As last resort I can use scripts to control the RawImage UV Rect according to the portraitIcon parameters. But it would be better if I can use AC API. Actually is the output from the above Debug.Log(KickStarter.player.portraitIcon.GetAnimatedRect()); correct?

  • No - the width is incorrect. The graphic needs to be reset to properly calculate animated Rect vaues.

    Instead of calling Reset() in Start, as above, try pasting the following into the top of the CursorIcon script's GetAnimatedRect function (around line 592):

    if (frameWidth < 0f)
    {
        Reset ();
    }
    
  • edited March 2019

    I found using Reset() in CursorIcon.cs, and using KickStarter.player.portraitIcon.Reset (); in OnEnable() or Start() all give gives correct GetAnimatedRect() values. But to get the characterPortrait texture to animate, I need these scripts:

    characterPortrait.texture = KickStarter.player.portraitIcon.GetAnimatedTexture ();

    animationRect = KickStarter.player.portraitIcon.GetAnimatedRect();

    characterPortrait.uvRect = new Rect(animationRect.x, animationRect.y, animationRect.width, animationRect.height);

    which I think is a simple enough solution. Thanks.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.