Unity的live2dgalgame多语言可配置剧情框架
这段代码用于读取表格
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using OfficeOpenXml;
using System.IO;
using UnityEngine.Networking;
using UnityEngine.UI;
using Random = UnityEngine.Random;public class Plots : MonoBehaviour
{public static string ReadingExcel;//正在读取的表格[Header("表格文件夹")] public static string URL = Application.streamingAssetsPath;public static string PlotsEXCEL = "Plots";private static bool m_loaded;public class plot{public string index, CN,NameCN, NameEN,EN,NameJP, JP,Face;}public static List<plot> S_Plots= new List<plot>();public virtual void OnEnable(){initialization();}//初始化public void initialization(){if (!m_loaded){LoadExcel();m_loaded = true;} }void LoadExcel(){//获取Excel文件的信息foreach (var VARIABLE in ReadFile()){Debug.Log("剧情挂载成功");FileInfo fileInfo = new FileInfo(VARIABLE);//加载背包信息if (VARIABLE.Contains(PlotsEXCEL)){//通过Excel表格的文件信息,打开Excel表格//使用using(){}语句命令,在结束时自动关闭文件using (ExcelPackage excelPackage = new ExcelPackage(fileInfo)){//读取Excel中的第一张表, 注意EPPlus的索引值是从1开始的ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets[1];//取得第一行第一列的数据
// Debug.Log("行数"+worksheet.Dimension.End.Row + 1);for (int Left = 2; Left < worksheet.Dimension.End.Row + 1; Left++) //根据行数遍历{if (worksheet.Cells[Left, 1].Value.ToString().Length>0){plot Plot = new plot();Plot.index= worksheet.Cells[Left, 1].Value.ToString();Plot.Face= worksheet.Cells[Left, 2].Value.ToString();Plot.CN= worksheet.Cells[Left, 3].Value.ToString();Plot.NameCN= worksheet.Cells[Left, 4].Value.ToString();Plot.EN= worksheet.Cells[Left, 5].Value.ToString();Plot.NameEN= worksheet.Cells[Left, 6].Value.ToString();Plot.JP= worksheet.Cells[Left, 7].Value.ToString();Plot.NameJP= worksheet.Cells[Left, 8].Value.ToString();S_Plots.Add(Plot);}}}}}}/// <summary>/// 字符串转Enum/// </summary>/// <typeparam name="T">枚举</typeparam>/// <param name="str">字符串</param>/// <returns>转换的枚举</returns>public static T ToEnum<T>(string str){try{return (T)Enum.Parse(typeof(T), str);}catch (ArgumentException e){Debug.LogError("未找到系列"+str);throw;}}static List<string> ReadFile(){List<string> files = GetFiles(URL, "*.xlsx");List<string> GetFiles(string directory, string pattern){List<string> files = new List<string>();foreach (var item in Directory.GetFiles(directory, pattern)){files.Add(item);}foreach (var item in Directory.GetDirectories(directory)){files.AddRange(GetFiles(item, pattern));}return files;}return files;}
}
这个方法里面设置剧情列表,和分支事件
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Text.RegularExpressions;
using DG.Tweening;
using Live2D.Cubism.Rendering;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Math;
using UnityEngine;
using UnityEngine.UI;public class PlotReader : MonoBehaviour
{public Button[] ChoiceButton;public Text Text_Plot,Text_Name;public static int[] PlotBar;public static Action PlotFinEvent;public Button BTN_NextStep,BTN_Auto,BTN_Skip;public bool AutoMode,SkipMode;public Button LanguageCN, LanguageEN, LanguageJP;public float DomoveOffeset = 100;public float DomoveTime=0.1f;public GameObject Live2DFolider;public int index=0;private void Start(){LanguageCN.onClick.AddListener(()=>{Center.Language = "CN";RefreshLanguage();});LanguageEN.onClick.AddListener(()=>{Center.Language = "EN";RefreshLanguage();});LanguageJP.onClick.AddListener(()=>{Center.Language = "JP";RefreshLanguage();});BTN_Auto.onClick.AddListener(() =>{AutoMode = !AutoMode;SkipMode = false;});BTN_Skip.onClick.AddListener(() =>{SkipMode = !SkipMode;AutoMode = false;});BTN_NextStep.onClick.AddListener(PlotNext);//注意!!! 剧情要-1,选项要-2SetPlotBar(new int[]{0,1,2,3,4,5,6,7},() => MakeChoice(new int[] { 7, 8 ,16}));PlotNext();}private float AutoTimeindex;private float AutoTimeMax = 3f;private void Update(){if (AutoMode){if (index<=PlotBar.Length){AutoTimeindex -= Time.deltaTime;}if (AutoTimeindex < 0 ){AutoTimeindex = AutoTimeMax;PlotNext();}}if (SkipMode){if (index<=PlotBar.Length){AutoTimeindex -= Time.deltaTime*20;}if (AutoTimeindex < 0 ){AutoTimeindex = AutoTimeMax;PlotNext();}}}public void PlotNext(){if (index<PlotBar.Length){DoMethodEvent(Plots.S_Plots[PlotBar[index]].Face);Text_Plot.text = GetLanguagePlot(PlotBar[index]);Text_Name.text = GetLanguageName(PlotBar[index]);}else if (index== PlotBar.Length){PlotFinEvent();}index++;}public void RefreshLanguage(){try{Text_Plot.text = GetLanguagePlot(PlotBar[index-1]);Text_Name.text = GetLanguageName(PlotBar[index-1]);}catch (IndexOutOfRangeException e){Text_Plot.text = GetLanguagePlot(PlotBar[PlotBar.Length-1]);Text_Name.text = GetLanguageName(PlotBar[PlotBar.Length-1]);}}/// <summary>/// 根据语言获得剧情/// </summary>/// <param name="index"></param>/// <returns></returns>public string GetLanguagePlot(int index){// Debug.LogError(index+Plots.S_Plots[index].CN+Plots.S_Plots[index].NameCN);switch (Center.Language){case "CN":return Plots.S_Plots[index].CN;case "EN":return Plots.S_Plots[index].EN;case "JP":return Plots.S_Plots[index].JP;}
Debug.LogError("语言??");return null;}/// <summary>/// 根据语言获得剧情/// </summary>/// <param name="index"></param>/// <returns></returns>public string GetLanguageName(int index){switch (Center.Language){case "CN":return Plots.S_Plots[index].NameCN;case "EN":return Plots.S_Plots[index].NameEN;case "JP":return Plots.S_Plots[index].NameJP;}Debug.LogError("语言??");return null;}/// <summary>/// 设置对话列表/// </summary>/// <param name="t"></param>public void SetPlotBar(int[] t,Action FinEvent){PlotBar = t;PlotFinEvent = FinEvent;}#region 分支public void MakeChoice(int [] choices){CloseAllButtons();for (int i = 0; i<choices.Length; i++){int i1 = i;ChoiceButton[i1].gameObject.SetActive(true);ChoiceButton[i1].transform.Find("Text").GetComponent<Text>().text = GetLanguagePlot(choices[i1]+1);ChoiceButton[i1].onClick.AddListener(() =>{index = 0;Debug.LogError("执行事件BTN"+(choices[i1]+2));ExecuteMethodByName("BTN"+(choices[i1]+2));CloseAllButtons();PlotNext();});}}public void CloseAllButtons(){foreach (var VARIABLE in ChoiceButton){VARIABLE.onClick.RemoveAllListeners();VARIABLE.gameObject.SetActive(false);}}public void BTN9(){SetPlotBar(new int[]{10,11,12,13},JumpToGameLevel);}public void BTN10(){SetPlotBar(new int[]{14,15,16},JumpToGameLevel);}public void BTN18(){SetPlotBar(new int[]{0,1,2,3,4,5,6,7},() => MakeChoice(new int[] { 7, 8 ,16}));}#endregionpublic void JumpToGameLevel(){Debug.LogError("将跳转场景");}public void ExecuteMethodByName(string methodName){// 使用反射获取类的类型Type type = this.GetType();// 使用反射获取方法信息MethodInfo methodInfo = type.GetMethod(methodName);if (methodInfo != null){// 调用匹配的方法methodInfo.Invoke(this, null);}else{Console.WriteLine("Method not found: " + methodName);}}public string[] CutMethod(string input){return input.Split('+');}public void DoMethodEvent(string input){Debug.LogError(input);foreach (var VARIABLE in CutMethod(input)){if (!VARIABLE.Contains("(")){ExecuteMethodByName(VARIABLE);}else if (VARIABLE.Contains("JOIN_")){Match match = Regex.Match(VARIABLE, @"\(([^)]*)\)");if (match.Success){// 获取括号内的内容,并使用逗号分割string[] parts = match.Groups[1].Value.Split(',');if (parts.Length == 4){string stringValue = parts[0];int intValue1, intValue2;if (int.TryParse(parts[1], out intValue1) && int.TryParse(parts[2], out intValue2)){string stringValue2 = parts[3];GameObject go = Instantiate(Resources.Load<GameObject>($"Live2D/{stringValue}"), Live2DFolider.transform);go.transform.position = new Vector3(-999, -999, 2);go.name = go.name.Replace("(Clone)", "");Vector3 worldCoordinate= Vector3.down;;switch (stringValue2){case "Left":worldCoordinate =Camera.main.ScreenToWorldPoint( new Vector3(intValue1-DomoveOffeset, intValue2, 2));Debug.LogError(worldCoordinate);worldCoordinate.z = 2;go.transform.position = worldCoordinate;worldCoordinate = Camera.main.ScreenToWorldPoint(new Vector3(intValue1, intValue2, 0));worldCoordinate.z = 0;go.transform.DOMove(worldCoordinate,DomoveTime);break;case "Right":worldCoordinate =Camera.main.ScreenToWorldPoint( new Vector3(intValue1+DomoveOffeset, intValue2, 2));Debug.LogError(worldCoordinate);worldCoordinate.z = 2;go.transform.position = worldCoordinate;worldCoordinate = Camera.main.ScreenToWorldPoint(new Vector3(intValue1, intValue2, 0));worldCoordinate.z = 0;go.transform.DOMove(worldCoordinate,DomoveTime);break;}}else{Debug.LogError("Failed to parse integers from the input.");}}else{Debug.LogError("Input does not contain 4 comma-separated values.");}}else{Debug.LogError("Input does not match the expected format.");}}else if (VARIABLE.Contains("SETA_")){Match match = Regex.Match(VARIABLE, @"\(([^)]*)\)");if (match.Success){// 获取括号内的内容,并使用逗号分割string[] parts = match.Groups[1].Value.Split(',');if (parts.Length == 3){string stringValue = parts[0];int intValue1, intValue2;if (int.TryParse(parts[1], out intValue1) && int.TryParse(parts[2], out intValue2)){GameObject go = Live2DFolider.transform.Find(parts[0]).gameObject;SetLive2DAlpha(go,intValue1,intValue2);}else{Debug.LogError("Failed to parse integers from the input.");}}else{Debug.LogError("Input does not contain 3 comma-separated values."+VARIABLE+$"length={parts.Length}");}}else{Debug.LogError("Input does not match the expected format.");}}else if (VARIABLE.Contains("FACE")){Match match = Regex.Match(VARIABLE, @"\(([^)]*)\)");if (match.Success){// 获取括号内的内容,并使用逗号分割string[] parts = match.Groups[1].Value.Split(',');if (parts.Length == 2){string stringValue = parts[0];string stringValue2 = parts[1];GameObject go = Live2DFolider.transform.Find(parts[0]).gameObject;SetFace(go,stringValue2);}else{Debug.LogError("Input does not contain 4 comma-separated values.");}}else{Debug.LogError("Input does not match the expected format.");}}else if (VARIABLE.Contains("LEAV_")){Match match = Regex.Match(VARIABLE, @"\(([^)]*)\)");if (match.Success){// 获取括号内的内容,并使用逗号分割string[] parts = match.Groups[1].Value.Split(',');if (parts.Length == 2){string stringValue = parts[0];GameObject go = Live2DFolider.transform.Find(parts[0]).gameObject;Vector3 worldCoordinate;StartCoroutine(des(go));break;IEnumerator des(GameObject go){SetLive2DAlpha(go,(int)(go.transform.Find("Drawables").GetComponentsInChildren<CubismRenderer>()[0].GetComponent<CubismRenderer>().Color.a)*100, 0);Debug.LogError($"Des{go.name}");yield return new WaitForSeconds(0.3f);Destroy(go);}}else{Debug.LogError("Input does not contain 4 comma-separated values.");}}else{Debug.LogError("Input does not match the expected format.");}}}}public void SetLive2DAlpha(GameObject target, int begin,int end){StartCoroutine(AlphaChangerCoroutine(target, begin / 100.0f, end / 100.0f));}private IEnumerator AlphaChangerCoroutine(GameObject target, float begin, float end){foreach (var renderer in target.transform.Find("Drawables").GetComponentsInChildren<CubismRenderer>()){Color currentColor = renderer.Color;currentColor.a = begin;Debug.LogError(currentColor.a);renderer.Color = currentColor;}float duration = 0.1f;int numSteps = 10;float stepTime = duration / numSteps;for (int step = 0; step <= numSteps; step++){float a = begin;if (end>begin){a += (end - begin) / numSteps * step;}else{a -= (begin - end) / numSteps * step;}foreach (var renderer in target.transform.Find("Drawables").GetComponentsInChildren<CubismRenderer>()){
// Debug.LogError(renderer.name);Color currentColor = renderer.Color;currentColor.a = a;Debug.LogError(currentColor.a);renderer.Color = currentColor;}yield return new WaitForSeconds(stepTime);}}public void SetFace(GameObject go, string Animatorname){go.GetComponent<Animator>().Play(Animatorname);}}
最终效果
相关文章:

Unity的live2dgalgame多语言可配置剧情框架
这段代码用于读取表格 using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using OfficeOpenXml; using System.IO; using UnityEngine.Networking; using UnityEngine.UI; using Random UnityEngine.Random;public class Plots…...

再畅通工程(最小生成树)
题目描述:还是畅通工程 某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离。省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可)&…...

