2016-03-07 187 views
0

我想創建一個簡單的應用程序,它捕捉到剪貼板(按Ctrl + C)網址鏈接,並打開他們在一個窗口(也關閉的時候,打開的窗口)。Unity3D - Webplayer不會在後臺運行(但設置設置...)?

我做了一個非常簡單的腳本:

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不運行這段代碼,當它沒有專注......(在後臺運行選項設置代碼和也是在播放器設置...)

有誰知道,如何解決我的問題呢?

謝謝!

+0

只檢查已更改Web播放器播放器設置爲在後臺運行,而不是一個不同中等錯誤? – Zze

+0

我現在檢查了兩次,以確保。但的確,該設置是設置... – 3DExtended

回答

1

這聽起來像一個錯誤我。我也遇到了這個bug,5.1和下面的代碼解決它。不知道你使用的是什麼版本,但試試看。

將代碼放在你的更新功能開始功能

if (!Application.runInBackground) { 
      Application.runInBackground = true; 
} 

編輯1: 提到你正在使用5.1.something。這是一個錯誤已被固定在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

+0

嘿!感謝回覆! 此代碼並沒有解決我的問題,但我確實使用Unity 5.1 ...我會馬上更新,讓你知道,發生了什麼。 – 3DExtended

+0

我剛剛檢查了Unity bug報告系統以驗證該錯誤在5.2中列爲固定。2版本。所以是更新Unity。 – Programmer

+0

我做過了,但並沒有解決問題......我的代碼只適用於當我重新調整網絡播放器的焦點...任何其他想法? – 3DExtended

相關問題