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

一、框架视图

VR开发实战HTC Vive项目之弹幕涂鸦(事件广播与监听)_第1张图片

二、主要代码

Common:

CallBack

public delegate void CallBack();
public delegate void CallBack(T arg);
public delegate void CallBack(T arg1, X arg2);
public delegate void CallBack(T arg1, X arg2, Y arg3);
public delegate void CallBack(T arg1, X arg2, Y arg3, Z arg4);
public delegate void CallBack(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 m_EventTable = new Dictionary();

    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(EventDefine EventDefine, CallBack callBack)
    {
        OnListenerAdding(EventDefine, callBack);
        m_EventTable[EventDefine] = (CallBack)m_EventTable[EventDefine] + callBack;
    }
    //two parameters
    public static void AddListener(EventDefine EventDefine, CallBack callBack)
    {
        OnListenerAdding(EventDefine, callBack);
        m_EventTable[EventDefine] = (CallBack)m_EventTable[EventDefine] + callBack;
    }
    //three parameters
    public static void AddListener(EventDefine EventDefine, CallBack callBack)
    {
        OnListenerAdding(EventDefine, callBack);
        m_EventTable[EventDefine] = (CallBack)m_EventTable[EventDefine] + callBack;
    }
    //four parameters
    public static void AddListener(EventDefine EventDefine, CallBack callBack)
    {
        OnListenerAdding(EventDefine, callBack);
        m_EventTable[EventDefine] = (CallBack)m_EventTable[EventDefine] + callBack;
    }
    //five parameters
    public static void AddListener(EventDefine EventDefine, CallBack callBack)
    {
        OnListenerAdding(EventDefine, callBack);
        m_EventTable[EventDefine] = (CallBack)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(EventDefine EventDefine, CallBack callBack)
    {
        OnListenerRemoving(EventDefine, callBack);
        m_EventTable[EventDefine] = (CallBack)m_EventTable[EventDefine] - callBack;
        OnListenerRemoved(EventDefine);
    }
    //two parameters
    public static void RemoveListener(EventDefine EventDefine, CallBack callBack)
    {
        OnListenerRemoving(EventDefine, callBack);
        m_EventTable[EventDefine] = (CallBack)m_EventTable[EventDefine] - callBack;
        OnListenerRemoved(EventDefine);
    }
    //three parameters
    public static void RemoveListener(EventDefine EventDefine, CallBack callBack)
    {
        OnListenerRemoving(EventDefine, callBack);
        m_EventTable[EventDefine] = (CallBack)m_EventTable[EventDefine] - callBack;
        OnListenerRemoved(EventDefine);
    }
    //four parameters
    public static void RemoveListener(EventDefine EventDefine, CallBack callBack)
    {
        OnListenerRemoving(EventDefine, callBack);
        m_EventTable[EventDefine] = (CallBack)m_EventTable[EventDefine] - callBack;
        OnListenerRemoved(EventDefine);
    }
    //five parameters
    public static void RemoveListener(EventDefine EventDefine, CallBack callBack)
    {
        OnListenerRemoving(EventDefine, callBack);
        m_EventTable[EventDefine] = (CallBack)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(EventDefine EventDefine, T arg)
    {
        Delegate d;
        if (m_EventTable.TryGetValue(EventDefine, out d))
        {
            CallBack callBack = d as CallBack;
            if (callBack != null)
            {
                callBack(arg);
            }
            else
            {
                throw new Exception(string.Format("广播事件错误:事件{0}对应委托具有不同的类型", EventDefine));
            }
        }
    }
    //two parameters
    public static void Broadcast(EventDefine EventDefine, T arg1, X arg2)
    {
        Delegate d;
        if (m_EventTable.TryGetValue(EventDefine, out d))
        {
            CallBack callBack = d as CallBack;
            if (callBack != null)
            {
                callBack(arg1, arg2);
            }
            else
            {
                throw new Exception(string.Format("广播事件错误:事件{0}对应委托具有不同的类型", EventDefine));
            }
        }
    }
    //three parameters
    public static void Broadcast(EventDefine EventDefine, T arg1, X arg2, Y arg3)
    {
        Delegate d;
        if (m_EventTable.TryGetValue(EventDefine, out d))
        {
            CallBack callBack = d as CallBack;
            if (callBack != null)
            {
                callBack(arg1, arg2, arg3);
            }
            else
            {
                throw new Exception(string.Format("广播事件错误:事件{0}对应委托具有不同的类型", EventDefine));
            }
        }
    }
    //four parameters
    public static void Broadcast(EventDefine EventDefine, T arg1, X arg2, Y arg3, Z arg4)
    {
        Delegate d;
        if (m_EventTable.TryGetValue(EventDefine, out d))
        {
            CallBack callBack = d as CallBack;
            if (callBack != null)
            {
                callBack(arg1, arg2, arg3, arg4);
            }
            else
            {
                throw new Exception(string.Format("广播事件错误:事件{0}对应委托具有不同的类型", EventDefine));
            }
        }
    }
    //five parameters
    public static void Broadcast(EventDefine EventDefine, T arg1, X arg2, Y arg3, Z arg4, W arg5)
    {
        Delegate d;
        if (m_EventTable.TryGetValue(EventDefine, out d))
        {
            CallBack callBack = d as CallBack;
            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 m_ObjDic = new Dictionary();

    public static Sprite LoadSprite(string spriteName)
    {
        Sprite temp;
        if (!m_ObjDic.ContainsKey(spriteName))
        {
            temp = Resources.Load(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(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("图片标签/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();
    }
    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(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(EventDefine.IsActiveKeyboard, IsActive);
        gameObject.SetActive(false);
    }
    private void OnDestroy()
    {
        EventCenter.RemoveListener(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();
        lowValue = transform.GetChild(0).GetComponent().text;
        keyboardManager = GetComponentInParent();

        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 = upValue;
        }
        else
        {
            transform.GetChild(0).GetComponent().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();
        EventCenter.AddListener(EventDefine.IsActiveDraw, IsActive);
        IsActive(false);
    }
    private void OnDestroy()
    {
        EventCenter.RemoveListener(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();
        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(EventDefine.IsActiveTagParent, IsActive);
    }
    private void OnDestroy()
    {
        EventCenter.RemoveListener(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(EventDefine.SpawnTextTag, SpawnTextTag);
    }
    private void OnDestroy()
    {
        EventCenter.RemoveListener(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;
    }
}

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(EventDefine.IsShowCharacterChoosePanel, Show);
        transform.Find("btn_Back").GetComponent

CharacterChooseToggle

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

public class CharacterChooseToggle : MonoBehaviour
{
    private void Awake()
    {
        GetComponent().onValueChanged.AddListener(OnValueChanged);
    }
    private void Start()
    {
        OnValueChanged(GetComponent().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(EventDefine.Hint, Show);
        gameObject.SetActive(false);
    }
    private void OnDestroy()
    {
        EventCenter.RemoveListener(EventDefine.Hint, Show);
    }
    private void Show(string str)
    {
        gameObject.SetActive(true);
        GetComponent().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();
    }
    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();
        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;

    /// 
    /// 键盘按钮点击
    /// 
    public void KeyboardButtonClick()
    {
        //广播左手震动的事件码
        EventCenter.Broadcast(EventDefine.LeftControllerPulse);

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

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

        EventCenter.Broadcast(EventDefine.IsActiveDraw, true);
        EventCenter.Broadcast(EventDefine.IsActiveTextureTagPanel, false);
        EventCenter.Broadcast(EventDefine.IsActiveKeyboard, false);
    }
    /// 
    /// 隐藏或者显示按钮点击
    /// 
    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

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(EventDefine.IsActiveTextureTagPanel, IsActive);
        gameObject.SetActive(false);
    }
    private void OnDestroy()
    {
        EventCenter.RemoveListener(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().transform.position;
        tempObj.transform.rotation = GetComponentInParent().transform.rotation;

        if (GetComponentInParent().m_Index == 0)
        {
            //加载第一页的图片
            Sprite sprite = ResourcesManager.LoadSprite("图片标签/" + GetComponent().sprite.name);
            tempObj.GetComponent().sprite = sprite;
        }
        else if (GetComponentInParent().m_Index == 1)
        {
            //加载第二页图片
            Sprite sprite = ResourcesManager.LoadEmoji(GetComponent().sprite.name);
            if (sprite != null)
            {
                tempObj.GetComponent().sprite = sprite;
            }
        }
        else
        {
            //加载第三页图片
            Sprite sprite = ResourcesManager.LoadSprite("图片标签/表情包/" + GetComponent().sprite.name);
            tempObj.GetComponent().sprite = sprite;
        }
    }
}

TextureTagNextButton

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

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

TextureTagPanelLastButton

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

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

三、效果图

VR开发实战HTC Vive项目之弹幕涂鸦(事件广播与监听)_第2张图片

你可能感兴趣的:(VR开发实战HTC Vive项目之弹幕涂鸦(事件广播与监听))