AR开发实战Vuforia项目之与美少女一起跳舞(实现MMD功能)

一、框架视图

二、关键代码

AndroidStatusBar

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

/**
 * @author zeh fernando
 * @modify MemoryC_2017-02-05
 */
class AndroidStatusBar
{

    /**
 * Manipulates the system application chrome to change the way the status bar and navigation bar work
 *POSTS FROM :
 *http://zehfernando.com/2015/unity-tidbits-changing-the-visibility-of-androids-navigation-and-status-bars-and-implementing-immersive-mode/
 *[url=http://www.manew.com/thread-100054-1-1.html]http://www.manew.com/thread-100054-1-1.html[/url]
 * References:
 * . http://developer.android.com/reference/android/view/View.html#setSystemUiVisibility(int)
 * . http://forum.unity3d.com/threads/calling-setsystemuivisibility.139445/#post-952946
 * . http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_LAYOUT_IN_SCREEN
 **/

    // Enums
    public enum States
    {
        Unknown,
        Visible,
        VisibleOverContent,
        TranslucentOverContent,
        Hidden,
    }

    // Constants
    private const uint DEFAULT_BACKGROUND_COLOR = 0xff000000;

#if UNITY_ANDROID
    // Original Android flags
    private const int VIEW_SYSTEM_UI_FLAG_VISIBLE = 0;                                        // Added in API 14 (Android 4.0.x): Status bar visible (the default)
    private const int VIEW_SYSTEM_UI_FLAG_LOW_PROFILE = 1;                                // Added in API 14 (Android 4.0.x): Low profile for games, book readers, and video players; the status bar and/or navigation icons are dimmed out (if visible)
    private const int VIEW_SYSTEM_UI_FLAG_HIDE_NAVIGATION = 2;                        // Added in API 14 (Android 4.0.x): Hides all navigation. Cleared when theres any user interaction.
    private const int VIEW_SYSTEM_UI_FLAG_FULLSCREEN = 4;                                // Added in API 16 (Android 4.1.x): Hides status bar. Does nothing in Unity (already hidden if "status bar hidden" is checked)
    private const int VIEW_SYSTEM_UI_FLAG_LAYOUT_STABLE = 256;                        // Added in API 16 (Android 4.1.x): ?
    private const int VIEW_SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION = 512;        // Added in API 16 (Android 4.1.x): like HIDE_NAVIGATION, but for layouts? it causes the layout to be drawn like that, even if the whole view isn't (to avoid artifacts in animation)
    private const int VIEW_SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN = 1024;                // Added in API 16 (Android 4.1.x): like FULLSCREEN, but for layouts? it causes the layout to be drawn like that, even if the whole view isn't (to avoid artifacts in animation)
    private const int VIEW_SYSTEM_UI_FLAG_IMMERSIVE = 2048;                                // Added in API 19 (Android 4.4): like HIDE_NAVIGATION, but interactive (it's a modifier for HIDE_NAVIGATION, needs to be used with it)
    private const int VIEW_SYSTEM_UI_FLAG_IMMERSIVE_STICKY = 4096;                // Added in API 19 (Android 4.4): tells that HIDE_NAVIGATION and FULSCREEN are interactive (also just a modifier)

    private static int WINDOW_FLAG_FULLSCREEN = 0x00000400;
    private static int WINDOW_FLAG_FORCE_NOT_FULLSCREEN = 0x00000800;
    private static int WINDOW_FLAG_LAYOUT_IN_SCREEN = 0x00000100;
    private static int WINDOW_FLAG_TRANSLUCENT_STATUS = 0x04000000;
    private static int WINDOW_FLAG_TRANSLUCENT_NAVIGATION = 0x08000000;
    private static int WINDOW_FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS = -2147483648; // 0x80000000; // Added in API 21 (Android 5.0): tells the Window is responsible for drawing the background for the system bars. If set, the system bars are drawn with a transparent background and the corresponding areas in this window are filled with the colors specified in getStatusBarColor() and getNavigationBarColor()

    // Current values
    private static int systemUiVisibilityValue;
    private static int flagsValue;
#endif

    // Properties
    private static States _statusBarState;
    //        private static States _navigationBarState;

    private static uint _statusBarColor = DEFAULT_BACKGROUND_COLOR;
    //        private static uint _navigationBarColor = DEFAULT_BACKGROUND_COLOR;

