VR開發(fā)實(shí)戰(zhàn)HTC Vive項(xiàng)目之彈幕涂鴉(事件廣播與監(jiān)聽)

一骨宠、框架視圖

二恍涂、主要代碼

Common:

CallBack

public delegate void CallBack();
public delegate void CallBack<T>(T arg);
public delegate void CallBack<T, X>(T arg1, X arg2);
public delegate void CallBack<T, X, Y>(T arg1, X arg2, Y arg3);
public delegate void CallBack<T, X, Y, Z>(T arg1, X arg2, Y arg3, Z arg4);
public delegate void CallBack<T, X, Y, Z, W>(T arg1, X arg2, Y arg3, Z arg4, W arg5);

EventCenter

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

public class EventCenter
{
    private static Dictionary<EventDefine, Delegate> m_EventTable = new Dictionary<EventDefine, Delegate>();

    private static void OnListenerAdding(EventDefine EventDefine, Delegate callBack)
    {
        if (!m_EventTable.ContainsKey(EventDefine))
        {
            m_EventTable.Add(EventDefine, null);
        }
        Delegate d = m_EventTable[EventDefine];
        if (d != null && d.GetType() != callBack.GetType())
        {
            throw new Exception(string.Format("嘗試為事件{0}添加不同類型的委托捉邢,當(dāng)前事件所對應(yīng)的委托是{1}康震,要添加的委托類型為{2}", EventDefine, d.GetType(), callBack.GetType()));
        }
    }
    private static void OnListenerRemoving(EventDefine EventDefine, Delegate callBack)
    {
        if (m_EventTable.ContainsKey(EventDefine))
        {
            Delegate d = m_EventTable[EventDefine];
            if (d == null)
            {
                throw new Exception(string.Format("移除監(jiān)聽錯誤:事件{0}沒有對應(yīng)的委托", EventDefine));
            }
            else if (d.GetType() != callBack.GetType())
            {
                throw new Exception(string.Format("移除監(jiān)聽錯誤:嘗試為事件{0}移除不同類型的委托,當(dāng)前委托類型為{1}提鸟,要移除的委托類型為{2}", EventDefine, d.GetType(), callBack.GetType()));
            }
        }
        else
        {
            throw new Exception(string.Format("移除監(jiān)聽錯誤:沒有事件碼{0}", EventDefine));
        }
    }
    private static void OnListenerRemoved(EventDefine EventDefine)
    {
        if (m_EventTable[EventDefine] == null)
        {
            m_EventTable.Remove(EventDefine);
        }
    }
    //no parameters
    public static void AddListener(EventDefine EventDefine, CallBack callBack)
    {
        OnListenerAdding(EventDefine, callBack);
        m_EventTable[EventDefine] = (CallBack)m_EventTable[EventDefine] + callBack;
    }
    //Single parameters
    public static void AddListener<T>(EventDefine EventDefine, CallBack<T> callBack)
    {
        OnListenerAdding(EventDefine, callBack);
        m_EventTable[EventDefine] = (CallBack<T>)m_EventTable[EventDefine] + callBack;
    }
    //two parameters
    public static void AddListener<T, X>(EventDefine EventDefine, CallBack<T, X> callBack)
    {
        OnListenerAdding(EventDefine, callBack);
        m_EventTable[EventDefine] = (CallBack<T, X>)m_EventTable[EventDefine] + callBack;
    }
    //three parameters
    public static void AddListener<T, X, Y>(EventDefine EventDefine, CallBack<T, X, Y> callBack)
    {
        OnListenerAdding(EventDefine, callBack);
        m_EventTable[EventDefine] = (CallBack<T, X, Y>)m_EventTable[EventDefine] + callBack;
    }
    //four parameters
    public static void AddListener<T, X, Y, Z>(EventDefine EventDefine, CallBack<T, X, Y, Z> callBack)
    {
        OnListenerAdding(EventDefine, callBack);
        m_EventTable[EventDefine] = (CallBack<T, X, Y, Z>)m_EventTable[EventDefine] + callBack;
    }
    //five parameters
    public static void AddListener<T, X, Y, Z, W>(EventDefine EventDefine, CallBack<T, X, Y, Z, W> callBack)
    {
        OnListenerAdding(EventDefine, callBack);
        m_EventTable[EventDefine] = (CallBack<T, X, Y, Z, W>)m_EventTable[EventDefine] + callBack;
    }

