美文网首页Android Development
在Fragment中集成Unity

在Fragment中集成Unity

作者: 喜欢书的女孩 | 来源:发表于2016-11-28 16:58 被阅读838次
    2017-1-12
    突然特别感谢在我编程时出现的bug,因为有了它,我才了解到什么是谷歌上没有的知识,或者不全面的知识,这样我才有内容可写,不想写别人写过的,因为觉得那样浪费资源。。。只是简单的重复别人的东西,只会增加资源冗余。昨晚遇到了一个bug,就是之前在activity中加载unity文件是可以正常显示的,具体参考上一篇文章unity3d项目导入android studio,然后我需要在一个fragment中也加载unity文件,显示unity场景,然而却出现了bug.

    1.问题所在:

    [区别1]
     Activity 显示Unity3d内容是 extends UnityPlayerNativeActivity
    UnityPlayerNativeFragment 显示Unity3d内容是extends Fragment
    [区别2]
    [2.1]在activity中
    View view=mUnityPlayer.getView(); 
    scan.addView(view);       //scan是显示unity的布局
    [2.2]在fragment中
    protected UnityPlayer mUnityPlayer;
    private static final String ARG_SECTION_NUMBER = "section_number";
    public static UnityPlayerNativeFragment newInstance(int sectionNumber) 
    { 
       UnityPlayerNativeFragment fragment = new UnityPlayerNativeFragment();    
       Bundle args = new Bundle();    
       args.putInt(ARG_SECTION_NUMBER, sectionNumber);        
       fragment.setArguments(args);    
       return fragment;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,                        
    Bundle savedInstanceState)
     {    
        getActivity().getWindow().takeSurface(null);       
        getActivity().setTheme(android.R.style.Theme_NoTitleBar_Fullscreen); 
        getActivity().getWindow().setFormat(PixelFormat.RGB_565);    
        mUnityPlayer = new UnityPlayer(getActivity());    
        if (mUnityPlayer.getSettings ().getBoolean ("hide_status_bar", true))        
        getActivity().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);    
        mUnityPlayer.windowFocusChanged(true);    
        View playerView = mUnityPlayer.getView();    
        return playerView;
    }
    

    2.写一个具体的demo

    1.android studio 2.2
    2.新建工程
    3.在MainActivity中添加两个fragment

    3.导入unity项目

    1.将unity3d项目目录下的libs下的jar文件复制黏贴到android studio 项目下的libs,并右击其中一个jar,选择add as library.
    2.将unity3d项目下的assets文件复制黏贴到android studio项目下的main文件下。
    3.在main文件下新建文件jniLibs,将unity3d项目libs文件里的armeabi-v7a复制到jniLibs文件下。
    4.配置清单文件

    4.实例代码

    在MainActivity

    @Override
    public void onClick(View v) 
    {    switch (v.getId()){        
    case R.id.scan:            
    L.i(LOGTAG,"呈现Unity");            
    Toast.makeText(getApplicationContext(), "加载中!",Toast.LENGTH_LONG).show();           
     clickScan();            
    break;        
    case R.id.me:            
    L.i(LOGTAG,"个人中心");            
    image03.setImageResource(R.mipmap.bottom_btn_me);            
    clickMe();            
    break;    
    }
    }
    public void clickScan(){    
    if(UnityPlayerNativeFragment==null)
    {        
    scanFragment=new UnityPlayerNativeFragment();    
    }    
    addOrShowFragment(getSupportFragmentManager().beginTransaction(),scanFragment);
    }
    

    在UnityPlayerNativeFragment

    public class UnityPlayerNativeFragment extends Fragment {    
    protected UnityPlayer mUnityPlayer;    
    private static final String ARG_SECTION_NUMBER = "section_number";    
    public static UnityPlayerNativeFragment newInstance(int sectionNumber) 
    {        
    UnityPlayerNativeFragment fragment = new UnityPlayerNativeFragment();        Bundle args = new Bundle();        
    args.putInt(ARG_SECTION_NUMBER, sectionNumber);        
    fragment.setArguments(args);        
    return fragment;    
    }    
    @Override    
    public View onCreateView(LayoutInflater inflater, ViewGroup container,                             
    Bundle savedInstanceState) {        
    getActivity().getWindow().takeSurface(null);        
    getActivity().setTheme(android.R.style.Theme_NoTitleBar_Fullscreen);        
    getActivity().getWindow().setFormat(PixelFormat.RGB_565);        
    mUnityPlayer = new UnityPlayer(getActivity());        
    if (mUnityPlayer.getSettings ().getBoolean ("hide_status_bar", true))            
    getActivity().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);        
    mUnityPlayer.windowFocusChanged(true);        
    View playerView = mUnityPlayer.getView();        
    return playerView;    
    }    
    @Override    
    public void onDestroy ()    
    {       
     mUnityPlayer.quit();        
    super.onDestroy();    
    }    
    @Override    
    public void onPause()    
    {        
    super.onPause();        
    mUnityPlayer.pause();    
    }       
    @Override   
     public void onResume()   
     {        
    super.onResume();        
    mUnityPlayer.resume();    
    }        
    @Override    
    public void onConfigurationChanged(Configuration newConfig)   
     {        
    super.onConfigurationChanged(newConfig);        
    mUnityPlayer.configurationChanged(newConfig);   
     }    
    public void onWindowFocusChanged(boolean hasFocus)   
     {        
    mUnityPlayer.windowFocusChanged(hasFocus);    
    }
    }
    

    基本就是这样了。。。运行,调试,success.

    参考资料
    Unity
    demo

    相关文章

      网友评论

        本文标题:在Fragment中集成Unity

        本文链接:https://www.haomeiwen.com/subject/tguopttx.html