    private static bool _isStatusBarTranslucent; // Just so we know whether its translucent when hidden or not
                                                 //        private static bool _isNavigationBarTranslucent;

    private static bool _dimmed;
    // ================================================================================================================
    // INTERNAL INTERFACE ---------------------------------------------------------------------------------------------

    static AndroidStatusBar()
    {
        applyUIStates();
        applyUIColors();
    }

    private static void applyUIStates()
    {
#if UNITY_ANDROID && !UNITY_EDITOR
 
                int newFlagsValue = 0;
                int newSystemUiVisibilityValue = 0;
 
                // Apply dim values
                if (_dimmed) newSystemUiVisibilityValue |= VIEW_SYSTEM_UI_FLAG_LOW_PROFILE;
 
                // Apply color values
//                if (_navigationBarColor != DEFAULT_BACKGROUND_COLOR || _statusBarColor != DEFAULT_BACKGROUND_COLOR) newFlagsValue |= WINDOW_FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS;
                if (_statusBarColor != DEFAULT_BACKGROUND_COLOR) newFlagsValue |= WINDOW_FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS;
 
                // Apply status bar values
                switch (_statusBarState) {
                case States.Visible:
                _isStatusBarTranslucent = false;
                newFlagsValue |= WINDOW_FLAG_FORCE_NOT_FULLSCREEN;
                break;
                case States.VisibleOverContent:
                _isStatusBarTranslucent = false;
                newFlagsValue |= WINDOW_FLAG_FORCE_NOT_FULLSCREEN | WINDOW_FLAG_LAYOUT_IN_SCREEN;
                newSystemUiVisibilityValue |= VIEW_SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
                break;
                case States.TranslucentOverContent:
                _isStatusBarTranslucent = true;
                newFlagsValue |= WINDOW_FLAG_FORCE_NOT_FULLSCREEN | WINDOW_FLAG_LAYOUT_IN_SCREEN | WINDOW_FLAG_TRANSLUCENT_STATUS;
                newSystemUiVisibilityValue |= VIEW_SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
                break;
                case States.Hidden:
                newFlagsValue |= WINDOW_FLAG_FULLSCREEN | WINDOW_FLAG_LAYOUT_IN_SCREEN;
                if (_isStatusBarTranslucent) newFlagsValue |= WINDOW_FLAG_TRANSLUCENT_STATUS;
                break;
                }
 
                // Applies navigation values
                /*
                switch (_navigationBarState) {
                case States.Visible:
                _isNavigationBarTranslucent = false;
                newSystemUiVisibilityValue |= VIEW_SYSTEM_UI_FLAG_LAYOUT_STABLE;
                break;
                case States.VisibleOverContent:
                // TODO: Side effect: forces status bar over content if set to VISIBLE
                _isNavigationBarTranslucent = false;
                newSystemUiVisibilityValue |= VIEW_SYSTEM_UI_FLAG_LAYOUT_STABLE | VIEW_SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
                break;
                case States.TranslucentOverContent:
                // TODO: Side effect: forces status bar over content if set to VISIBLE
                _isNavigationBarTranslucent = true;
                newFlagsValue |= WINDOW_FLAG_TRANSLUCENT_NAVIGATION;
                newSystemUiVisibilityValue |= VIEW_SYSTEM_UI_FLAG_LAYOUT_STABLE | VIEW_SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
                break;
                case States.Hidden:
                newSystemUiVisibilityValue |= VIEW_SYSTEM_UI_FLAG_FULLSCREEN | VIEW_SYSTEM_UI_FLAG_HIDE_NAVIGATION | VIEW_SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
                if (_isNavigationBarTranslucent) newFlagsValue |= WINDOW_FLAG_TRANSLUCENT_NAVIGATION;
                break;
                }
                */
                if (Screen.fullScreen) Screen.fullScreen = false;
 
                // Applies everything natively
                setFlags(newFlagsValue);
                setSystemUiVisibility(newSystemUiVisibilityValue);
#endif
    }