    //no parameters
    public static void RemoveListener(EventDefine EventDefine, CallBack callBack)
    {
        OnListenerRemoving(EventDefine, callBack);
        m_EventTable[EventDefine] = (CallBack)m_EventTable[EventDefine] - callBack;
        OnListenerRemoved(EventDefine);
    }
    //single parameters
    public static void RemoveListener<T>(EventDefine EventDefine, CallBack<T> callBack)
    {
        OnListenerRemoving(EventDefine, callBack);
        m_EventTable[EventDefine] = (CallBack<T>)m_EventTable[EventDefine] - callBack;
        OnListenerRemoved(EventDefine);
    }
    //two parameters
    public static void RemoveListener<T, X>(EventDefine EventDefine, CallBack<T, X> callBack)
    {
        OnListenerRemoving(EventDefine, callBack);
        m_EventTable[EventDefine] = (CallBack<T, X>)m_EventTable[EventDefine] - callBack;
        OnListenerRemoved(EventDefine);
    }
    //three parameters
    public static void RemoveListener<T, X, Y>(EventDefine EventDefine, CallBack<T, X, Y> callBack)
    {
        OnListenerRemoving(EventDefine, callBack);
        m_EventTable[EventDefine] = (CallBack<T, X, Y>)m_EventTable[EventDefine] - callBack;
        OnListenerRemoved(EventDefine);
    }
    //four parameters
    public static void RemoveListener<T, X, Y, Z>(EventDefine EventDefine, CallBack<T, X, Y, Z> callBack)
    {
        OnListenerRemoving(EventDefine, callBack);
        m_EventTable[EventDefine] = (CallBack<T, X, Y, Z>)m_EventTable[EventDefine] - callBack;
        OnListenerRemoved(EventDefine);
    }
    //five parameters
    public static void RemoveListener<T, X, Y, Z, W>(EventDefine EventDefine, CallBack<T, X, Y, Z, W> callBack)
    {
        OnListenerRemoving(EventDefine, callBack);
        m_EventTable[EventDefine] = (CallBack<T, X, Y, Z, W>)m_EventTable[EventDefine] - callBack;
        OnListenerRemoved(EventDefine);
    }


    //no parameters
    public static void Broadcast(EventDefine EventDefine)
    {
        Delegate d;
        if (m_EventTable.TryGetValue(EventDefine, out d))
        {
            CallBack callBack = d as CallBack;
            if (callBack != null)
            {
                callBack();
            }
            else
            {
                throw new Exception(string.Format("廣播事件錯誤:事件{0}對應(yīng)委托具有不同的類型", EventDefine));
            }
        }
    }
    //single parameters
    public static void Broadcast<T>(EventDefine EventDefine, T arg)
    {
        Delegate d;
        if (m_EventTable.TryGetValue(EventDefine, out d))
        {
            CallBack<T> callBack = d as CallBack<T>;
            if (callBack != null)
            {
                callBack(arg);
            }
            else
            {
                throw new Exception(string.Format("廣播事件錯誤:事件{0}對應(yīng)委托具有不同的類型", EventDefine));
            }
        }
    }
    //two parameters
    public static void Broadcast<T, X>(EventDefine EventDefine, T arg1, X arg2)
    {
        Delegate d;
        if (m_EventTable.TryGetValue(EventDefine, out d))
        {
            CallBack<T, X> callBack = d as CallBack<T, X>;
            if (callBack != null)
            {
                callBack(arg1, arg2);
            }
            else
            {
                throw new Exception(string.Format("廣播事件錯誤:事件{0}對應(yīng)委托具有不同的類型", EventDefine));
            }
        }
    }
    //three parameters
    public static void Broadcast<T, X, Y>(EventDefine EventDefine, T arg1, X arg2, Y arg3)
    {
        Delegate d;
        if (m_EventTable.TryGetValue(EventDefine, out d))
        {
            CallBack<T, X, Y> callBack = d as CallBack<T, X, Y>;
            if (callBack != null)
            {
                callBack(arg1, arg2, arg3);
            }
            else
            {
                throw new Exception(string.Format("廣播事件錯誤:事件{0}對應(yīng)委托具有不同的類型", EventDefine));
            }
        }
    }
    //four parameters
    public static void Broadcast<T, X, Y, Z>(EventDefine EventDefine, T arg1, X arg2, Y arg3, Z arg4)
    {
        Delegate d;
        if (m_EventTable.TryGetValue(EventDefine, out d))
        {
            CallBack<T, X, Y, Z> callBack = d as CallBack<T, X, Y, Z>;
            if (callBack != null)
            {
                callBack(arg1, arg2, arg3, arg4);
            }
            else
            {
                throw new Exception(string.Format("廣播事件錯誤:事件{0}對應(yīng)委托具有不同的類型", EventDefine));
            }
        }
    }
    //five parameters
    public static void Broadcast<T, X, Y, Z, W>(EventDefine EventDefine, T arg1, X arg2, Y arg3, Z arg4, W arg5)
    {
        Delegate d;
        if (m_EventTable.TryGetValue(EventDefine, out d))
        {
            CallBack<T, X, Y, Z, W> callBack = d as CallBack<T, X, Y, Z, W>;
            if (callBack != null)
            {
                callBack(arg1, arg2, arg3, arg4, arg5);
            }
            else
            {
                throw new Exception(string.Format("廣播事件錯誤:事件{0}對應(yīng)委托具有不同的類型", EventDefine));
            }
        }
    }
}

