Files
JJBB/Assets/Project/Script/ResNetwork/ResNetwork.cs

89 lines
2.3 KiB
C#
Raw Normal View History

2024-08-23 15:49:34 +08:00
using System.Collections;
using System.Text;
using Module.Log;
using UnityEngine;
using UnityEngine.Networking;
public class ResNetwork : MonoBehaviour
{
public byte[] CaptureScreen()
{
var width = Screen.width;
var height = Screen.height;
var tex = new Texture2D(width, height, TextureFormat.RGB24, false);
// Read screen contents into the texture
tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
tex.Apply();
// Encode texture into PNG
var bytes = tex.EncodeToPNG();
Destroy(tex);
return bytes;
}
public void UploadFile(byte[] fileContent, string fileName)
{
StartCoroutine(Upload(fileContent, fileName));
}
private IEnumerator Upload(byte[] fileContent, string fileName)
{
//UnityWebRequest www = UnityWebRequest.Put(MySpaceUrl + "/" + fileName, fileContent);
//yield return www.Send();
var myData = Encoding.UTF8.GetBytes("This is some test data");
var www = UnityWebRequest.Put(MySpaceUrl, myData);
yield return www.Send();
LogModule.DebugLog("Upload file name" + MySpaceUrl + "/" + fileName);
if (www.isNetworkError)
LogModule.DebugLog(www.error);
else
LogModule.DebugLog("Upload complete!");
}
public void DownLoadImage(string fileName)
{
StartCoroutine(GetTexture(fileName));
}
private IEnumerator GetTexture(string fileName)
{
var www = UnityWebRequestTexture.GetTexture(fileName);
yield return www.Send();
if (www.isNetworkError)
{
LogModule.DebugLog(www.error);
}
else
{
Texture myTexture = ((DownloadHandlerTexture) www.downloadHandler).texture;
}
}
#region
public static ResNetwork Instance { get; private set; }
private static string _MySpaceUrl = "";
public static string MySpaceUrl
{
get
{
if (string.IsNullOrEmpty(_MySpaceUrl) && Singleton<ObjManager>.GetInstance().MainPlayer != null)
_MySpaceUrl = NetworkConfig.WebBlogUrl + "/" +
GameManager.gameManager.PlayerDataPool.MainPlayerBaseAttr.Guid;
return _MySpaceUrl;
}
}
private void Start()
{
Instance = this;
}
#endregion
}