    private static void applyUIColors()
    {
#if UNITY_ANDROID && !UNITY_EDITOR
                runOnAndroidUiThread(applyUIColorsAndroidInThread);
#endif
    }

#if UNITY_ANDROID
    private static void runOnAndroidUiThread(Action target)
    {
        using (var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
        {
            using (var activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
            {
                activity.Call("runOnUiThread", new AndroidJavaRunnable(target));
            }
        }
    }

    private static void setSystemUiVisibility(int value)
    {
        if (systemUiVisibilityValue != value)
        {
            systemUiVisibilityValue = value;
            runOnAndroidUiThread(setSystemUiVisibilityInThread);
        }
    }

    private static void setSystemUiVisibilityInThread()
    {
        //Debug.Log("SYSTEM FLAGS: " + systemUiVisibilityValue);
        using (var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
        {
            using (var activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
            {
                using (var window = activity.Call<AndroidJavaObject>("getWindow"))
                {
                    using (var view = window.Call<AndroidJavaObject>("getDecorView"))
                    {
                        view.Call("setSystemUiVisibility", systemUiVisibilityValue);
                    }
                }
            }
        }
    }

    private static void setFlags(int value)
    {
        if (flagsValue != value)
        {
            flagsValue = value;
            runOnAndroidUiThread(setFlagsInThread);
        }
    }

    private static void setFlagsInThread()
    {
        //Debug.Log("FLAGS: " + flagsValue);
        using (var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
        {
            using (var activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
            {
                using (var window = activity.Call<AndroidJavaObject>("getWindow"))
                {
                    window.Call("setFlags", flagsValue, -1); // (int)0x7FFFFFFF
                }
            }
        }
    }

    private static void applyUIColorsAndroidInThread()
    {
        using (var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
        {
            using (var activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
            {
                using (var window = activity.Call<AndroidJavaObject>("getWindow"))
                {
                    //Debug.Log("Colors SET: " + _statusBarColor);
                    window.Call("setStatusBarColor", unchecked((int)_statusBarColor));
                    //                                        window.Call("setNavigationBarColor", unchecked((int)_navigationBarColor));
                }
            }
        }
    }

#endif

    // ================================================================================================================
    // ACCESSOR INTERFACE ---------------------------------------------------------------------------------------------
    /*
    public static States navigationBarState {
            get { return _navigationBarState; }
            set {
                    if (_navigationBarState != value) {
                            _navigationBarState = value;
                            applyUIStates();
                    }
            }
    }
*/
    public static States statusBarState
    {
        get { return _statusBarState; }
        set
        {
            if (_statusBarState != value)
            {
                _statusBarState = value;
                applyUIStates();
            }
        }
    }

    public static bool dimmed
    {
        get { return _dimmed; }
        set
        {
            if (_dimmed != value)
            {
                _dimmed = value;
                applyUIStates();
            }
        }
    }

    public static uint statusBarColor
    {
        get { return _statusBarColor; }
        set
        {
            if (_statusBarColor != value)
            {
                _statusBarColor = value;
                applyUIColors();
                applyUIStates();
            }
        }
    }
    /*
    public static uint navigationBarColor {
            get { return _navigationBarColor; }
            set {
                    if (_navigationBarColor != value) {
                            _navigationBarColor = value;
                            applyUIColors();
                            applyUIStates();
                    }
            }
    }
    */
}

ButtonMessage

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;


/// <summary>
/// 异步加载
/// </summary>
public class ButtonMessage : MonoBehaviour
{
    //滑动条
    public Slider progress;
    //显示值

    public Text progressValue;

    //异步加载的场景
    private AsyncOperation op;

    void Awake()
    {
    }

    void Start() {

            op = SceneManager.LoadSceneAsync("Gril");
            //开启进度条
            StartCoroutine(LoadingScene());       
    }
    /// <summary>
    /// 设置进度值
    /// </summary>
    /// <param name="value">参数名</param>
    private void setProgressValue(int value)
    {
        //获取进度条的值
        progress.value = value;

        //显示进度条的值
        progressValue.text = value + "%";
    }

    private IEnumerator LoadingScene()
    {
        int displayProgress = 0;

        int toProgress = 0;

        op.allowSceneActivation = false; //不允许自动加载场景

        while (op.progress < 0.9f)
        {
            toProgress = (int)op.progress * 100;
            while (displayProgress < toProgress)
            {
                ++displayProgress;
                setProgressValue(displayProgress);
                yield return new WaitForEndOfFrame();
            }               
        }

        toProgress = 100;

        while (displayProgress < toProgress)
        {
            ++displayProgress;
            setProgressValue(displayProgress);
            yield return new WaitForEndOfFrame();
        }

        op.allowSceneActivation = true;
    }

   
}

CameraMode

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


/// <summary>
/// 自动对焦功能
/// </summary>
public class CameraMode : MonoBehaviour {


    void Start () {
        //一开始自动对焦
        //Vuforia.CameraDevice.Instance.SetFocusMode(Vuforia.CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
        VuforiaARController.Instance.RegisterVuforiaStartedCallback(OnVuforiaStarted);
        VuforiaARController.Instance.RegisterOnPauseCallback(OnPaused);
    }

    void Update () {
        //触碰的时候对焦
        //if (Input.GetMouseButtonUp(0))
        //{
        //    if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
        //    {
        //        Vuforia.CameraDevice.Instance.SetFocusMode(Vuforia.CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
        //    }
        //}

    }
    private void OnVuforiaStarted()
    {
            CameraDevice.Instance.SetFocusMode(
            CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
    }

    private void OnPaused(bool paused)
    {
        if (!paused)
        { // resumed
            // Set again autofocus mode when app is resumed
            CameraDevice.Instance.SetFocusMode(
            CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
        }
    }
}

MyTrackableEventHandler

/*==============================================================================
Copyright (c) 2010-2014 Qualcomm Connected Experiences, Inc.
All Rights Reserved.
Confidential and Proprietary - Protected under copyright and other laws.
==============================================================================*/

using UnityEngine;

namespace Vuforia
{
    /// <summary>
    /// A custom handler that implements the ITrackableEventHandler interface.
    /// </summary>
    public class MyTrackableEventHandler : MonoBehaviour,
                                                ITrackableEventHandler
    {

        //定义音频
        private AudioSource music;

        //定义动画状态机
        private Animator mAnimator;

        #region PRIVATE_MEMBER_VARIABLES

        private TrackableBehaviour mTrackableBehaviour;

        #endregion // PRIVATE_MEMBER_VARIABLES



        #region UNTIY_MONOBEHAVIOUR_METHODS

        void Start()
        {
            mTrackableBehaviour = GetComponent<TrackableBehaviour>();
            if (mTrackableBehaviour)
            {
                mTrackableBehaviour.RegisterTrackableEventHandler(this);
            }
            //获取音频组件;
            music = this.GetComponent<AudioSource>();

            //获取动画上组件
            mAnimator = GameObject.FindGameObjectWithTag("Gril").GetComponent<Animator>();
        }

        #endregion // UNTIY_MONOBEHAVIOUR_METHODS



        #region PUBLIC_METHODS

        /// <summary>
        /// Implementation of the ITrackableEventHandler function called when the
        /// tracking state changes.
        /// </summary>
        public void OnTrackableStateChanged(
                                        TrackableBehaviour.Status previousStatus,
                                        TrackableBehaviour.Status newStatus)
        {
            if (newStatus == TrackableBehaviour.Status.DETECTED ||
                newStatus == TrackableBehaviour.Status.TRACKED ||
                newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
            {
                OnTrackingFound();
            }
            else
            {
                OnTrackingLost();
            }
        }

        #endregion // PUBLIC_METHODS



        #region PRIVATE_METHODS


        private void OnTrackingFound()
        {
            Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
            Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);

            // Enable rendering:
            foreach (Renderer component in rendererComponents)
            {
                component.enabled = true;
            }

            // Enable colliders:
            foreach (Collider component in colliderComponents)
            {
                component.enabled = true;
            }

            Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " found");
            //判断是否在播放 没播放就播放 有播放就把音量调到1;
            if (!music.isPlaying)
            {
                //播放音乐;
                music.Play();
                music.volume = 1;
            }
            else if (music.isPlaying)
            {
                music.volume = 1;
            }

            //播放动画;
            mAnimator.enabled = true;
            mAnimator.speed = 1;
        }


        private void OnTrackingLost()
        {
            Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
            Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);

            // Disable rendering:
            foreach (Renderer component in rendererComponents)
            {
                component.enabled = false;
            }

            // Disable colliders:
            foreach (Collider component in colliderComponents)
            {
                component.enabled = false;
            }

            Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " lost");
            //获取音频组件;
            music = this.GetComponent<AudioSource>();
            //暂停音乐
           // music.Pause();
            music.volume = 0;

            //获取动画上组件
            mAnimator = GameObject.FindGameObjectWithTag("Gril").GetComponent<Animator>();
            //暂停动画;
            mAnimator.speed = 0;
        }

        #endregion // PRIVATE_METHODS
    }
}



SpringBone

using UnityEngine;
using System.Collections;

namespace UnityChan
{
    public class SpringBone : MonoBehaviour
    {
        
        public Transform child;

        
        public Vector3 boneAxis = new Vector3 (-1.0f, 0.0f, 0.0f);
        public float radius = 0.05f;

        
        public bool isUseEachBoneForceSettings = false; 

        
        public float stiffnessForce = 0.01f;

    
        public float dragForce = 0.4f;
        public Vector3 springForce = new Vector3 (0.0f, -0.0001f, 0.0f);
        public SpringCollider[] colliders;
        public bool debug = true;
        
        public float threshold = 0.01f;
        private float springLength;
        private Quaternion localRotation;
        private Transform trs;
        private Vector3 currTipPos;
        private Vector3 prevTipPos;
        
        private Transform org;
        
        private SpringManager managerRef;

        private void Awake ()
        {
            trs = transform;
            localRotation = transform.localRotation;
            
            managerRef = GetParentSpringManager (transform);
        }

        private SpringManager GetParentSpringManager (Transform t)
        {
            var springManager = t.GetComponent<SpringManager> ();

            if (springManager != null)
                return springManager;

            if (t.parent != null) {
                return GetParentSpringManager (t.parent);
            }

            return null;
        }

        private void Start ()
        {
            springLength = Vector3.Distance (trs.position, child.position);
            currTipPos = child.position;
            prevTipPos = child.position;
        }

        public void UpdateSpring ()
        {
            
            org = trs;
            
            trs.localRotation = Quaternion.identity * localRotation;

            float sqrDt = Time.deltaTime * Time.deltaTime;

            //stiffness
            Vector3 force = trs.rotation * (boneAxis * stiffnessForce) / sqrDt;

            //drag
            force += (prevTipPos - currTipPos) * dragForce / sqrDt;

            force += springForce / sqrDt;

            
            Vector3 temp = currTipPos;

            //verlet
            currTipPos = (currTipPos - prevTipPos) + currTipPos + (force * sqrDt);

            
            currTipPos = ((currTipPos - trs.position).normalized * springLength) + trs.position;

            
            for (int i = 0; i < colliders.Length; i++) {
                if (Vector3.Distance (currTipPos, colliders [i].transform.position) <= (radius + colliders [i].radius)) {
                    Vector3 normal = (currTipPos - colliders [i].transform.position).normalized;
                    currTipPos = colliders [i].transform.position + (normal * (radius + colliders [i].radius));
                    currTipPos = ((currTipPos - trs.position).normalized * springLength) + trs.position;
                }


            }

            prevTipPos = temp;

            
            Vector3 aimVector = trs.TransformDirection (boneAxis);
            Quaternion aimRotation = Quaternion.FromToRotation (aimVector, currTipPos - trs.position);
            
            Quaternion secondaryRotation = aimRotation * trs.rotation;
            trs.rotation = Quaternion.Lerp (org.rotation, secondaryRotation, managerRef.dynamicRatio);
        }

        private void OnDrawGizmos ()
        {
            if (debug) {
                Gizmos.color = Color.yellow;
                Gizmos.DrawWireSphere (currTipPos, radius);
            }
        }
    }
}

SpringCollider

using UnityEngine;
using System.Collections;

namespace UnityChan
{
    public class SpringCollider : MonoBehaviour
    {
        //半径
        public float radius = 0.5f;

        private void OnDrawGizmosSelected ()
        {
            Gizmos.color = Color.green;
            Gizmos.DrawWireSphere (transform.position, radius);
        }
    }
}

SpringManager

using UnityEngine;
using System.Collections;

namespace UnityChan
{
    public class SpringManager : MonoBehaviour
    {
        //Kobayashi
        // DynamicRatio is paramater for activated level of dynamic animation 
        public float dynamicRatio = 1.0f;

        //Ebata
        public float            stiffnessForce;
        public AnimationCurve   stiffnessCurve;
        public float            dragForce;
        public AnimationCurve   dragCurve;
        public SpringBone[] springBones;

        void Start ()
        {
            UpdateParameters ();
        }
    
        void Update ()
        {
#if UNITY_EDITOR
        
        if(dynamicRatio >= 1.0f)
            dynamicRatio = 1.0f;
        else if(dynamicRatio <= 0.0f)
            dynamicRatio = 0.0f;
        //Ebata
        UpdateParameters();
#endif
        }
    
        private void LateUpdate ()
        {
            
            if (dynamicRatio != 0.0f) {
                for (int i = 0; i < springBones.Length; i++) {
                    if (dynamicRatio > springBones [i].threshold) {
                        springBones [i].UpdateSpring ();
                    }
                }
            }
        }

        private void UpdateParameters ()
        {
            UpdateParameter ("stiffnessForce", stiffnessForce, stiffnessCurve);
            UpdateParameter ("dragForce", dragForce, dragCurve);
        }
    
        private void UpdateParameter (string fieldName, float baseValue, AnimationCurve curve)
        {
            var start = curve.keys [0].time;
            var end = curve.keys [curve.length - 1].time;
            //var step  = (end - start) / (springBones.Length - 1);
        
            var prop = springBones [0].GetType ().GetField (fieldName, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
        
            for (int i = 0; i < springBones.Length; i++) {
                
                if (!springBones [i].isUseEachBoneForceSettings) {
                    var scale = curve.Evaluate (start + (end - start) * i / (springBones.Length - 1));
                    prop.SetValue (springBones [i], baseValue * scale);
                }
            }
        }
    }
}

StateTest

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

/// <summary>
/// 显示手机状态栏的类
/// </summary>
public class StateTest : MonoBehaviour {

    
    /// <summary>
    /// 保留手机状态栏,必须放在awake方法中去
    /// </summary>
    void Awake() {
        
        AndroidStatusBar.statusBarState = AndroidStatusBar.States.TranslucentOverContent;
    }


StatusBarTest

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class StatusBarTest : MonoBehaviour
{
    //public Button toggleDimmedButton;
    //public Button visibleButton;
    //public Button visibleOverContentButton;
    //public Button translucentOverContentButton;
    //public Button hiddenButton;
    // Use this for initialization
    void Start()
    {
        ////当AndroidStatusBar.dimmed=false时,状态栏显示所有状态及通知图标
        ////当AndroidStatusBar.dimmed=true时,状态栏仅电量和时间,不显示其他状态及通知
        //if (toggleDimmedButton != null)
        //{
        //    toggleDimmedButton.onClick.AddListener(delegate {
        //        AndroidStatusBar.dimmed = !AndroidStatusBar.dimmed;
        //    });
        //}
        ////显示状态栏,占用屏幕最上方的一部分像素
        //if (visibleButton != null)
        //{
        //    visibleButton.onClick.AddListener(delegate {
        //       AndroidStatusBar.statusBarState = AndroidStatusBar.States.Visible;
        //    });
        //}
        //悬浮显示状态栏,不占用屏幕像素
        //if (visibleOverContentButton != null)
        //{
        //    visibleOverContentButton.onClick.AddListener(delegate {
                //AndroidStatusBar.statusBarState = AndroidStatusBar.States.VisibleOverContent;
        //    });
        //}
    //    //透明悬浮显示状态栏,不占用屏幕像素
    //    if (translucentOverContentButton != null)
    //    {
    //        translucentOverContentButton.onClick.AddListener(delegate {
                AndroidStatusBar.statusBarState = AndroidStatusBar.States.TranslucentOverContent;
    //        });
    //    }
    //    //隐藏状态栏
    //    if (hiddenButton != null)
    //    {
    //        hiddenButton.onClick.AddListener(delegate {
    //            AndroidStatusBar.statusBarState = AndroidStatusBar.States.Hidden;
    //        });
    //    }
    }

}

三、效果展示

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,294评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,493评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,790评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,595评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,718评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,906评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,053评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,797评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,250评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,570评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,711评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,388评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,018评论 3 316
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,796评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,023评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,461评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,595评论 2 350

推荐阅读更多精彩内容