EventDefine

public enum EventDefine
{
    IsShowCharacterChoosePanel,
    IsShowStartPanel,
    OnCharacterChoose,
    StartLoadScene,
    HintOrShowButtonClick,
    Hint,
    SpawnTextTag,
    IsActiveKeyboard,
    IsActiveTagParent,
    IsActiveTextureTagPanel,
    LeftControllerPulse,
    RightControllerPulse,
    IsActiveDraw,
}

ResourcesManager

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

public class ResourcesManager
{
    private static Dictionary<string, object> m_ObjDic = new Dictionary<string, object>();

    public static Sprite LoadSprite(string spriteName)
    {
        Sprite temp;
        if (!m_ObjDic.ContainsKey(spriteName))
        {
            temp = Resources.Load<Sprite>(spriteName);
            m_ObjDic.Add(spriteName, temp);
        }
        else
        {
            temp = m_ObjDic[spriteName] as Sprite;
        }
        return temp;
    }
    public static GameObject LoadObj(string objName)
    {
        GameObject temp;
        if (m_ObjDic.ContainsKey(objName) == false)
        {
            temp = Resources.Load<GameObject>(objName);
            m_ObjDic.Add(objName, temp);
        }
        else
        {
            temp = m_ObjDic[objName] as GameObject;
        }
        return temp;
    }
    public static Sprite LoadEmoji(string name)
    {
        Sprite temp = null;
        Sprite[] arr = Resources.LoadAll<Sprite>("圖片標(biāo)簽/emoji");
        for (int i = 0; i < arr.Length; i++)
        {
            if (arr[i].name == name)
            {
                temp = arr[i];
            }
        }
        return temp;
    }
}

Game:

CursorCtrl

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

public class CursorCtrl : MonoBehaviour
{
    private void OnCollisionStay(Collision collision)
    {
        if (collision.collider.tag == "MarkPen")
        {
            ContactPoint[] points = collision.contacts;
            for (int i = 0; i < points.Length; i++)
            {
                transform.position = points[i].point;
            }
        }
    }
}

IKBind

using RootMotion.FinalIK;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class IKBind : MonoBehaviour
{
    private VRIK vrik;
    private bool isLeftArm = false;
    private bool isRightArm = false;

    private void Awake()
    {
        vrik = GetComponent<VRIK>();
    }
    private void Start()
    {
        vrik.solver.spine.headTarget = GameObject.FindGameObjectWithTag("NickHead").transform;
    }
    private void Update()
    {
        if (GameObject.FindGameObjectWithTag("LArmHand") != null)
        {
            if (isLeftArm == false)
            {
                vrik.solver.leftArm.target = GameObject.FindGameObjectWithTag("LArmHand").transform;
                isLeftArm = true;
            }
        }
        if (GameObject.FindGameObjectWithTag("RArmHand") != null)
        {
            if (isRightArm == false)
            {
                vrik.solver.rightArm.target = GameObject.FindGameObjectWithTag("RArmHand").transform;
                isRightArm = true;
            }
        }
    }
}