前后端分离不可忽视的陷阱,深入剖析挑战,分享解决方案,助你顺利实施分离开发。
不管你设计的系统架构是怎么样,最后都是你的组织内的沟通结构胜出。这个观点一直在组织内不断地被证明,但也不断地被忽略。 前后端分离的利与弊 近几年,随着微服务架构风格的引入、前后端生态的快速发展、多端产品化的出现,前后…...

(四)库存超卖案例实战——优化redis分布式锁
前言 在上一节内容中,我们已经实现了使用redis分布式锁解决商品“超卖”的问题,本节内容是对redis分布式锁的优化。在上一节的redis分布式锁中,我们的锁有俩个可以优化的问题。第一,锁需要实现可重入,同一个线程不用重…...

【ROS入门】雷达、摄像头及kinect信息仿真以及显示
文章结构 雷达信息仿真以及显示Gazebo仿真雷达配置雷达传感器信息xacro文件集成启动仿真环境 Rviz显示雷达数据 摄像头信息仿真以及显示Gazebo仿真摄像头新建xacro文件,配置摄像头传感器信息xacro文件集成启动仿真环境 Rviz显示摄像头数据 kinect信息仿真以及显示Ga…...

实用篇-认识微服务
一、服务架构演变 1. 单体架构 单体架构:将业务的所有功能集中在一个项目中开发,打成一个包部署 单体架构的优点: 架构简单部署成本低 单体架构的缺点: 耦合度高 2. 分布式架构 分布式架构: 根据业务功能对系…...

