該程式碼是套用在 Image 物件上,如下圖所示:

Unity (64bit) - Test.unity - New Unity Project 22 - PC, Mac & Linux Standalone DX11  

程式碼:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class Test : MonoBehaviour {

    public Texture2D myImg; // 從外部拖拉自己喜歡的圖片進來

    void Awake () {
        Sprite s = Sprite.Create (myImg, new Rect (0, 0, myImg.width, myImg.height), Vector2.zero);
        GetComponent<Image> ().sprite = s;
    }
}

執行前:

Unity (64bit) - Test.unity - New Unity Project 22 - PC, Mac & Linux Standalone DX11_2  

執行後:

Unity (64bit) - Test.unity - New Unity Project 22 - PC, Mac & Linux Standalone DX11_3  




arrow
arrow
    全站熱搜

    黃彥霖 發表在 痞客邦 留言(0) 人氣()