默认分类
只是一个默认分类

unity3d冷启动黑屏优化

问题分析

在 Unity 开屏的开发过程中,通常在 Unity 中编写是 Unity 的开屏页,在 Android 端运行时会额外添加unity_static_splash, 来保证Android 端展示时不是 黑屏,但是 unity_static_splash 只能使用 图片资源,如果涉及到需要根据屏幕尺寸进行动态适配的时候,一张图片就不能满足要求。 同时如果 Unity 开屏和unity_static_splash 展示相同内容的话,由于适配方式不一致,会出现界面抖动。

Static Splash Image 原理

在 Unity 中通常可以设置 Static Splash Image 来屏蔽黑屏,那么Static Splash Image 是如何实现的在 Android 端展示并且消失的? 将 Unity 项目导出 Android 项目之后,可以在drawable 文件夹中找到对应的图片

Android 端启动 Unity 的 Activity 是UnityPlayerActivity,分析源码过程

//UnityPlayerActivity  
@Override protected void onCreate(Bundle savedInstanceState)
    {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);

        String cmdLine = updateUnityCommandLineArguments(getIntent().getStringExtra("unity"));
        getIntent().putExtra("unity", cmdLine);

        mUnityPlayer = new UnityPlayer(this);
        setContentView(mUnityPlayer);
        mUnityPlayer.requestFocus();
    }

UnityPlayer 是一个 FrameLayout, 具体实现

// UnityPlayer
public UnityPlayer(Context var1) {
    super(var1);
    if (var1 instanceof Activity) {
      currentActivity = (Activity)var1;
      this.c = currentActivity.getRequestedOrientation();
    }

    a(currentActivity);
    this.q = var1;
    if (currentActivity != null && this.k()) {
      this.m = new l(this.q, com.unity3d.player.l.a.a()[this.getSplashMode()]);
      this.addView(this.m);
    }
    ....
    ...
}

分析代码,new l 添加到了布局里面,猜测应该是这个类

//l  
public l(Context var1, int var2) {
    super(var1);
    this.a = var2;
    this.b = this.getResources().getIdentifier("unity_static_splash", "drawable", this.getContext().getPackageName());
    if (this.b != 0) {
      this.forceLayout();
    }

  }

可以看到L 这个类也是个 View, 读取unity_static_splash,然后添加到父布局里面。 这样就了解了unity_static_splash 的原理 ​

黑屏优化

出现黑屏的前提是不能使用unity_static_splash,可能是因为

需要根据屏幕适配调整布局 播放视频或者其他资源

添加自定义布局展示开屏

针对以上的情况,我们可以手动编写布局,然后同样添加到UnityPlayerActivityd的布局中实现开屏的展示 java复制代码//MyUnityPlayerActivity
@Override protected void onCreate(Bundle bundle) { super.onCreate(bundle); ViewGroup layoutView = (FrameLayout) View.inflate(this, R.layout.activity_splash, null); addContentView(layoutView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); }

添加自定义布局展示任意内容。同时需要将 Unity中 Static Splash Image 设置为空

关于添加布局消失

在添加了自定义展示的开屏之后 ,需要处理的就是 什么时机让自定义布局消失。两种方案

利用 AndroidJavaObject, 在 Unity 中调用 Android 端的函数,实现关闭布局。 利用延时机制,设置 1s 或者 0.5 s 之后关闭

以上两种方案都会有一些问题

方案一导致 Unity 开屏内容会有一部分遮挡 Unity 开屏展示时机不确定,延时可能短或者长,短就会出现黑屏(因为没有unity_static_splash),长就会遮挡 Unity 开屏。

分析unity_static_splash 的消失时机,找到合适的关闭自定义布局的时机 java复制代码//MyUnityPlayerActivity
private void a() { this.a(new Runnable() { public final void run() { UnityPlayer.this.removeView(UnityPlayer.this.m); UnityPlayer.f(UnityPlayer.this); } }); }

UnityPlayer 系统通过移除布局来实现关闭unity_static_splash 界面,那么可以监听父布局的子View 移除,如果是 L ,那么就代表unity_static_splash 被关闭,同时关闭我们自己的自定义布局就可以了

关闭自定义布局

