简体   繁体   English

我无法在 android studio 中捕获 unityplayer 的屏幕

[英]I can't capture the screen of unityplayer in android studio

UnityPlayerActivity.java UnityPlayerActivity.java

// GENERATED BY UNITY. REMOVE THIS COMMENT TO PREVENT OVERWRITING WHEN EXPORTING AGAIN.
package com.unity3d.player;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;

public class UnityPlayerActivity extends Activity implements IUnityPlayerLifecycleEvents
{
    protected UnityPlayer mUnityPlayer; // don't change the name of this variable; referenced from native code
    FrameLayout fl_forUnity;
    public static Context mContext;
    public String[] cur_name=new String[5];
    public String[] cur_part=new String[5];

    ImageView imageView;
    // Setup activity layout
    @Override protected void onCreate(Bundle savedInstanceState)
    {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        getWindow().setFormat(PixelFormat.RGBX_8888); // <--- This makes xperia play happy

        mUnityPlayer = new UnityPlayer(this);
        if (mUnityPlayer.getSettings ().getBoolean ("hide_status_bar", true))
        {
            setTheme(android.R.style.Theme_NoTitleBar_Fullscreen);
            getWindow ().setFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }

        //setContentView(mUnityPlayer);
        //Set the content to main
        setContentView(R.layout.main);

        //Inflate the frame layout from XML
        this.fl_forUnity = (FrameLayout)findViewById(R.id.fl_forUnity);

        //Add the mUnityPlayer view to the FrameLayout, and set it to fill all the area available
        this.fl_forUnity.addView(mUnityPlayer.getView(),
                FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);

        mContext=this;
        mUnityPlayer.requestFocus();

        Button btn_capture=findViewById(R.id.bt_capture);
        btn_capture.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                                 
                imageView=findViewById(R.id.imageView);
                imageView.setImageBitmap();
            }
        });

        mUnityPlayer.requestFocus();

    }

}

main.xml主文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:rowCount="3"
    android:columnCount="10"
    android:background="#FFFFFF">


    <FrameLayout
        android:id="@+id/fl_forUnity"
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:layout_row="0"
        android:layout_column="4"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:background="#FBFBFA"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <Button
        android:id="@+id/bt_capture"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="143dp"
        android:layout_marginBottom="140dp"
        android:text="capture"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/fl_forUnity" />


    <ImageView
        android:id="@+id/imageView"
        android:layout_width="200dp"
        android:layout_height="360dp"
        android:layout_marginEnd="120dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/fl_forUnity" />

</androidx.constraintlayout.widget.ConstraintLayout>

framelayout shows the screen of unityplayer. framelayout 显示了 unityplayer 的屏幕。

I want to capture the screen of unityplayer.我想捕获 unityplayer 的屏幕。

I have tried several methods.我尝试了几种方法。 The two below are examples.下面两个是例子。 However, no matter which method is used, the unityplayer screen is always captured in black or white.但是,无论使用哪种方法,unityplayer 屏幕总是以黑色或白色捕获。

I also used drawing on canvas.我也在画布上绘画。 but it doesn't work但它不起作用

FrameLayout capture = (FrameLayout) findViewById(R.id.fl_forUnity);
capture.buildDrawingCache();
Bitmap captureview = capture.getDrawingCache();

it does not work.这没用。

View view = mUnityPlayer.getView();
view.buildDrawingCache();
view.setDrawingCacheEnabled(true);
Bitmap saveBitmap = v1.getDrawingCache();

it also does not work.它也不起作用。

Please try the below way of capture you layout请尝试下面的方式来捕捉你的布局

(1) first way
Bitmap bitmap = Bitmap.createBitmap(
                lContentRoot.width,
                rlContentRoot.height
                , Bitmap.Config.ARGB_8888
        );
        canvas canvas = new Canvas(bitmap);
        rlContentRoot.draw(canvas)

        byte[] bytes = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, bytes);
        File file = getimagePath(this, "layer");
        try {
            FileOutputStream fo = new FileOutputStream(file);
            fo.write(bytes.toByteArray());
            fo.flush();
            fo.close();
        } catch (IOException e) {
            e.printStackTrace()
        }



(2) second way

    v.setDrawingCacheEnabled(true);
    Bitmap bmp = Bitmap.createBitmap(v.getDrawingCache());
    v.setDrawingCacheEnabled(false);
    try {
        FileOutputStream fos = new FileOutputStream(new File(Environment
                .getExternalStorageDirectory().toString(), "SCREEN"
                + System.currentTimeMillis() + ".png"));
        bmp.compress(CompressFormat.PNG, 100, fos);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

hope it may help you希望它可以帮助你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android Studio 屏幕截图 - Android Studio Screen Capture 我无法使用adb屏幕录制工具捕获android屏幕 - I can't capture android screen using adb screen record tool Android Studio:我无法将按钮置于屏幕底部 - Android Studio: I can’t make button be at the bottom of screen 如何将消息从Android发送到Unity(使用VR)? (UnityPlayer.UnitySendMessage不起作用) - How do I send messages from Android to Unity (with VR)? (UnityPlayer.UnitySendMessage doesn't work) 卡在图像捕获屏幕上,无法返回onActivityResult(Android) - stuck on image capture screen ,can't get back to onActivityResult (Android) 我可以运行模拟器(Android Studio),但是在模拟器屏幕上找不到我制作的应用程序 - I can run my emulator(Android studio) but in emulator screen I can't find the app I made 我可以使用服务从Android应用程序截屏吗? - Can I take a screen capture from an Android app by using a service? 如何在Android设备上捕获屏幕捕获事件? - How can I catch a screen capture event on android devices? 如何在Android的xamarin中捕获带有弹出窗口的屏幕截图? - How can I capture screenshot of a screen with a popup in xamarin in Android? 我找不到 ViewPager2 的 AppCompatTextView(捕获屏幕) - I can't find AppCompatTextView of ViewPager2(Capture Screen)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM