unity for android 自定义Activity

有些时候,用于对接得Android SDK要求主Activity必须继承其接口提供得activity,这时就不能继承unity的activity,我们只能手工创建unity player。

import com.unity3d.player.UnityPlayer;

import android.os.Bundle;
import android.app.Activity;
import android.content.res.Configuration;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

public class MainActivity extends Activity {

	private UnityPlayer mUnityPlayer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setTheme(android.R.style.Theme_NoTitleBar_Fullscreen);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        
        //创建unityplayer对象
        mUnityPlayer = new UnityPlayer(this);
        
        if(mUnityPlayer.getSettings().getBoolean("hide_status_bar", true))
        {
        	getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
        int glesMode = mUnityPlayer.getSettings().getInt("gles_mode", 1);
        boolean trueColor8888 = false;
        mUnityPlayer.init(glesMode, trueColor8888);
        
        //设置Unity view
        View playerView = mUnityPlayer.getView();
        setContentView(playerView);
        
        playerView.requestFocus();
    }

    //调用unity的onDestroy
    protected void onDestroy()
    {
    	super.onDestroy();
    	mUnityPlayer.quit();
    }
    
    //调用Unity的onPause
    protected void onPause()
    {
    	super.onPause();
    	mUnityPlayer.pause();
    }
    
    //调用Unity的onResume
    protected void onResume()
    {
    	super.onResume();
    	mUnityPlayer.resume();
    }
    
    //调用Unity的ConfigurationChanged
    public void onConfigurationChanged(Configuration newConfig)
    {
    	super.onConfigurationChanged(newConfig);
    	mUnityPlayer.configurationChanged(newConfig);
    }
    
    //调用Unity的onWindowFocusChanged
    public void onWindowFocusChanged(boolean hasFocus)
    {
    	super.onWindowFocusChanged(hasFocus);
    	mUnityPlayer.windowFocusChanged(hasFocus);
    }
    
    //调用Unity的onKeyDown
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
    	return mUnityPlayer.onKeyDown(keyCode, event);
    }
    
    //调用Unity的
    public boolean onKeyUp(int keyCode, KeyEvent event)
    {
    	return mUnityPlayer.onKeyUp(keyCode, event);
    }
    
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值