Beispiel #1
0
    /// <summary>
    /// check for screen orientation changes
    /// </summary>
    public void Update()
    {
        if (Application.platform == RuntimePlatform.Android)
        {
            if (SurfaceUtilities.HasSurfaceBeenRecreated())
            {
                InitializeSurface();
            }
            else
            {
                // if Unity reports that the orientation has changed, reset the member variable
                // - this will trigger a check in Java for a few frames...
                if (Screen.orientation != mScreenOrientation)
                {
                    ResetUnityScreenOrientation();
                }

                CheckOrientation();

                if (mScreenWidth != Screen.width || mScreenHeight != Screen.height)
                {
                    mScreenWidth  = Screen.width;
                    mScreenHeight = Screen.height;
                    SurfaceUtilities.OnSurfaceChanged(mScreenWidth, mScreenHeight);
                }
            }

            mFramesSinceLastOrientationReset++;
        }
    }
        public DebugBitmapDialog(string title, CanvasBitmap bitmap)
        {
            this.InitializeComponent();

            Title           = title;
            RootGrid.Width  = bitmap.Size.Width;
            RootGrid.Height = bitmap.Size.Height;

            var compositor = Window.Current.Compositor;
            var visual     = compositor.CreateSpriteVisual();

            visual.RelativeSizeAdjustment = Vector2.One;

            var brush = compositor.CreateSurfaceBrush();

            brush.Stretch = CompositionStretch.Uniform;
            brush.BitmapInterpolationMode  = CompositionBitmapInterpolationMode.NearestNeighbor;
            brush.HorizontalAlignmentRatio = 0.5f;
            brush.VerticalAlignmentRatio   = 0.5f;

            visual.Brush = brush;

            var surfaceFactory = SurfaceFactory.GetSharedSurfaceFactoryForCompositor(compositor);

            _surface = surfaceFactory.CreateSurface(bitmap.Size);
            SurfaceUtilities.FillSurfaceWithDirect3DSurface(surfaceFactory, _surface, bitmap);
            brush.Surface = _surface;
            _bitmap       = bitmap;

            ElementCompositionPreview.SetElementChildVisual(RootGrid, visual);
        }
Beispiel #3
0
    private void InitializeSurface()
    {
        SurfaceUtilities.OnSurfaceCreated();

        AndroidJavaClass javaUnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");

        mCurrentActivity = javaUnityPlayer.GetStatic <AndroidJavaObject>("currentActivity");
        if (mCurrentActivity != null)
        {
            mJavaOrientationUtility = new AndroidJavaClass("com.qualcomm.QCARUnityPlayer.OrientationUtility");
        }

        ResetUnityScreenOrientation();
        CheckOrientation();

        mScreenWidth  = Screen.width;
        mScreenHeight = Screen.height;
        SurfaceUtilities.OnSurfaceChanged(mScreenWidth, mScreenHeight);
    }
Beispiel #4
0
    private void CheckOrientation()
    {
        // check for the activity orientation for a few frames after it has changed in Unity
        if (mFramesSinceLastOrientationReset < NUM_FRAMES_TO_QUERY_ORIENTATION)
        {
            // mScreenOrientation remains at the value reported by Unity even when the acitivity reports a different one
            // otherwise the check for orientation changes will return true every frame.
            int correctScreenOrientation = (int)mScreenOrientation;

            if (mCurrentActivity != null)
            {
                // The orientation reported by Unity is not reliable on some devices (e.g. landscape right on the Nexus 10)
                // We query the correct orientation from the activity to make sure.
                int activityOrientation = mJavaOrientationUtility.CallStatic <int>("getSurfaceOrientation", mCurrentActivity);
                if (activityOrientation != 0)
                {
                    correctScreenOrientation = activityOrientation;
                }
            }

            SurfaceUtilities.SetSurfaceOrientation(correctScreenOrientation);
        }
    }