Init

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

public class Init : MonoBehaviour
{
    private void Awake()
    {
        GameObject go = Resources.Load<GameObject>(PlayerPrefs.GetString("CharacterName"));
        Instantiate(go);
    }
}

KeyboardManager

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

public class KeyboardManager : MonoBehaviour
{
    public Text ShowText;

    public string inputStr;
    public bool isUP = false;
    private Transform targetPos;

    private void Awake()
    {
        targetPos = GameObject.Find("[CameraRig]/Controller (left)/KeyboardTargetPos").transform;
        EventCenter.AddListener<bool>(EventDefine.IsActiveKeyboard, IsActive);
        gameObject.SetActive(false);
    }
    private void OnDestroy()
    {
        EventCenter.RemoveListener<bool>(EventDefine.IsActiveKeyboard, IsActive);
    }
    private void IsActive(bool value)
    {
        gameObject.SetActive(value);
    }
    private void Update()
    {
        ShowText.text = inputStr;
        if (gameObject.activeInHierarchy)
        {
            transform.position = targetPos.position;
            transform.rotation = targetPos.rotation;
        }
    }
    public void DeleteStr()
    {
        if (inputStr == null || inputStr == "")
        {
            return;
        }
        //hello
        inputStr = inputStr.Substring(0, inputStr.Length - 1);
    }
    public void EnterPress()
    {
        if (inputStr == null || inputStr == "")
        {
            EventCenter.Broadcast(EventDefine.Hint, "輸入文本為空军援,不能生成彈幕");
            return;
        }
        //生成彈幕
        EventCenter.Broadcast(EventDefine.SpawnTextTag, inputStr, transform.position, transform.rotation);
        inputStr = "";
    }
}

KeyManager

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

public class KeyManager : MonoBehaviour
{
    public Material mat_Normal;
    public Material mat_Press;
    private MeshRenderer m_MeshRenderer;
    private string lowValue;
    private string upValue;
    private KeyboardManager keyboardManager;
    private bool isClick = true;
    private float timer = 0.0f;
    private float time = 0.5f;

    private void Awake()
    {
        m_MeshRenderer = GetComponentInChildren<MeshRenderer>();
        lowValue = transform.GetChild(0).GetComponent<Text>().text;
        keyboardManager = GetComponentInParent<KeyboardManager>();

        if (lowValue == "0" || lowValue == "1" || lowValue == "2" || lowValue == "3"
            || lowValue == "4" || lowValue == "5" || lowValue == "6" || lowValue == "7"
            || lowValue == "8" || lowValue == "9" || lowValue == "UP" || lowValue == " " || lowValue == "0"
            || lowValue == "!" || lowValue == "." || lowValue == "," || lowValue == "Back" || lowValue == "Enter")
        {
            upValue = lowValue;
        }
        else
        {
            upValue = lowValue.ToUpper();
        }
    }
    private void Update()
    {
        if (keyboardManager.isUP)
        {
            transform.GetChild(0).GetComponent<Text>().text = upValue;
        }
        else
        {
            transform.GetChild(0).GetComponent<Text>().text = lowValue;
        }

        if (isClick == false)
        {
            timer += Time.deltaTime;
            if (timer >= time)
            {
                timer = 0.0f;
                isClick = true;
            }
        }
    }
    private void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Finger" && isClick)
        {
            //廣播右手震動的事件碼
            EventCenter.Broadcast(EventDefine.RightControllerPulse);

            m_MeshRenderer.material = mat_Press;

            if (lowValue == "UP")
            {
                keyboardManager.isUP = !keyboardManager.isUP;
            }
            else if (lowValue == "Back")
            {
                keyboardManager.DeleteStr();
            }
            else if (lowValue == "Enter")
            {
                keyboardManager.EnterPress();
            }
            else
            {
                if (keyboardManager.isUP)
                {
                    keyboardManager.inputStr += upValue;
                }
                else
                {
                    keyboardManager.inputStr += lowValue;
                }
            }
        }
    }
    private void OnTriggerExit(Collider other)
    {
        if (other.tag == "Finger")
        {
            isClick = false;
            m_MeshRenderer.material = mat_Normal;
        }
    }
}

MarkPen

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

public class MarkPen : MonoBehaviour
{
    private P3D_PaintOnCollision paint;

