Unity3D - Webplayer 没有 运行 在后台(但设置已设置...)?
Unity3D - Webplayer does not run in background (but setting is set...)?
我想创建一个简单的应用程序,它在剪贴板 (Ctrl + C) 中捕获 URL-Links 并在 window 中打开它们(也关闭旧的,打开的 window).
我做了一个很简单的脚本:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
public class ClipboardWebpage : MonoBehaviour {
List<string> openedWindows = new List<string>();
void OpenNewPage(string url) {
string name = "window";
openedWindows.Add(name);
Application.ExternalEval("var " + name + " = window.open('" + url + "', 'title')");
}
void Start()
{
Application.runInBackground = true;
}
string lastCheck = "";
void Update () {
string clip = getClipboard();
if (!lastCheck.Equals(clip))
{
lastCheck = clip;
Uri uriResult;
bool result = Uri.TryCreate(lastCheck, UriKind.Absolute, out uriResult)
&& (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);
if (result)
{
CloseAll();
OpenNewPage(lastCheck);
}
}
}
void CloseAll()
{
foreach (string name in openedWindows)
{
Application.ExternalEval(name + ".close()");
}
}
string getClipboard()
{
return GUIUtility.systemCopyBuffer;
}
void setClipboard(string value)
{
GUIUtility.systemCopyBuffer = value;
}
}
问题是,Web Player 没有 运行 这段代码,当它没有聚焦时...(运行 in background 选项在代码和播放器设置中设置...)
有谁知道如何解决我的问题?
谢谢!
对我来说,这听起来像是一个 bug。我也用 5.1 遇到过这个错误,下面的代码解决了它。不知道你用的是什么版本,试试看吧
将代码放在 Update 函数 NOT 中 Start 函数
if (!Application.runInBackground) {
Application.runInBackground = true;
}
编辑 1:
提到您正在使用 5.1.something。这是 bug,已在 5.2.2 版本中修复。
编辑 2:
解决方案 是使用WebGL。
Application.ExternalCall()
和 Application.ExternalEval()
不适用于 WebGL。 WebGL 要求你为它制作一个插件。你这样做的方式与你使用 WebPlayer 的方式不同。
JavaScript 插件:
var MyPlugin = {
HelloString: function(str)
{
window.alert(Pointer_stringify(str));
},
AddNumbers: function(x,y)
{
return x + y;
},
};
mergeInto(LibraryManager.library, MyPlugin);
另存为Assets/Plugins/WebGL/MyPlugin.jslib
。文件扩展名必须以 .jslib.
结尾
从 C# 调用:
[DllImport("__Internal")]
private static extern void HelloString(string str);
[DllImport("__Internal")]
private static extern int AddNumbers(int x, int y);
void Start() {
HelloString("This is a string.");
int result = AddNumbers(5, 7);
Debug.Log(result);
}
注意: 确保包括 using System.Runtime.InteropServices;
http://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html
我想创建一个简单的应用程序,它在剪贴板 (Ctrl + C) 中捕获 URL-Links 并在 window 中打开它们(也关闭旧的,打开的 window).
我做了一个很简单的脚本:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
public class ClipboardWebpage : MonoBehaviour {
List<string> openedWindows = new List<string>();
void OpenNewPage(string url) {
string name = "window";
openedWindows.Add(name);
Application.ExternalEval("var " + name + " = window.open('" + url + "', 'title')");
}
void Start()
{
Application.runInBackground = true;
}
string lastCheck = "";
void Update () {
string clip = getClipboard();
if (!lastCheck.Equals(clip))
{
lastCheck = clip;
Uri uriResult;
bool result = Uri.TryCreate(lastCheck, UriKind.Absolute, out uriResult)
&& (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);
if (result)
{
CloseAll();
OpenNewPage(lastCheck);
}
}
}
void CloseAll()
{
foreach (string name in openedWindows)
{
Application.ExternalEval(name + ".close()");
}
}
string getClipboard()
{
return GUIUtility.systemCopyBuffer;
}
void setClipboard(string value)
{
GUIUtility.systemCopyBuffer = value;
}
}
问题是,Web Player 没有 运行 这段代码,当它没有聚焦时...(运行 in background 选项在代码和播放器设置中设置...)
有谁知道如何解决我的问题?
谢谢!
对我来说,这听起来像是一个 bug。我也用 5.1 遇到过这个错误,下面的代码解决了它。不知道你用的是什么版本,试试看吧
将代码放在 Update 函数 NOT 中 Start 函数
if (!Application.runInBackground) {
Application.runInBackground = true;
}
编辑 1: 提到您正在使用 5.1.something。这是 bug,已在 5.2.2 版本中修复。
编辑 2: 解决方案 是使用WebGL。
Application.ExternalCall()
和 Application.ExternalEval()
不适用于 WebGL。 WebGL 要求你为它制作一个插件。你这样做的方式与你使用 WebPlayer 的方式不同。
JavaScript 插件:
var MyPlugin = {
HelloString: function(str)
{
window.alert(Pointer_stringify(str));
},
AddNumbers: function(x,y)
{
return x + y;
},
};
mergeInto(LibraryManager.library, MyPlugin);
另存为Assets/Plugins/WebGL/MyPlugin.jslib
。文件扩展名必须以 .jslib.
从 C# 调用:
[DllImport("__Internal")]
private static extern void HelloString(string str);
[DllImport("__Internal")]
private static extern int AddNumbers(int x, int y);
void Start() {
HelloString("This is a string.");
int result = AddNumbers(5, 7);
Debug.Log(result);
}
注意: 确保包括 using System.Runtime.InteropServices;
http://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html