Search Unity

UnityEngine.UI.Image change Texture2D

Discussion in 'UGUI & TextMesh Pro' started by Graph, Aug 24, 2015.

  1. Graph

    Graph

    Joined:
    Jun 8, 2014
    Posts:
    154
    Hey I'm trying to assign a Texture2D to a UI Image control but can't find a proper way -_-

    Code (CSharp):
    1.       var pix = sourceTex.GetPixels(width * panel.X, height * panel.Y, width, height);
    2.       Texture2D destTex = new Texture2D(width, height);
    3.       destTex.SetPixels(pix);
    4.       destTex.Apply();
    5.  
    6.       // panel.Image is a UnityEngine.UI.Image
    7.       panel.Image.material.mainTexture = destTex;
    halp?
     
  2. Graph

    Graph

    Joined:
    Jun 8, 2014
    Posts:
    154
    in case anyone is looking for a solution:
    Code (CSharp):
    1. panel.Image.sprite = Sprite.Create(destTex, new Rect(0, 0, width, height), Vector2.zero);