    private void Awake()
    {
        paint = GetComponentInChildren<P3D_PaintOnCollision>();
        EventCenter.AddListener<bool>(EventDefine.IsActiveDraw, IsActive);
        IsActive(false);
    }
    private void OnDestroy()
    {
        EventCenter.RemoveListener<bool>(EventDefine.IsActiveDraw, IsActive);
    }
    private void Update()
    {
        paint.Brush.Color = ColorSelector.Instance.finalColor;
    }
    private void IsActive(bool value)
    {
        gameObject.SetActive(value);
    }
}

PulseManager

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

public class PulseManager : MonoBehaviour
{
    public EventDefine eventType;
    private VRTK_ControllerActions actions;

    private void Awake()
    {
        actions = GetComponent<VRTK_ControllerActions>();
        EventCenter.AddListener(eventType, Pulse);
    }
    private void OnDestroy()
    {
        EventCenter.RemoveListener(eventType, Pulse);
    }
    private void Pulse()
    {
        actions.TriggerHapticPulse(60000, 0.1f, 0.01f);
    }
}

TagParentManager

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

public class TagParentManager : MonoBehaviour
{
    private void Awake()
    {
        EventCenter.AddListener<bool>(EventDefine.IsActiveTagParent, IsActive);
    }
    private void OnDestroy()
    {
        EventCenter.RemoveListener<bool>(EventDefine.IsActiveTagParent, IsActive);
    }
    private void IsActive(bool value)
    {
        gameObject.SetActive(value);
    }
}

TagSpawnManager

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

public class TagSpawnManager : MonoBehaviour
{
    private Transform tagParent;

    private void Awake()
    {
        tagParent = GameObject.FindGameObjectWithTag("TagParent").transform;
        EventCenter.AddListener<string, Vector3, Quaternion>(EventDefine.SpawnTextTag, SpawnTextTag);
    }
    private void OnDestroy()
    {
        EventCenter.RemoveListener<string, Vector3, Quaternion>(EventDefine.SpawnTextTag, SpawnTextTag);
    }
    private void SpawnTextTag(string text, Vector3 pos, Quaternion rot)
    {
        int ran = Random.Range(0, 9);
        string path = "TextTag/TextTag_" + ran;
        GameObject go = ResourcesManager.LoadObj(path);
        GameObject tempObj = Instantiate(go, pos, rot);
        tempObj.transform.SetParent(tagParent);
        tempObj.GetComponentInChildren<Text>().text = text;
    }
}

UI:

BaseTrigger

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

public abstract class BaseTrigger : MonoBehaviour
{
    private bool isCanClick = true;
    private float timer = 0.0f;
    private float time = 0.25f;

    private void Update()
    {
        if (isCanClick == false)
        {
            timer += Time.deltaTime;
            if (timer > time)
            {
                timer = 0.0f;
                isCanClick = true;
            }
        }
    }
    private void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Finger" && isCanClick)
        {
            //廣播右手震動的事件碼
            EventCenter.Broadcast(EventDefine.RightControllerPulse);

            TriggerEnterEvent();
        }
    }
    public abstract void TriggerEnterEvent();
    private void OnTriggerExit(Collider other)
    {
        if (other.tag == "Finger")
        {
            isCanClick = false;
        }
    }
}

CharacterChoosePanel

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

public class CharacterChoosePanel : MonoBehaviour
{
    private void Awake()
    {
        gameObject.SetActive(false);
        EventCenter.AddListener<bool>(EventDefine.IsShowCharacterChoosePanel, Show);
        transform.Find("btn_Back").GetComponent<Button>().onClick.AddListener(() =>
        {
            EventCenter.Broadcast(EventDefine.IsShowStartPanel, true);
            Show(false);
        });
    }
    private void OnDestroy()
    {
        EventCenter.RemoveListener<bool>(EventDefine.IsShowCharacterChoosePanel, Show);
    }
    private void Show(bool value)
    {
        gameObject.SetActive(value);
    }
}

CharacterChooseToggle

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

public class CharacterChooseToggle : MonoBehaviour
{
    private void Awake()
    {
        GetComponent<Toggle>().onValueChanged.AddListener(OnValueChanged);
    }
    private void Start()
    {
        OnValueChanged(GetComponent<Toggle>().isOn);
    }
    private void OnValueChanged(bool value)
    {
        transform.GetChild(0).gameObject.SetActive(value);
        if (value)
        {
            EventCenter.Broadcast(EventDefine.OnCharacterChoose, gameObject.name);
        }
    }
}

