unity工具
提示:以下是本篇文章正文内容,下面案例可供参考
代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class LoadSprite : MonoBehaviour
{
public Image image;
private string url="自己图片的地址";
private void Start()
{
StartCoroutine(PostSprite(url));
}
public void LoadSpriteByte(byte[] path)
{
image.sprite = ChangeToSprite(ByteToTex2d(path));
}
//根据字节流转换成图片
public static Texture2D ByteToTex2d(byte[] bytes)
{
int w = 500;
int h = 500;
Texture2D tex = new Texture2D(w, h);
tex.LoadImage(bytes);
return tex;
}
//转换为Image
private Sprite ChangeToSprite(Texture2D tex)
{
Sprite sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
return sprite;
}
public IEnumerator PostSprite(string url)
{
using (UnityWebRequest webRequest = UnityWebRequestTexture.GetTexture(url))
{
yield return webRequest.SendWebRequest();
// 检查是否有错误
if (webRequest.result != UnityWebRequest.Result.Success)
{
Debug.LogError("Error: " + webRequest.error);
Debug.Log(webRequest.responseCode);
image.sprite = null;
}
else
{
// 请求成功,处理响应数据
LoadSpriteByte(webRequest.downloadHandler.data);
webRequest.Dispose();
}
}
}
}
IEnumerator DownloadImage(string url)
{
WWW www = new WWW(url);
yield return www;
Texture2D tex2d = www.texture;
Sprite m_sprite = Sprite.Create(tex2d, new Rect(0, 0, tex2d.width, tex2d.height), new Vector2(0, 0));
image.sprite=m_sprite;
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
using System.Net.Http;
using System.Threading.Tasks;
public static Texture2D ByteToTex2d(byte[] bytes)
{
int w = 500;
int h = 500;
Texture2D tex = new Texture2D(w, h);
tex.LoadImage(bytes);
return tex;
}
//转换为Image
private Sprite ChangeToSprite(Texture2D tex)
{
Sprite sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
return sprite;
}
public async Task PostSpriteAsync(string url)
{
image.sprite = ChangeToSprite(ByteToTex2d(await GetImageAsBase64String(url)));
}
public static async Task<byte[]> GetImageAsBase64String(string url)
{
using (var httpClient = new HttpClient())
{
// 发送 HTTP 请求获取图片的字节
byte[] imageBytes = await httpClient.GetByteArrayAsync(url);
// 将字节转换为 Base64 字符串
return (imageBytes);
}
}
提示:以下是本篇文章正文内容,下面案例可供参考
代码如下(示例):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class ABLoad : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
///
/// 加载ab包
///
/// 路径
/// 加载到哪个父物体下面
///
public IEnumerator FromWebRequest(string path, Transform parents)
{
UnityWebRequest webRequest = UnityWebRequestAssetBundle.GetAssetBundle(path);
yield return webRequest.SendWebRequest();
AssetBundle assetBundle = DownloadHandlerAssetBundle.GetContent(webRequest);
object[] objects = assetBundle.LoadAllAssets();
for (int i = 0; i < objects.Length; i++)
{
GameObject obj = Instantiate((GameObject)objects[i], parents);
}
//yield return new WaitForSeconds (3f);
yield return null;
//卸载资源
assetBundle.Unload(false);
}
}
代码如下
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class PostLoad : MonoBehaviour
{
private string url;
public void GetPostDatas(string ipp, string deviceId, string idd)
{ //deviceId
WWWForm formData = new WWWForm();
formData.AddField(deviceId, idd);
StartCoroutine(PostRequest(url + ipp, formData, data=> {
//后面的逻辑写到这个地方 data就是数据
}));
}
///
/// 获取设备的状态的 Root
///
///
///
///
///
IEnumerator PostRequest(string url, WWWForm formData , Action<string> callback)
{
using (UnityWebRequest webRequest = UnityWebRequest.Post(url, formData))
{
// 发送请求
yield return webRequest.SendWebRequest();
// 检查是否有错误
if (webRequest.result != UnityWebRequest.Result.Success)
{
Debug.LogError("Error: " + webRequest.error);
Debug.Log(webRequest.responseCode);
}
else
{
// 请求成功,处理响应数据
Debug.Log("Response: " + webRequest.downloadHandler.text);
callback?.Invoke(webRequest.downloadHandler.text);
webRequest.Dispose();
}
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class GetLoad : MonoBehaviour
{
private string urll;
private string token; //有就加,没有就不加
// Start is called before the first frame update
void Start()
{
StartCoroutine(PostRequest_GET("code", "floor", 1, data => {
//后面的逻辑写到这个地方 data就是数据
}));
}
public IEnumerator PostRequest_GET(string code, string floor, int page, Action<string> callback)
{
string url = string.Format(urll + "/IndoorAntenna/antennaList?code={0}&floor={1}&page={2}&pageSize=10&Authorization=Bearer {3}", code, floor, page, token);
using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
{
// 发送请求
yield return webRequest.SendWebRequest();
// 检查是否有错误
if (webRequest.result != UnityWebRequest.Result.Success)
{
Debug.LogError("Error: " + webRequest.error);
Debug.Log(webRequest.responseCode);
}
else
{
// 请求成功,处理响应数据
Debug.Log(webRequest.responseCode);
Debug.Log("Response: " + webRequest.downloadHandler.text);
callback?.Invoke(webRequest.downloadHandler.text);
webRequest.Dispose();
//GetJsonData_HuiYiShi(root.data);
}
}
}
}
总结一下,希望对你有需要,也刚好对你有用
如果觉得本篇文章有用别忘了点个关注,关注不迷路,持续分享更多Unity干货文章。