C-3*_*3PO 0 c# http unity-game-engine
我想System.Environment.UserName在打开和关闭 unity 时以“状态”发布到数据库!不是在播放场景时。打开 unity 时,我想更新“ON”状态以及关闭“OFF”状态或数据库中的 1 和 0 时。
尽管你的问题很广泛......
就在这里!
您可以UnityWebRequest以通常不会使用的方式使用:没有协程。在打开和关闭编辑器的过程中,如果 Web 请求期间有短暂的延迟,那可能还不错,因此我们可以在这个特定用例中使用简单的阻塞调用。
所以只需等待它在通常的while循环中完成即可。我刚刚UnityWebRequest.Get在 Google 上使用简单到一些随机图像创建了一个示例,但您可以轻松地将其替换为任何更复杂的UnityWebRequest.Post.
只需通过以下方式启动打开请求,InitializeOnLoadMethod并注册一个回调EditorApplication.wantsToQuit,以便在尝试关闭 Unity 时调用关闭请求。这样,编辑器只能在EditorApplication.wantsToQuit返回的回调时关闭true。
确保将此文件放入名为Editor.
using System.IO;
using UnityEditor;
using UnityEngine;
using UnityEngine.Networking;
public static class Example
{
// InitializeOnLoadMethod causes this method to be called when the editor is opened
[InitializeOnLoadMethod]
private static void Initialize()
{
#region Required Part
// register callback
// The editor will only close if OnQuitEditor returns true
EditorApplication.wantsToQuit += OnQuitEditor;
// I'll just do a random Get Request but you can do any request here
var request = UnityWebRequest.Get("https://upload.wikimedia.org/wikipedia/commons/thumb/8/84/Example.svg/2000px-Example.svg.png");
request.SendWebRequest();
while (!request.isDone && !request.isHttpError && !request.isNetworkError)
{
// just wait
}
if(request.isHttpError || request.isNetworkError || !string.IsNullOrWhiteSpace(request.error))
{
Debug.LogError("Couldn't finish opening request!");
return;
}
#endregion Required Part
#region DEMO Part
// Just to show here that it worked create a file called "Open"
var filePath = Path.Combine(Application.streamingAssetsPath, "open.txt");
if (!Directory.Exists(Application.streamingAssetsPath)) Directory.CreateDirectory(Application.streamingAssetsPath);
using (var file = File.Open(filePath, FileMode.OpenOrCreate, FileAccess.Write))
{
using (var writer = new StreamWriter(file))
{
writer.Write("Hello!");
}
}
#endregion DEMO Part
}
/// <summary>
/// Hinders the Editor to close if the request failed
/// </summary>
/// <returns></returns>
private static bool OnQuitEditor()
{
#region Required Part
// I'll just do a random Get Request but you can do any request here
var request = UnityWebRequest.Get("https://image.shutterstock.com/image-vector/example-red-square-grunge-stamp-260nw-327662909.jpg");
request.SendWebRequest();
while (!request.isDone && !request.isHttpError && !request.isNetworkError)
{
// just wait
}
if(request.isHttpError || request.isNetworkError || !string.IsNullOrWhiteSpace(request.error))
{
Debug.LogError("Couldn't finish closing request!");
return false;
}
#endregion Required Part
#region DEMO Part
// Just to show here that it worked delete the Open file and create a closed file
if (!Directory.Exists(Application.streamingAssetsPath)) Directory.CreateDirectory(Application.streamingAssetsPath);
var filePath = Path.Combine(Application.streamingAssetsPath, "open.txt");
if(File.Exists(filePath)) File.Delete(filePath);
filePath = Path.Combine(Application.streamingAssetsPath, "close.txt");
using (var file = File.Open(filePath, FileMode.OpenOrCreate, FileAccess.Write))
{
using (var writer = new StreamWriter(file))
{
writer.Write("World!");
}
}
#endregion DEMO Part
return string.IsNullOrEmpty(request.error);
}
}
Run Code Online (Sandbox Code Playgroud)
注意:目前编辑器无法以通常的方式关闭,以防关闭请求失败,因此您可以根据需要进行调整。例如,如果关闭请求的结果不是那么重要,您可以改为添加一个简单的voidas 回调EditorApplication.quitting。
作为它在这里工作的一个小演示,您可以看到当我打开项目时,它Assets/StreamingAssets/open.txt被创建了。
当我关闭项目时,Assets/StreamingAssets/open.txt将删除并Assets/StreamingAssets/closed.txt创建。