• 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

  • 相关阅读:
    JDBC的使用流程
    typescript vscode /bin/sh: ts-node: command not found
    小程序打开app场景
    设置获取cookie,setCookie,getCookie
    解决IOS微信页面回退不刷新问题
    百度小程序添加编译
    百度小程序审核不通过,基础库问题
    Charles Mac 破解安装和证书安装成功抓包单个域名是unknown
    xhrFields实现跨域访问
    Mac上启动nginx报错:nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use)
  • 原文地址:https://www.cnblogs.com/cation/p/10309157.html
Copyright © 2020-2023  润新知