本文将继续介绍SvrPlugin中的相关代码:
public SvrManager svrCamera = null;
public SvrEye[] eyes = null;
public SvrOverlay[] overlays = null;
public DeviceInfo deviceInfo;
在SvrPlugin中分别保存了SvrManager的实例 svrCamera,SvrEye数组 eyes,以及DeviceInfo变量deviceInfo,这些变量都可以通过SvrPlugin.Instance来调用。
其中,SvrManager保存的是当前场景中SvrManager的单例类,是在调用Initstialize()方法来实现赋值的。
public virtual IEnumerator Initialize ()
{
svrCamera = SvrManager.Instance;
if (svrCamera == null)
{
Debug.LogError("SvrManager object not found!");
yield break;
}
yield break;
}
在SvrPlugin中的BeginVr方法中完成了变量eyes的赋值逻辑:
public virtual IEnumerator BeginVr(int cpuPerfLevel =0, int gpuPerfLevel =0)
{
if (eyes == null)
{
eyes = SvrEye.Instances.ToArray();
if (eyes == null)
{
Debug.Log("Components with SvrEye not found!");
}
Array.Sort(eyes);
}
if (overlays == null)
{
overlays = SvrOverlay.Instances.ToArray();
if (overlays == null)
{
Debug.Log("Components with SvrOverlay not found!");
}
Array.Sort(overlays);
}
yield break;
}
而保存DeviceInfo的变量deviceInfo则是在SvrPlugin的子类SvrpluginAndroid以及SvrPluginWin中重载Initialize方法来赋值的:
SvrPluginAndroid
public override IEnumerator Initialize()
{
//yield return new WaitUntil(() => SvrIsInitialized() == false); // Wait for shutdown
yield return base.Initialize();
#if UNITY_ANDROID && !UNITY_EDITOR
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
SvrInitializeEventData(activity.GetRawObject());
#endif
IssueEvent(RenderEvent.Initialize);
yield return new WaitUntil (() => SvrIsInitialized () == true);
yield return null; // delay one frame - fix for re-init w multi-threaded rendering
deviceInfo = GetDeviceInfo();
}
SvrPluginWin:
public override IEnumerator Initialize()
{
yield return base.Initialize();
deviceInfo = GetDeviceInfo();
yield break;
}