Hint

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

public class Hint : MonoBehaviour
{
    public float waitTime = 1.5f;

    private void Awake()
    {
        EventCenter.AddListener<string>(EventDefine.Hint, Show);
        gameObject.SetActive(false);
    }
    private void OnDestroy()
    {
        EventCenter.RemoveListener<string>(EventDefine.Hint, Show);
    }
    private void Show(string str)
    {
        gameObject.SetActive(true);
        GetComponent<Text>().text = str;
        StartCoroutine("DealyHide");
    }
    IEnumerator DealyHide()
    {
        yield return new WaitForSeconds(waitTime);
        gameObject.SetActive(false);
    }
}

HintOrShowButtonSet

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

public class HintOrShowButtonSet : MonoBehaviour
{
    private bool isClick = false;
    private Image image;

    private void Awake()
    {
        EventCenter.AddListener(EventDefine.HintOrShowButtonClick, OnBtnClick);
        image = GetComponentInChildren<Image>();
    }
    private void Start()
    {
        image.transform.localEulerAngles = new Vector3(0, 0, 270);
    }
    private void OnDestroy()
    {
        EventCenter.RemoveListener(EventDefine.HintOrShowButtonClick, OnBtnClick);
    }
    private void OnBtnClick()
    {
        isClick = !isClick;
        if (isClick)
        {
            //顯示睜眼的圖標(biāo)
            image.sprite = ResourcesManager.LoadSprite("showIcon");
        }
        else
        {
            //顯示閉眼的圖標(biāo)
            image.sprite = ResourcesManager.LoadSprite("hintIcon");
        }
    }
}

Loading

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

public class Loading : MonoBehaviour
{
    public string SceneName;

    private Text txt_Progress;
    private AsyncOperation ao;
    private bool isLoad = false;

    private void Awake()
    {
        txt_Progress = GetComponent<Text>();
        EventCenter.AddListener(EventDefine.StartLoadScene, StartLoad);
        gameObject.SetActive(false);
    }
    private void OnDestroy()
    {
        EventCenter.RemoveListener(EventDefine.StartLoadScene, StartLoad);
    }
    private void StartLoad()
    {
        gameObject.SetActive(true);
        StartCoroutine("Load");
    }
    IEnumerator Load()
    {
        int displayProgress = -1;
        int toProgress = 100;

        while (displayProgress < toProgress)
        {
            ++displayProgress;
            ShowProgress(displayProgress);

            if (isLoad == false)
            {
                ao = SceneManager.LoadSceneAsync(SceneName);
                ao.allowSceneActivation = false;
                isLoad = true;
            }
            yield return new WaitForEndOfFrame();
        }
        if (displayProgress == 100)
        {
            ao.allowSceneActivation = true;
            StopCoroutine("Load");
        }
    }
    private void ShowProgress(int progress)
    {
        txt_Progress.text = progress.ToString() + "%";
    }
}

MenuButtonManager

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

public class MenuButtonManager : MonoBehaviour
{
    private bool isActiveTagParent = true;

