Fel*_*lva 3 unity-game-engine unityscript
我正在尝试使用Unity 4.6中的新UI对象来创建排名屏幕,我在其中下载有关用户的信息并显示它们.在这些信息中,我有一个指向其图片的URL.
在UI工具之前,我使用GUITexture并使用www.LoadImageIntoTexture方法,但似乎没有类似的UI图像解决方案.使用带有Rect变换的GUITexture似乎不起作用.
我应该手动下载图像并将其加载到图像中,还是使用类似于使用WWW类的解决方案?
新的Image组件需要一个sprite来渲染.应该做的是使用WWW类下载PNG纹理,然后创建一个精灵,最后将其分配给组件.
这是一些示例代码.
有关实际结果,请参阅附带的图片.
//This is the method you call if you use the "OnClick" event from a button click.
//We cannot call the Coroutine function below, because it breaks the rule for
//the event system. i.e. Must be a return type of void.
public void LoadImage () {
//Call the actual loading method
StartCoroutine(RealLoadImage());
}
//This is where the actual code is executed
private IEnumerator RealLoadImage () {
//A URL where the image is stored
string url = "http://www.axsu3d.com/DLs/AxSWP8Plugin/TileLogo.png";
//Call the WWW class constructor
WWW imageURLWWW = new WWW(url);
//Wait for the download
yield return imageURLWWW;
//Simple check to see if there's indeed a texture available
if(imageURLWWW.texture != null) {
//Construct a new Sprite
Sprite sprite = new Sprite();
//Create a new sprite using the Texture2D from the url.
//Note that the 400 parameter is the width and height.
//Adjust accordingly
sprite = Sprite.Create(imageURLWWW.texture, new Rect(0, 0, 400, 400), Vector2.zero);
//Assign the sprite to the Image Component
GetComponent<UnityEngine.UI.Image>().sprite = sprite;
}
yield return null;
}
Run Code Online (Sandbox Code Playgroud)

| 归档时间: |
|
| 查看次数: |
8355 次 |
| 最近记录: |