java复制代码//MyUnityPlayerActivity @Override protected void onCreate(Bundle bundle) { super.onCreate(bundle); ... ... mUnityPlayer.setOnHierarchyChangeListener(new ViewGroup.OnHierarchyChangeListener() { @Override public void onChildViewAdded(View parent, View child) {

  }

  @Override public void onChildViewRemoved(View parent, View child) {
      //判断是l,关闭自定义布局
    if (child instanceof com.unity3d.player.l) {
      removeSplashLayout();
    }
  }
});

添加布局监听,判断类型,关闭自定义布局

加密unity3d中dll文件,防止破解

遇到很多坑,慢慢记录,这里使用的centos 6.7,环境搭建路漫漫,一定要根据错误提示来解决问题

  1. 下载源码 https://github.com/Unity-Technologies/mono/tree/unity-5.3

  2. 安装环境 64bit
yum install autoconf

yum install libtool

yumintall –y gcc-c++

yum install bison

yum install –y gettext

yum install glib2-devel.i686 #注意此处安装的是glib开发包

yum installperl #安装perl

yum install git #安装git,后面编译的脚本会用到git去下载

yum install glibc.i686

yum install glibc-devel.i686

yum install libstdc++.i686

yum install zlib-devel.i686

yum install ncurses-devel.i686

yum install libX11-devel.i686

64位linux下使用32位的SDK进行开发必须安装如下的32位的包

http://stream2010.iteye.com/blog/1174242 http://blog.csdn.net/yxq408576080/article/details/47779395 http://csftech.logdown.com/posts/452269-android-unity-encryption

mac下使用jenkins

连接ftp服务器

man ftp 可以看到有这些信息。 NAME ftp -- Internet file transfer program

SYNOPSIS ftp [-46AadefginpRtvV] [-N netrc] [-o output] [-P port] [-q quittime] [-s srcaddr] [-r retry] [-T dir,max[,inc]] [[user@]host [port]] [[user@]host:[path][/]] [file:///path] [ftp://[user[:password]@]host[:port]/path[/][;type=X]] [http://[user[:password]@]host[:port]/path] [...] ftp -u URL file [...] 连接服务器的话基本上就用到上面的讯息了。原本没有看man手册,一直使用 ftp user@xxx.com port 每次都要输入密码。后来还是用了下面这个更加简单的 ftp ftp://User:Passwd@xxx.com:port

浏览文件

命令和Windows、Linux的命令基本相同 ftp> cd Documents ftp> ls
ftp> dir

下载上传文件

put filename - Upload a file to the server

get filename - Download a file from the server

mput filename - Put multiple files on the server

mget filename - Get multiple files on the server

断开连接

bye:中断与服务器的连接。 ftp> bye


大部分的命令如下,可敲入man ftp获得

ls – list the contents of a directory on the FTP server cd – change the working directory on the FTP server pwd – show the current directory on the FTP server get – download files from the FTP server put – upload files to the FTP server account – include a password with your login information bye – terminate an ftp session and close ftp (or use disconnect to simply terminate a session) bell – make a cute sound after each file transfer is done chmod – change permissions delete – your guess is as good as mine (OK, you got me, it’s to delete a file off the server) glob – enable globbing hash – only functional in Amsterdam help – get help lpwd – print the local working directory for transfers mkdir – create folders on the FTP server rmdir – delete folders from the FTP server newer – only get a file if it’s newer (great for scripting synchronizations) nmap – use positional parameters to set filenames passive – use FTP passive mode prompt – allows the use of letters to automate answers to prompts rate – limit the speed of an upload or download

关于ftp,你甚至还可以写脚本进行文件操作,比如

#!/bin/bash
ftp -d krypted.com << ftpEnd
prompt
cd /Library/WebServer/Documents
put “*.html”
put “*.php”
cd /Library/WebServer/Documents
put “*.png”
quit
ftpEnd

#!/bin/bash
ftp -d krypted.com << ftpEnd
prompt
cd /My/Documents
get “*.doc”
quit
ftpEnd

在你的脚本中,可以使用以下几个字符获取一些特定的变量:

%/ – the current working directory of the FTP server
%M – the hostname of the FTP server
%m – the hostname only up to the .
%n – the username used for the FTP server