BeatSaber節奏光劍插件開發官方教程2-簡單的插件示例


原文:https://wiki.assistant.moe/modding/example-mod

一、在開始之前

  1 確保你已經看過教你如何添加插件模板的教程,且你已經使用插件模板創建了一個新項目

  https://www.cnblogs.com/cation/p/10308764.html

  2 如果此教程中有很多地方你看的一頭霧水,那可能是因為你不太了解unity或c#,你需要先學習一些相關的知識

  3 如果你已經學習過了前面的插件模板添加教程,你的當前視圖應該是下圖這樣的:

  4 本教程默認你對c#和unity有基本的了解,如果沒有這方面基礎的話還請先去學習下基本知識

二、簡介

   此教程將會引導你創建一個簡單的插件,該插件可以記錄我們miss了多少個方塊,這個示例插件將包括:

  1 一個包含組件類的空GameObject 

  2 TextMeshPro meshes,unity中用來顯示文本的實例

  3 events和actions的初步了解

三、設置變量

  在開始之前,我們需要設置一些變量來協助我們的開發工作

enabled 是否進行計數的flag
counterPosition 計數顯示在界面中的位置

  如果提示了“Vector3 variable”錯誤,你需要在代碼的最前面添加“using UnityEngine;”

PS:

  可以在OnApplicationStart()函數第一行添加如下代碼:

    Console.WriteLine("Hello World!");

  這行代碼可以幫助你進行代碼的調試,你可以使用--verbose參數啟動游戲,這樣會伴隨游戲啟動一個調試窗口,調試窗口會顯示異常信息和上述代碼中你設置的調試信息。

四、創建一個GameObject

  在“Plugin.cs”文件中我們只需要少量代碼。

  “SceneManagerOnActiveSceneChanged()”事件會在游戲場景變化時觸發,所以我們可以在這里創建GameObject。

  1 第一行代碼做了個是否執行插件的判定,前面我們設置了enabled

  2 第二行代碼中判定當前的場景是否是“GameCore”,確保游戲開始時對插件進行初始化,避免在主菜單就初始化插件

  3 第三行代碼中MissedCounter報錯了,因為你還沒創建這個object呢

五、MissedCounter.cs

  創建一個新的class,命名為MissedCounter.cs,並使其繼承MonoBehavior。

 

后面很繁瑣了,我直接貼代碼出來,完整的代碼可以這里下載(MissedCounter-master.zip):

https://github.com/Caeden117/MissedCounter

(或Q群810303476,群文件下載)

如果想知道其他的接口如何使用,可以到github上下載其他的開源插件代碼參考。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using TMPro;
using System.Collections;
using System.Threading;

namespace MissedCounter
{
    class MissedCounter : MonoBehaviour
    {
        int counter = 0;
        private ScoreController score;
        //private ComboUIController combo;
        GameObject countGO;
        TextMeshPro counterText;

        void Awake()
        {
            StartCoroutine(GetScore());
        }

        IEnumerator GetScore()
        {
            while (true)
            {
                score = Resources.FindObjectsOfTypeAll<ScoreController>().FirstOrDefault();
                //combo = Resources.FindObjectsOfTypeAll<ComboUIController>().FirstOrDefault();
                //if (score != null && combo != null) break;
                if (score != null) break;
                yield return null;
            }
            Init();
        }

        void Init()
        {
            counterText = this.gameObject.AddComponent<TextMeshPro>();
            counterText.text = "0";
            counterText.fontSize = 4;
            counterText.color = Color.white;
            //counterText.font = combo.GetPrivateField<TextMeshProUGUI>("_comboText").font;
            counterText.alignment = TextAlignmentOptions.Center;
            counterText.rectTransform.position = Plugin.counterPosition + new Vector3(0, -0.4f, 0);

            countGO = new GameObject("Label");
            TextMeshPro label = countGO.AddComponent<TextMeshPro>();
            label.text = "Misses";
            label.fontSize = 3;
            label.color = Color.white;
            //label.font = combo.GetPrivateField<TextMeshProUGUI>("_comboText").font;
            label.alignment = TextAlignmentOptions.Center;
            label.rectTransform.position = Plugin.counterPosition;

            if (score != null)
            {
                score.noteWasCutEvent += onNoteCut;
                score.noteWasMissedEvent += onNoteMiss;
            }
        }

        void OnDestroy()
        {
            score.noteWasCutEvent -= onNoteCut;
            score.noteWasMissedEvent -= onNoteMiss;
        }

        private void onNoteCut(NoteData data, NoteCutInfo info, int c)
        {
            if (data.noteType == NoteType.Bomb || !info.allIsOK) incrementCounter();
        }

        private void onNoteMiss(NoteData data, int c)
        {
            if (data.noteType != NoteType.Bomb) incrementCounter();
        }

        private void incrementCounter()
        {
            counter++;
            counterText.text = counter.ToString();
        }
    }
}

 

請務必關注我們的公眾號獲取最新資源和信息:

更多資源在我們的討論Q群:

810303476

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM