Unity-UWP平台使用Windows Store Nativ
2019-10-15 本文已影响0人
Walk_In_Jar
起因是一个uwp应用需要网页功能,正如知乎大神的这篇文章写的:
https://zhuanlan.zhihu.com/p/63424435
并没有完美的内嵌网页插件支持il2cpp,需要对
《Windows Store Native》
插件进行魔改,首先得知道为什么这个插件web功能不支持il2cpp,知乎大佬文章里说
也就是说,在il2cpp下,需要对这个_dxSwapChainPanel进行赋值就可以了,怎么获取呢,大佬在unity官方论坛留言咨询,得到了技术人员的回复:
https://forum.unity.com/threads/uwp-get-swapchainpanel-grid-in-il2cpp-xaml-build.502176/
首先需要在Plugins下创建一个.cpp C++代码,代码内容如下
#include <windows.ui.xaml.controls.h>
#include <wrl.h>
using namespace ABI::Windows::UI::Xaml;
using namespace ABI::Windows::UI::Xaml::Controls;
using namespace Microsoft::WRL;
extern "C" HRESULT GetPageContent(IInspectable* frame, IUIElement** pageContent)
{
*pageContent = nullptr;
ComPtr<IContentControl> frameContentControl;
auto hr = frame->QueryInterface(__uuidof(frameContentControl), &frameContentControl);
if (FAILED(hr))
return hr;
ComPtr<IInspectable> frameContentInspectable;
hr = frameContentControl->get_Content(&frameContentInspectable);
if (FAILED(hr))
return hr;
if (frameContentInspectable == nullptr)
return S_OK;
ComPtr<IUserControl> frameContent;
hr = frameContentInspectable.As(&frameContent);
if (FAILED(hr))
return hr;
return frameContent->get_Content(pageContent);
}
注意这是x64的代码,如果是x86,
需要替换
extern "C" HRESULT __stdcall GetPageContent(IInspectable* frame, IUIElement** pageContent)
unity中使用这个脚本就可以获取_dxSwapChainPanel了
using System.Runtime.InteropServices;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
#if ENABLE_WINMD_SUPPORT
[DllImport("__Internal")]
extern static int GetPageContent([MarshalAs(UnmanagedType.IInspectable)]object frame, [MarshalAs(UnmanagedType.IInspectable)]out object pageContent);
#endif
void Start()
{
UnityEngine.WSA.Application.InvokeOnUIThread(() =>
{
#if ENABLE_WINMD_SUPPORT
object pageContent;
var result = GetPageContent(Windows.UI.Xaml.Window.Current.Content, out pageContent);
if (result < 0)
Marshal.ThrowExceptionForHR(result);
var dxSwapChainPanel = pageContent as Windows.UI.Xaml.Controls.SwapChainPanel;
Debug.Log(dxSwapChainPanel);
#endif
}, false);
}
}
那怎么进行下一步操作来打开网页呢。
查询
https://github.com/ClaytonIndustries/WSANative/wiki
可知,
image.png
可知,webView暂时没有对il2cpp支持,所以需要打开《Windows Store Native》插件下的WSANativeWebView.cs脚本,对比其他支持il2cpp的可知,要编译il2cpp,需要将所有的 #if NETFX_CORE 替换为#if NETFX_CORE || (ENABLE_IL2CPP && UNITY_WSA_10_0),也就是.net支持换为.net和il2cpp支持。
image.png
接下来就好办了。借助官方人员的代码,就得到了这样的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using CI.WSANative.Web;
using System.Runtime.InteropServices;
using System;
public class Testss : MonoBehaviour
{
#if ENABLE_WINMD_SUPPORT
[DllImport("__Internal")]
extern static int GetPageContent([MarshalAs(UnmanagedType.IInspectable)]object frame, [MarshalAs(UnmanagedType.IInspectable)]out object pageContent);
#endif
// Start is called before the first frame update
void Awake()
{
#if NETFX_CORE || (ENABLE_IL2CPP && UNITY_WSA_10_0)
UnityEngine.WSA.Application.InvokeOnUIThread(() =>
{
#if ENABLE_WINMD_SUPPORT
object pageContent;
var result = GetPageContent(Windows.UI.Xaml.Window.Current.Content, out pageContent);
if (result < 0)
Marshal.ThrowExceptionForHR(result);
var dxSwapChainPanel = pageContent as Windows.UI.Xaml.Controls.SwapChainPanel;
WSANativeWebView.Configure(dxSwapChainPanel);
#endif
}, false);
#endif
}
private void Start()
{
#if NETFX_CORE || (ENABLE_IL2CPP && UNITY_WSA_10_0)
WSANativeWebView.Create(335, 310, 1250, 540, new Uri("http://www.baidu.com")) ;
#endif
}
}
挂载到任意物体,导出设置 :buildtype xaml ,添加Internetclient权限,打开后
image.png
webview出现了