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

2019-04-04  本文已影响0人  TonyWan_AR

一、框架视图

二、主要代码

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();
    }
}

三、效果图

上一篇下一篇

猜你喜欢

热点阅读