    /// <summary>
    /// 鍵盤按鈕點(diǎn)擊
    /// </summary>
    public void KeyboardButtonClick()
    {
        //廣播左手震動的事件碼
        EventCenter.Broadcast(EventDefine.LeftControllerPulse);

        EventCenter.Broadcast(EventDefine.IsActiveKeyboard, true);
        EventCenter.Broadcast(EventDefine.IsActiveTextureTagPanel, false);
        EventCenter.Broadcast(EventDefine.IsActiveDraw, false);
    }
    /// <summary>
    /// 圖片彈幕按鈕點(diǎn)擊
    /// </summary>
    public void PicButtonClick()
    {
        //廣播左手震動的事件碼
        EventCenter.Broadcast(EventDefine.LeftControllerPulse);

        EventCenter.Broadcast(EventDefine.IsActiveTextureTagPanel, true);
        EventCenter.Broadcast(EventDefine.IsActiveKeyboard, false);
        EventCenter.Broadcast(EventDefine.IsActiveDraw, false);
    }
    /// <summary>
    /// 涂鴉按鈕點(diǎn)擊
    /// </summary>
    public void DrawButtonClick()
    {
        //廣播左手震動的事件碼
        EventCenter.Broadcast(EventDefine.LeftControllerPulse);

        EventCenter.Broadcast(EventDefine.IsActiveDraw, true);
        EventCenter.Broadcast(EventDefine.IsActiveTextureTagPanel, false);
        EventCenter.Broadcast(EventDefine.IsActiveKeyboard, false);
    }
    /// <summary>
    /// 隱藏或者顯示按鈕點(diǎn)擊
    /// </summary>
    public void HideOrShowButtonClick()
    {
        //廣播左手震動的事件碼
        EventCenter.Broadcast(EventDefine.LeftControllerPulse);

        isActiveTagParent = !isActiveTagParent;
        EventCenter.Broadcast(EventDefine.HintOrShowButtonClick);
        EventCenter.Broadcast(EventDefine.IsActiveTagParent, isActiveTagParent);
        EventCenter.Broadcast(EventDefine.IsActiveKeyboard, false);
        EventCenter.Broadcast(EventDefine.IsActiveDraw, false);
    }
}

StartPanel

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

public class StartPanel : MonoBehaviour
{
    private string m_CharacterName = "kachujin";

    private void Awake()
    {
        transform.Find("btn_CharacterChoose").GetComponent<Button>().onClick.AddListener(() =>
        {
            EventCenter.Broadcast(EventDefine.IsShowCharacterChoosePanel, true);
            Show(false);
        });
        transform.Find("btn_Start").GetComponent<Button>().onClick.AddListener(() =>
        {
            Show(false);
            EventCenter.Broadcast(EventDefine.StartLoadScene);
            PlayerPrefs.SetString("CharacterName", m_CharacterName);
        });
        EventCenter.AddListener<bool>(EventDefine.IsShowStartPanel, Show);
        EventCenter.AddListener<string>(EventDefine.OnCharacterChoose, ChooseCharacter);
    }
    private void OnDestroy()
    {
        EventCenter.RemoveListener<bool>(EventDefine.IsShowStartPanel, Show);
        EventCenter.RemoveListener<string>(EventDefine.OnCharacterChoose, ChooseCharacter);
    }
    private void Show(bool value)
    {
        gameObject.SetActive(value);
    }
    private void ChooseCharacter(string characterName)
    {
        m_CharacterName = characterName;
    }
}

TexturesTagPanel

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

public class TexturesTagPanel : MonoBehaviour
{
    public Transform panels;
    public int m_Index = 0;

    private void Awake()
    {
        EventCenter.AddListener<bool>(EventDefine.IsActiveTextureTagPanel, IsActive);
        gameObject.SetActive(false);
    }
    private void OnDestroy()
    {
        EventCenter.RemoveListener<bool>(EventDefine.IsActiveTextureTagPanel, IsActive);
    }
    private void IsActive(bool value)
    {
        gameObject.SetActive(value);
    }
    public void LastButtonClick()
    {
        panels.GetChild(m_Index).gameObject.SetActive(false);
        m_Index--;
        if (m_Index < 0)
        {
            m_Index = panels.childCount - 1;
        }
        panels.GetChild(m_Index).gameObject.SetActive(true);
    }
    public void NextButtonClick()
    {
        panels.GetChild(m_Index).gameObject.SetActive(false);
        m_Index++;
        if (m_Index > panels.childCount - 1)
        {
            m_Index = 0;
        }
        panels.GetChild(m_Index).gameObject.SetActive(true);
    }
}

TextureTagButtonClick

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

public class TextureTagButtonClick : BaseTrigger
{
    private Transform tagParent;