【产品运营】产品需求应该如何管理
产品项目在进行时经常会有一些需求需要实现,需求是产品更新迭代的动力,需求也是从用户诉求转化而来;在做需求管理时,我们需要判断一个需求的优先级等方面,对产品进行优化; 目录: 一、 为什么要…...

Linux 系统调用IO口,利用光标偏移实现文件复制
用系统调用IO函数实现从一个文件读取最后2KB数据并复制到另一个文件中,源文件以只读方式打开,目标文件以只写的方式打开,若目标文件不存在,可以创建并设置初始值为0664,写出相应代码,要对出错情况有一定的处…...

【原创】指针变量作为函数参数要点注意
指针变量作为函数参数要点注意(已写至笔记) 1传参指针不加*(main中函数) 2收参指针要加*(被main调用的函数) 3传参指针名可与收参指针名不同,不影响 4【问】如何看主函数中指针所指内容是否改变…...

SpringMVC Day 04 : 数据绑定
前言 SpringMVC是一个非常流行的Java Web框架,它提供了很多方便的功能和工具来帮助我们构建高效、灵活的Web应用程序。其中,数据绑定就是SpringMVC中非常重要的一部分,它可以帮助我们方便地将请求参数绑定到Java对象上,从而简化了…...

2.3.1 协程设计原理与汇编实现
1.为什么要有协程? 同步的编程方式,异步的性能。同步编程时,我们需要等待io就绪。但是在协程这里,我们使用一种机制,当io需要等待时,就切到下一个io,之后当之前的io就绪时,再切换回来…...

