VR开发实战HTC Vive项目之弹幕涂鸦(事件广播与监听)

一、框架视图

二、主要代码

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}添加不同类型的委托,当前事件所对应的委托是{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("移除监听错误:事件{0}没有对应的委托", EventDefine));
            }
            else if (d.GetType() != callBack.GetType())
            {
                throw new Exception(string.Format("移除监听错误:尝试为事件{0}移除不同类型的委托,当前委托类型为{1},要移除的委托类型为{2}", EventDefine, d.GetType(), callBack.GetType()));
            }
        }
        else
        {
            throw new Exception(string.Format("移除监听错误:没有事件码{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}对应委托具有不同的类型", 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}对应委托具有不同的类型", 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}对应委托具有不同的类型", 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}对应委托具有不同的类型", 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}对应委托具有不同的类型", 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}对应委托具有不同的类型", 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>("图片标签/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)
        {
            //显示睁眼的图标
            image.sprite = ResourcesManager.LoadSprite("showIcon");
        }
        else
        {
            //显示闭眼的图标
            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>
    /// 键盘按钮点击
    /// </summary>
    public void KeyboardButtonClick()
    {
        //广播左手震动的事件码
        EventCenter.Broadcast(EventDefine.LeftControllerPulse);

        EventCenter.Broadcast(EventDefine.IsActiveKeyboard, true);
        EventCenter.Broadcast(EventDefine.IsActiveTextureTagPanel, false);
        EventCenter.Broadcast(EventDefine.IsActiveDraw, false);
    }
    /// <summary>
    /// 图片弹幕按钮点击
    /// </summary>
    public void PicButtonClick()
    {
        //广播左手震动的事件码
        EventCenter.Broadcast(EventDefine.LeftControllerPulse);

        EventCenter.Broadcast(EventDefine.IsActiveTextureTagPanel, true);
        EventCenter.Broadcast(EventDefine.IsActiveKeyboard, false);
        EventCenter.Broadcast(EventDefine.IsActiveDraw, false);
    }
    /// <summary>
    /// 涂鸦按钮点击
    /// </summary>
    public void DrawButtonClick()
    {
        //广播左手震动的事件码
        EventCenter.Broadcast(EventDefine.LeftControllerPulse);

        EventCenter.Broadcast(EventDefine.IsActiveDraw, true);
        EventCenter.Broadcast(EventDefine.IsActiveTextureTagPanel, false);
        EventCenter.Broadcast(EventDefine.IsActiveKeyboard, false);
    }
    /// <summary>
    /// 隐藏或者显示按钮点击
    /// </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("图片标签/" + 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("图片标签/表情包/" + 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();
    }
}

三、效果图

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

推荐阅读更多精彩内容