我是刚接触Unity 3D,以前也没学过C#。我需要开发一个项目,使用Unity 3D开发一个程序,可以在程序内部调用浏览器访问不同的链接。并且可以通过设置的按键关闭和打开内部浏览器。
我使用了Embedded Browser插件。现在遇到了几个问题。
将插件里的Browser脚本加入空组件,并写入固定的Url地址后,程序启动后,浏览器自动启动,并进入那个地址。我需要浏览器不随程序启动而启动,而是通过点击按键启动。我认为应该在插件的Browser脚本中加入
gameObject.SetActive(false);
,但我找不到放置这条指令合适的位置,或者应该使用别的指令阻止浏览器随程序开启。另外,我为包含浏览器的gameObject编写了一个关闭和开启的脚本。通过关闭按键可以关闭浏览器窗口的gameObject,通过程序内桌面的网页书签可以打开浏览器窗口的gameObject
public class OpenCloseOption : MonoBehaviour { // Close the browser window with cross button public void Close() { this.gameObject.SetActive(false); } // Open the browser window with bookmark button public void Open() { gameObject.SetActive(true); } }
这个脚本可以顺利关闭浏览器窗口,但在再次打开窗口时,浏览器会显示上次关闭时的页面(about.google)如图,而不是google首页。
有办法可以让gameObject在关闭,并重新打开后还原到原始状态吗?——载入google首页。我想要在程序内桌面上放置几个书签按钮,包含Url地址,然后保持Browser组件里的Url为空。
点击任意书签按钮,可以打开内置浏览器,并转至需要的链接。我在Browser脚本里找到这段代码,似乎是用于实现这一功能的。但我不知道应该如何修改。/** * Navigates to the given URL. If force is true, it will go there right away. * If force is false, pages that wish to can prompt the user and possibly cancel the * navigation. */ public void LoadURL(string url, bool force) { if (DeferUnready(() => LoadURL(url, force))) return; const string magicPrefix = "localGame://"; if (url.StartsWith(magicPrefix)) { url = LocalUrlPrefix + url.Substring(magicPrefix.Length); } if (string.IsNullOrEmpty(url)) { //If we ask CEF to load "" it will crash. Try Url = "about:blank" or LoadHTML() instead. throw new ArgumentException("URL must be non-empty", "value"); } loadPending = true; BrowserNative.zfb_goToURL(browserId, url, force); } /** * Loads the given HTML string as if it were the given URL. * For the URL use http://-like porotocols or else things may not work right. (In particular, the backend * might sanitize it to "about:blank" and things won't work right because it appears a page isn't loaded.) * * Note that, instead of using this, you can also load "data:" URIs into this.Url. * This allows pretty much any type of content to be loaded as the whole page. */ public void LoadHTML(string html, string url = null) { if (DeferUnready(() => LoadHTML(html, url))) return; //Debug.Log("Load HTML " + html); loadPending = true; if (string.IsNullOrEmpty(url)) { url = LocalUrlPrefix + "custom"; } if (string.IsNullOrEmpty(this.Url)) { //Nothing will happen if we don't have an initial page, so cause one. this.Url = "about:blank"; skipNextLoad = true; } BrowserNative.zfb_goToHTML(browserId, html, url); }
谢谢帮助