原文: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 = ;
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 = "";
counterText.fontSize = ;
counterText.color = Color.white;
//counterText.font = combo.GetPrivateField<TextMeshProUGUI>("_comboText").font;
counterText.alignment = TextAlignmentOptions.Center;
counterText.rectTransform.position = Plugin.counterPosition + new Vector3(, -0.4f, ); countGO = new GameObject("Label");
TextMeshPro label = countGO.AddComponent<TextMeshPro>();
label.text = "Misses";
label.fontSize = ;
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

BeatSaber节奏光剑插件开发官方教程2-简单的插件示例的更多相关文章

  1. BeatSaber节奏光剑插件开发官方教程1-创建一个插件模板

    原文:https://wiki.assistant.moe/modding/intro 一.简介 Beat Saber 开发环境:unity2018.C#..NET framework 4.6. 此教 ...

  2. BeatSaber节奏光剑双手柄MR教程

    一.物料准备: 1 显卡1060及以上的PC主机 2 HTC VIVE头盔一套(头盔直插显卡上的HDMI接口) 3 1080P摄像头一个(插USB3.0) 4 绿幕一套,能覆盖整个摄像头的可拍摄范围即 ...

  3. RecyclerView(5)官方教程带简单示例

    Create Lists The RecyclerView widget is a more advanced and flexible version of ListView. This widge ...

  4. Unity性能优化(3)-官方教程Optimizing garbage collection in Unity games翻译

    本文是Unity官方教程,性能优化系列的第三篇<Optimizing garbage collection in Unity games>的翻译. 相关文章: Unity性能优化(1)-官 ...

  5. Unity性能优化(4)-官方教程Optimizing graphics rendering in Unity games翻译

    本文是Unity官方教程,性能优化系列的第四篇<Optimizing graphics rendering in Unity games>的翻译. 相关文章: Unity性能优化(1)-官 ...

  6. Unity性能优化(2)-官方教程Diagnosing performance problems using the Profiler window翻译

    本文是Unity官方教程,性能优化系列的第二篇<Diagnosing performance problems using the Profiler window>的简单翻译. 相关文章: ...

  7. Unity性能优化(1)-官方教程The Profiler window翻译

    本文是Unity官方教程,性能优化系列的第一篇<The Profiler window>的简单翻译. 相关文章: Unity性能优化(1)-官方教程The Profiler window翻 ...

  8. Google Guava官方教程(中文版)

    Google Guava官方教程(中文版) 原文链接  译文链接 译者: 沈义扬,罗立树,何一昕,武祖  校对:方腾飞 引言 Guava工程包含了若干被Google的 Java项目广泛依赖 的核心库, ...

  9. OpenGL官方教程——着色器语言概述

    OpenGL官方教程——着色器语言概述 OpenGL官方教程——着色器语言概述 可编程图形硬件管线(流水线) 可编程顶点处理器 可编程几何处理器 可编程片元处理器 语言 可编程图形硬件管线(流水线) ...

随机推荐

  1. mysql_real_connect 端口号说明

    mysql_real_connect语法:  C++ Code  12345678   MYSQL * mysql_real_connect(MYSQL * mysql,                ...

  2. java&javaweb学习笔记

    http://blog.csdn.net/h3243212/article/details/50659471

  3. PostgreSql Partition + Hibernate Insert

    与Oracle不同.PostgreSQL须要手动控制分区规则触发器. 步骤一:创建分区 CREATE TABLE table_partition_1( CHECK partition_column c ...

  4. iOS 友盟统计怎么用

    本文转载至 http://blog.csdn.net/woaifen3344/article/details/41284395 友盟统计UMAnalyticsiOS友盟统计iOS UMAnalytic ...

  5. c++ std::ifstream

    #include <iostream> #include <plug/plug.h> using namespace std; //使用宽字符,我猜是为了适应那些要使用宽字符的 ...

  6. 160706、Java HashMap工作原理及实现

    1. 概述 从本文你可以学习到: 什么时候会使用HashMap?他有什么特点? 你知道HashMap的工作原理吗? 你知道get和put的原理吗?equals()和hashCode()的都有什么作用? ...

  7. ZOJ 3332 Strange Country II

    Strange Country II Time Limit: 1 Second      Memory Limit: 32768 KB      Special Judge You want to v ...

  8. ClickHouse开源数据库

    ClickHouse是一个开源的面向列式数据的数据库管理系统,能够使用SQL查询并且生成实时数据报告. 优点: 1.并行处理单个查询(利用多核) 2.在多个服务器上分布式处理 3.非常快的扫描,可用于 ...

  9. 巨蟒python全栈开发-第23天 内置常用模块2

    一.今日主要内容 1.nametuple:(命名元组,本质还是元组) 命名元组=>类似创建了一个类 结构化时间其实是个命名元组 2.os 主要是针对操作系统的 一般用来操作文件系统 os.mak ...

  10. Universally Unique Identifier amazonservices API order 亚马逊订单接口的分析 NextToken

    one hour in linux mysql> ) from listorders; +----------+ | count() | +----------+ | | +---------- ...