使用官方API跳过Unity启动页
1.通过Unity的SplashScreen提供的接口
[Preserve]
public class SkipSplash
{
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSplashScreen)]
private static void BeforeSplashScreen()
{
#if UNITY_WEBGL
Application.focusChanged += Application_focusChanged;
#else
System.Threading.Tasks.Task.Run(AsyncSkip);
#endif
}
#if UNITY_WEBGL
private static void Application_focusChanged(bool obj)
{
Application.focusChanged -= Application_focusChanged;
SplashScreen.Stop(SplashScreen.StopBehavior.StopImmediate);
}
#else
private static void AsyncSkip()
{
SplashScreen.Stop(SplashScreen.StopBehavior.StopImmediate);
}
#endif
}
问题
1.通过接口打包出来发现启动页确实去掉了,但是在Android上发现启动后会有一小段黑屏,项目越大资源越多,首帧逻辑越复杂,黑屏时间越长,unity相对其他引擎,启动过程确实比较长,不太友好
2.这里有个思路就是在Unity启动到场景首帧这个过程中在盖上一个加载页面,不就可以到去掉启动页/黑屏阶段了吗
Android 上跳过启动页/黑屏
1.我们在Unity上BuildSettings页面勾选Export Project导出Android工程
2.导出后使用Android Studio打开工程,可以看到在unityLibrary模块下的com.unity3d.player里面有个UnityPlayerActivity
3.打开UnityPlayerActivity可以看到重写的onCreate函数里面,unity使用了自己的一个view视图UnityPlayer
@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, this);
setContentView(mUnityPlayer);
mUnityPlayer.requestFocus();
}
4.我们导入一张背景图片作为加载页面,在res文件夹下面创建一个drawable文件夹,将bg.png图片放在drawable目录下
5.接下来我们在UnityPlayer上面盖上我们导入的资源bg.png,这里直接动态生成一张图片了
private ImageView bgView = null;
private void addSplash(){
bgView=new ImageView(this);
int bg=getResources().getIdentifier("bg", "drawable", getPackageName());
bgView.setScaleType(ImageView.ScaleType.CENTER_CROP);
bgView.setImageResource(bg);
mUnityPlayer.addView(bgView);
}
6.接下来在onCreate里面调用一下,这样我们启动应用就会看见unity启动页被bg给覆盖了
@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, this);
setContentView(mUnityPlayer);
mUnityPlayer.requestFocus();
addSplash();
}
7.接下来就是在合适的时机把这张图片给移除掉了,我们在unity进入首场景的时候通知android端移除掉该图片即可 我们把android隐藏接口写好
public void HideSplash() {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
mUnityPlayer.removeView(bgView);
bgView = null;
}
});
}
Unity与Android通信
Unity端
1.获取Android端的java脚本
如:这是android端新建的java脚本AndroidBridge.java
2.通过AndroidJavaClass获取到AndroidBridge脚本,如果要获取脚本实例,可以通过AndroidJavaObject
private AndroidJavaClass brideg;
private void Awake()
{
brideg = new AndroidJavaClass("com.unity3d.player.AndroidBridge");
}
3.调用AndroidBridge脚本里的静态方法
brideg.CallStatic("OnReceiveUnity", param1, param2);
Android端
1.简单写个单例
private static AndroidBridge instance;
public static AndroidBridge getInstance(){
if(instance==null){
instance=new AndroidBridge();
}
return instance;
}
2.写个初始话方法缓存下UnityActivity
private Activity context;
public void init(Activity activity){
context=activity;
}
3.写一下给Unity端调用的接口
public static void OnReceiveUnity(String param1,String param2)
{
HideSplash();
}
public static void HideSplash()
{
((UnityPlayerActivity)getInstance().context).HideSplash();
}
4.最后在Unity场景启动的时候Call一下OnReceiveUnity就可以隐藏启动页面了