    private void Awake()
    {
        tagParent = GameObject.FindGameObjectWithTag("TagParent").transform;
    }
    public override void TriggerEnterEvent()
    {
        GameObject go = ResourcesManager.LoadObj("TextureTagSprite");
        GameObject tempObj = Instantiate(go, tagParent);
        tempObj.transform.position = GetComponentInParent<TexturesTagPanel>().transform.position;
        tempObj.transform.rotation = GetComponentInParent<TexturesTagPanel>().transform.rotation;

        if (GetComponentInParent<TexturesTagPanel>().m_Index == 0)
        {
            //加載第一頁的圖片
            Sprite sprite = ResourcesManager.LoadSprite("圖片標(biāo)簽/" + GetComponent<Image>().sprite.name);
            tempObj.GetComponent<SpriteRenderer>().sprite = sprite;
        }
        else if (GetComponentInParent<TexturesTagPanel>().m_Index == 1)
        {
            //加載第二頁圖片
            Sprite sprite = ResourcesManager.LoadEmoji(GetComponent<Image>().sprite.name);
            if (sprite != null)
            {
                tempObj.GetComponent<SpriteRenderer>().sprite = sprite;
            }
        }
        else
        {
            //加載第三頁圖片
            Sprite sprite = ResourcesManager.LoadSprite("圖片標(biāo)簽/表情包/" + GetComponent<Image>().sprite.name);
            tempObj.GetComponent<SpriteRenderer>().sprite = sprite;
        }
    }
}

TextureTagNextButton

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

public class TextureTagNextButton : BaseTrigger
{
    public override void TriggerEnterEvent()
    {
        GetComponentInParent<TexturesTagPanel>().NextButtonClick();
    }
}

TextureTagPanelLastButton

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

public class TextureTagPanelLastButton : BaseTrigger
{
    public override void TriggerEnterEvent()
    {
        GetComponentInParent<TexturesTagPanel>().LastButtonClick();
    }
}

三、效果圖

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末称勋,一起剝皮案震驚了整個濱河市胸哥,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌赡鲜,老刑警劉巖空厌,帶你破解...
    沈念sama閱讀 206,602評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異银酬,居然都是意外死亡嘲更,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,442評論 2 382
  • 文/潘曉璐 我一進(jìn)店門揩瞪,熙熙樓的掌柜王于貴愁眉苦臉地迎上來赋朦,“玉大人,你說我怎么就攤上這事李破〕韬澹” “怎么了?”我有些...
    開封第一講書人閱讀 152,878評論 0 344
  • 文/不壞的土叔 我叫張陵嗤攻,是天一觀的道長毛嫉。 經(jīng)常有香客問我,道長屯曹,這世上最難降的妖魔是什么狱庇? 我笑而不...
    開封第一講書人閱讀 55,306評論 1 279
  • 正文 為了忘掉前任,我火速辦了婚禮恶耽,結(jié)果婚禮上密任,老公的妹妹穿的比我還像新娘。我一直安慰自己偷俭,他們只是感情好浪讳,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,330評論 5 373
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著涌萤,像睡著了一般淹遵。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上负溪,一...
    開封第一講書人閱讀 49,071評論 1 285
  • 那天透揣,我揣著相機(jī)與錄音,去河邊找鬼川抡。 笑死辐真,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播侍咱,決...
    沈念sama閱讀 38,382評論 3 400
  • 文/蒼蘭香墨 我猛地睜開眼耐床,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了楔脯?” 一聲冷哼從身側(cè)響起撩轰,我...
    開封第一講書人閱讀 37,006評論 0 259
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎昧廷,沒想到半個月后堪嫂,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,512評論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡麸粮,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,965評論 2 325
  • 正文 我和宋清朗相戀三年溉苛,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片弄诲。...
    茶點(diǎn)故事閱讀 38,094評論 1 333
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖娇唯,靈堂內(nèi)的尸體忽然破棺而出齐遵,到底是詐尸還是另有隱情,我是刑警寧澤塔插,帶...
    沈念sama閱讀 33,732評論 4 323
  • 正文 年R本政府宣布梗摇,位于F島的核電站,受9級特大地震影響想许,放射性物質(zhì)發(fā)生泄漏伶授。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,283評論 3 307
  • 文/蒙蒙 一流纹、第九天 我趴在偏房一處隱蔽的房頂上張望糜烹。 院中可真熱鬧,春花似錦漱凝、人聲如沸疮蹦。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,286評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽愕乎。三九已至,卻和暖如春壁公,著一層夾襖步出監(jiān)牢的瞬間感论,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,512評論 1 262
  • 我被黑心中介騙來泰國打工紊册, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留比肄,地道東北人。 一個月前我還...
    沈念sama閱讀 45,536評論 2 354
  • 正文 我出身青樓,卻偏偏與公主長得像薪前,于是被迫代替她去往敵國和親润努。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,828評論 2 345

推薦閱讀更多精彩內(nèi)容