Unity3D 2020 Android显示和修改状态栏

开发中遇到了需要修改状态栏的问题,看了其它文章说新版本UnityPlayerActivity不再能被继承,所以打包aar用unity调用变得不好用了,找了好多资料都不是很理想。直到看了一个老外的帖子。

Unity tidbits: changing the visibility of Android’s navigation and status bars, and implementing immersive mode – Zeh Fernando

根据他的帖子我做了一些修改。

using System;
using System.Collections.Generic;
using UnityEngine;

class ApplicationChrome
{
    private static AndroidJavaObject activity;

    private static AndroidJavaObject Activity
    {
        get
        {
            if (activity == null)
            {
                var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
                activity = unityPlayer.GetStatic("currentActivity");
            }
            return activity;
        }
    }

    private static AndroidJavaObject Window
    {
        get
        {
            return Activity.Call("getWindow");
        }
    }

    private static AndroidJavaObject View
    {
        get
        {
            return Window.Call("getDecorView");
        }
    }
    /// 
    /// 测试
    /// 
    public static void TestCall()
    {
        if (Activity != null)
        {
            Debug.Log("Activity 不为空");
        }
        else
        {
            Debug.Log("Activity 为空");
        }


        if (Window != null)
        {
            Debug.Log("Window 不为空");
        }
        else
        {
            Debug.Log("Window 为空");
        }


        if (View != null)
        {
            Debug.Log("View 不为空");
        }
        else
        {
            Debug.Log("View 为空");
        }
    }
    /// 
    /// 设置是否显示状态栏
    /// 
    /// 
    public static void SetStatusBar(bool isShow)
    {
        if (isShow)
        {
            RunOnAndroidUiThread(()=> 
            {
                if (Window != null)
                {
                    Window.Call("clearFlags", 1024);
                }
            });
        }
        else
        {
            RunOnAndroidUiThread(() =>
            {
                if (Window != null)
                {
                    Window.Call("addFlags", 1024);
                }
            });
        }
    }

    /// 
    /// 设置状态栏颜色&状态栏字体颜色
    /// 
    /// 
    /// 
    public static void SetStatusBarColor(int color, bool isBlack)
    {
        if (Window != null)
        {
            RunOnAndroidUiThread(() =>
            {
                if (Window != null)
                {
                    Window.Call("setStatusBarColor", color);
                }
            });
        }

        if (isBlack)
        {
            RunOnAndroidUiThread(() =>
            {
                if (View != null)
                {
                    View.Call("setSystemUiVisibility", 8192);
                }
            });
        }
        else
        {
            RunOnAndroidUiThread(() =>
            {
                if (View != null)
                {
                    View.Call("setSystemUiVisibility", 256);
                }
            });
        }
    }

    /// 
    /// 运行
    /// 
    /// 
    private static void RunOnAndroidUiThread(Action target)
    {
        AndroidJavaObject activity = Activity;
        if (activity != null)
        {
            activity.Call("runOnUiThread", new AndroidJavaRunnable(target));
        }
    }

    /// 
    /// 将颜色转为int
    /// 
    /// 
    /// 
    public static int ConvertColorToAndroidColor(Color color)
    {
        Color32 color32 = color;
        int alpha = color32.a;
        int red = color32.r;
        int green = color32.g;
        int blue = color32.b;
        using (var colorClass = new AndroidJavaClass("android.graphics.Color"))
        {
            int androidColor = colorClass.CallStatic("argb", alpha, red, green, blue);
            return androidColor;
        }
    }
}

使用方法如下: 

int color = ApplicationChrome.ConvertColorToAndroidColor(ColorConversion.HexToColor("00000000"));
ApplicationChrome.SetStatusBarColor(color, true);

 ColorConversion.HexToColor是一个将字符串转颜色的函数

    /// 
    /// hex转换到color
    /// 
    /// 
    /// 
    public static Color HexToColor(string hex)
    {
        byte br = byte.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
        byte bg = byte.Parse(hex.Substring(2, 2), System.Globalization.NumberStyles.HexNumber);
        byte bb = byte.Parse(hex.Substring(4, 2), System.Globalization.NumberStyles.HexNumber);
        byte cc = byte.Parse(hex.Substring(6, 2), System.Globalization.NumberStyles.HexNumber);
        float r = br / 255f;
        float g = bg / 255f;
        float b = bb / 255f;
        float a = cc / 255f;
        return new Color(r, g, b, a);
    }

 

————————————————————————————————

版权声明
版权声明:本博客为非营利性个人原创
所刊登的所有作品的著作权均为本人所拥有
本人保留所有法定权利,违者必究!
对于需要复制、转载、链接和传播博客文章或内容的
请及时和本博主进行联系
对于经本博主明确授权和许可使用文章及内容的
使用时请注明文章或内容出处并注明网址
转载请附上原文出处链接及本声明

你可能感兴趣的:(android,unity)