J2EE项目部署与发布(Windows版本)->会议OA单体项目Windows部署,spa前后端分离项目Windows部署
会议OA单体项目Windows部署spa前后端分离项目Windows部署 1.会议OA单体项目Windows部署(以实施的角度) 将项目放入webapp,项目能够访问: 首先拿到war包和数据库脚本,并检查是否有什么问题。 如何查看项目报错信息(当你…...

Lua脚本语言
1. 概念 Lua(发音为"loo-ah",葡萄牙语中的"lua"意为月亮)是一种轻量级的、高效的、可嵌入的脚本编程语言。官网Lua最初由巴西计算机科学家Roberto Ierusalimschy、Waldemar Celes和Luiz Henrique de Figueiredo于1993年开…...

cat()函数和print()函数的区别
目录 区别一: 区别二: cat、print函数都是输出函数。 区别一: cat()函数不能赋值; print()函数可以赋值。 x<-cat("hello world") //赋值 hello world x //cat函数无返回值 NULLy<-print("hello …...

宝塔面板安装Python和Flask(新版Python项目)
(一)宝塔面板的项目菜单,打开Python项目的“项目版本管理” 安装Python版本3.10.0。 会创建一个Python版本的文件夹www/server/pyproject_evn/versions/ 会创建一个Python虚拟环境的文件夹www/server/pyproject_evn/python_venv/ …...

火柴排队.
题意:给两列火柴,可以交换任意相邻的火柴,使得(ai-bi)^2的和最小,求最小交换次数。 分析:使得(ai-bi)^2的和最小,即a^2-2abb^2的和最小,那么使得2ab最大,就可…...

改善游戏体验:数据分析与可视化的威力
当今,电子游戏已经超越了娱乐,成为一种文化现象,汇聚了全球数十亿的玩家。游戏制作公司正采用越来越复杂的技术来提高游戏质量,同时游戏数据分析和可视化工具变得不可或缺。 数据的力量:解析游戏体验 游戏制作涉及到大…...

GEE:本地影像上传到GEE的Assets中,并输入机器学习算法中作为特征变量
作者:CSDN @ _养乐多_ 当我们在 Google Earth Engine(GEE)中应用机器学习算法时,会输入一些影像作为特征变量数据,进一步根据这些特征影像去推理未知区域的数据。但是 GEE 平台上计算特征变量的 API 函数并不是非常全面,我们希望获得更多的特征用于分类。这个时候,我们…...

【Mybatis源码】XMLConfigBuilder构建器 - 读取XML配置初始化Configuration对象
XMLConfigBuilder是Mybatis中定义的进行构建Configuration对象的类,此类用于读取XML配置文件创建并初始化Configuration对象; 上一篇中我们介绍了XMLConfigBuilder构建器加载XML配置文件以及创建Configuration对象https://blog.csdn.net/m1729339749/article/details/133983…...

Python算法练习 10.28
leetcode 700 二叉搜索树中的搜索 给定二叉搜索树(BST)的根节点 root 和一个整数值 val。 你需要在 BST 中找到节点值等于 val 的节点。 返回以该节点为根的子树。 如果节点不存在,则返回 null 。 示例 1: 输入:root [4,2,7,1,…...

【java学习—八】单例设计模式(5)
文章目录 1. 相关概念2. 单例设计模式-饿汉式3. 单例设计模式-懒汉式4. 总结 1. 相关概念 单例:只有一个实例(实例化对象) 设计模式是在大量的实践中总结和理论化之后优选的代码结构、编程风格、以及解决问题的思考方式。设计模式就像是经典的…...

【设计模式】第4节:创建型模式之“单例模式”
一、介绍 采取一定的方法保证在整个的软件系统中,对某个类只能存在一个对象实例,并且该类只提供一个取得其对象实例的方法。 不使用单例模式的UML类图: 使用单例模式的UML类图: 使用场景: 需要频繁创建或销毁的对象…...

NodeJS爬取墨刀上的设计图片
背景 设计人员分享了一个墨刀的原型图,但是给的是只读权限,无法下载其中的素材;开发时想下载里面的一张动图,通过浏览器的F12工具在页面结构找到了图片地址。 但是浏览器直接访问后发现没权限: Nginx 的 403 页面。。…...

linux--
一、crond 任务调度 1、原理示意图 2、crontab 进行定时任务的设置 2.1. 概述 任务调度,是指系统在某个时间执行的特定的命令或程序。任务调度分类: 系统工作: 有些重要的工作必须周而复始地执行。如病毒扫描等 个别用户工作:个别用户可能希望执行某些…...

conda虚拟环境笔记收录
1、安装conda 增加执行权限: chmod x Anaconda3-2023.03-1-Linux-x86_64.sh 开始执行:./Anaconda3-2023.03-1-Linux-x86_64.sh2、查看版本 conda --version3、查看当前虚拟环境 虚拟环境和全局环境有前缀可见 如果不进行设置,重新启动就变成…...

RGB-T Salient Object Detection via Fusing Multi-Level CNN Features
ADFC means ‘adjacent-depth feature combination’,MGF means ‘multi-branch group fusion’,JCSA means ‘joint channel-spatial attention’,JABMP means ‘joint attention guided bi-directional message passing’ 作者未提供代…...

安卓开发实例:方向传感器
调用手机的方向传感器,X轴,Y轴,Z轴的数值 activity_sensor.xml <?xml version"1.0" encoding"utf-8"?> <androidx.constraintlayout.widget.ConstraintLayoutxmlns:android"http://schemas.android.c…...

[论文笔记]GTE
引言 今天带来今年的一篇文本嵌入论文GTE, 中文题目是 多阶段对比学习的通用文本嵌入。 作者提出了GTE,一个使用对阶段对比学习的通用文本嵌入。使用对比学习在多个来源的混合数据集上训练了一个统一的文本嵌入模型,通过在无监督预训练阶段和有监督微调阶段显著增加训练数…...

Prometheus字段解析
官方文档::Configuration | Prometheus 1、全局配置指定在所有其他配置上下文中有效的参数。它们还用作其他配置部分的默认值。 global:# How frequently to scrape targets by default.默认情况下,定期抓取目标的频率是多久一次?…...

msigdbr hallmarks gsea broad研究所
使用msigdbr r包 #BiocManager::install("msigdb") #https://www.gsea-msigdb.org/gsea/msigdb #https://cran.r-project.org/web/packages/msigdbr/vignettes/msigdbr-intro.html #https://bioconductor.org/packages/release/data/experiment/vignettes/msigdb/ins…...