电子商务网站建设的方法与流程,wordpress如何自定义小工具栏,合肥专业商业网站,模板建站小程序一、前言
最近在学习unity2D#xff0c;想做一个横版过关游戏#xff0c;需要按键设置功能#xff0c;让用户可以自定义方向键与攻击键等。
自己写了一个#xff0c;总结如下。
二、界面效果图 这个是一个csv文件#xff0c;准备第一列是中文按键说明#xff0c;第二列…一、前言
最近在学习unity2D想做一个横版过关游戏需要按键设置功能让用户可以自定义方向键与攻击键等。
自己写了一个总结如下。
二、界面效果图 这个是一个csv文件准备第一列是中文按键说明第二列是英文第三列是日文还没有翻译第四列是默认按键名称第五列是默认按键ascII码如果用户选了恢复默认设置就会用到第六列是用户自己设置的按键名称第七列是用户自己设置的按键ascII码第八列保留暂未使用。 这个是首页如果选到了这个按钮就是按键设置页面。 这个是按键设置页面实现了按 上下/用户设置的上下移动光标按回车/ 开始键确定然后按另一个键进行按键修改。 图中灰色块就是光标还没有找图片最后3行按钮进行了特殊处理
三、主要部分代码
unity代码比较多总结下主要用到的3个类。
1.FileManager.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEngine.SceneManagement;
using System.Text;public class FileManager : MonoSingleTonFileManager
{private bool isInit false;public TextAsset keyConfigCsv;//012列是语言3是默认名称4是默认ascii56是用户设置的//这个要装第二行的数据/*wsadjkliqeuocvjk*///语言这个不能修改只能读取public static string[] LanguageWasd new string[16];private static string[] LanguageWasd0 new string[16];private static string[] LanguageWasd1 new string[16];private static string[] LanguageWasd2 new string[16];//默认的key的str与int这个不能修改只能读取public static int[] DefaultWasdNumKeys new int[16];public static string[] DefaultWasdNumKeysStr new string[16];//实际用的key的str与intpublic static int[] WasdNumKeys new int[16];public static string[] WasdNumKeysStr new string[16];//临时用的key的str与int因为可能只修改不保存所以用public static int[] WasdNumKeysTemp new int[16];public static string[] WasdNumKeysStrTemp new string[16];// Start is called before the first frame update// 这个方法会在脚本被启用时注册监听事件void OnEnable(){//Debug.Log(进入这个场景playerData);//Debug.Log(执行完毕这个场景playerData);}private void Awake(){if (!isInit){InitKeyConfig();}}void Start(){}// Update is called once per framevoid Update(){}private bool InitKeyConfig(){string path GetKeyConfigPath();Debug.Log(path);if (File.Exists(path)){//用用户的初始化LoadKeyConfig(File.ReadAllText(path).Split(\n));Debug.Log(使用用户的初始化);isInit true;return false;}else{string text keyConfigCsv.text;//把默认的文件写进本地File.WriteAllText(path, text, Encoding.UTF8);//用默认的初始化LoadKeyConfig(text.Split(\n));//string[] dataRow text.Split(\n);//File.WriteAllLines(path, dataRow);Debug.Log(使用默认的初始化);isInit true;return true;}}//读取到临时变量里public void LoadKeyConfig(string[] dataRow){int language LanguageManager.GetLanguage();//File.WriteAllLines(path, dataRow);for(int i 0; i dataRow.Length; i){string[] dataCell dataRow[i].Split(,);if (i 16){break;}else{LanguageWasd[i] dataCell[language];LanguageWasd0[i] dataCell[0];LanguageWasd1[i] dataCell[1];LanguageWasd2[i] dataCell[2];DefaultWasdNumKeysStr[i] dataCell[3];DefaultWasdNumKeys[i] int.Parse(dataCell[4]);WasdNumKeysStr[i] dataCell[5];WasdNumKeysStrTemp[i] dataCell[5];WasdNumKeys[i] int.Parse(dataCell[6]);WasdNumKeysTemp[i] int.Parse(dataCell[6]);}}}private static void SaveKeyConfig(string[] dataRow){//Application.dataPath//这个打包后就不能用了可能没权限string path GetKeyConfigPath();if (File.Exists(path)){//删了重新创建File.Delete(path);File.CreateText(path).Close();}else{File.CreateText(path).Close();}//Liststring datas2 new Liststring();//datas.Add(coins,1);Debug.Log(写入数据);File.WriteAllLines(path, dataRow, Encoding.UTF8);Debug.Log(写入数据完毕);}public void ResetKeyConfig(){string text keyConfigCsv.text;//用默认的初始化LoadKeyConfig(text.Split(\n));//并且保存SaveKeyConfig(text.Split(\n));//SceneManager.LoadScene(0);}public static string GetKeyConfigPath(){return Application.persistentDataPath /KeyConfig.csv;}public static void SaveKeyConfig(){string[] newData new string[16];//保存文件for(int i0; i16; i){newData[i] LanguageWasd0[i] , LanguageWasd1[i] , LanguageWasd2[i] , DefaultWasdNumKeysStr[i] , DefaultWasdNumKeys[i] , WasdNumKeysStr[i] , WasdNumKeys[i] , ;;}SaveKeyConfig(newData);Debug.Log(保存键盘信息完毕);}}
这个类负责操作配置文件要把内置的配置文件保存到本地配置文件中以及读取配置文件。要区分读取内置的还是本地的
2.TitleScreen.cs
using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;public class TitleScreen : MonoBehaviour
{TextMeshProUGUI tmpTitleText;bool calledNextScene;bool inputDetected false;bool isTitle true;bool isKeySetting false;bool isLanguageSetting false;int alphaKeyPressText 255;bool alphaKeyPressTextShow true;public AudioClip keyPressClip;public AudioClip keyChangeClip;[SerializeField] Image[] buttons new Image[5];[SerializeField] Text[] buttonsText new Text[5];private int buttonsChoosedNum 0;private enum TitleScreenStates { WaitForInput, NextScene };TitleScreenStates titleScreenState TitleScreenStates.WaitForInput;#if UNITY_STANDALONEstring insertKeyPressText PRESS ANY KEY;
#endif#if UNITY_ANDROID || UNITY_IOSstring insertKeyPressText TAP TO START;
#endifstring titleText
size18color#ffffff{0:X2}{1}/color/size;private void Awake(){//tmpTitleText GameObject.Find(TitleText).GetComponentTextMeshProUGUI();}// Start is called before the first frame updatevoid Start(){//tmpTitleText.alignment TextAlignmentOptions.Center;//tmpTitleText.alignment TextAlignmentOptions.Midline;//tmpTitleText.fontStyle FontStyles.UpperCase;titleScreenState TitleScreenStates.WaitForInput;if (isHaveSavePoint()){initButton(1);}else {initButton(0);}}// Update is called once per framevoid Update(){switch(titleScreenState){case TitleScreenStates.WaitForInput://buttonsText[buttonsChoosedNum].text String.Format(titleText, alphaKeyPressText, buttonsText[buttonsChoosedNum].text);buttonsText[buttonsChoosedNum].enabled alphaKeyPressTextShow;if (!inputDetected){ChooseButton();SelectButton();}break;case TitleScreenStates.NextScene:if (!calledNextScene){GameManager.Instance.StartNextScene();calledNextScene true;}break;}}private IEnumerator FlashTitleText() { for(int i 0; i 5; i){alphaKeyPressTextShow true;alphaKeyPressText 0;yield return new WaitForSeconds(0.1f);alphaKeyPressTextShow false;alphaKeyPressText 255;yield return new WaitForSeconds(0.1f);}alphaKeyPressTextShow true;alphaKeyPressText 0;yield return new WaitForSeconds(0.1f);titleScreenState TitleScreenStates.NextScene;}private IEnumerator FlashTitleTextAndOpenKeyConfig(){for (int i 0; i 5; i){alphaKeyPressTextShow true;alphaKeyPressText 0;yield return new WaitForSeconds(0.1f);alphaKeyPressTextShow false;alphaKeyPressText 255;yield return new WaitForSeconds(0.1f);}alphaKeyPressTextShow true;alphaKeyPressText 0;yield return new WaitForSeconds(0.1f);//退出的时候需要改为接受输入KeyConfigScript.Instance.Show(() { inputDetected false;Debug.Log(回调方法); });}private bool isHaveSavePoint() {return false;}private void ChooseButton() {if ( Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown((KeyCode)FileManager.WasdNumKeys[0]) ){if (buttonsChoosedNum 0){buttons[buttonsChoosedNum].enabled false;buttonsChoosedNum--;buttons[buttonsChoosedNum].enabled true;PlayChangeButton();}else{PlayChangeButton();}}if (Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown((KeyCode)FileManager.WasdNumKeys[1]) ){if (buttonsChoosedNum buttons.Length - 1){buttons[buttonsChoosedNum].enabled false;buttonsChoosedNum;buttons[buttonsChoosedNum].enabled true;PlayChangeButton();}else{PlayChangeButton();}}}private void SelectButton() { if( Input.GetKeyDown(KeyCode.Return) || Input.GetKeyDown((KeyCode)FileManager.WasdNumKeys[13]) ){if (buttonsChoosedNum 0){inputDetected true;StartCoroutine(FlashTitleText());SoundManager.Instance.Play(keyPressClip);} else if (buttonsChoosedNum 1) {inputDetected true;SoundManager.Instance.Play(keyPressClip);}else if (buttonsChoosedNum 2){inputDetected true;SoundManager.Instance.Play(keyPressClip);}else if (buttonsChoosedNum 3){inputDetected true;SoundManager.Instance.Play(keyPressClip);StartCoroutine(FlashTitleTextAndOpenKeyConfig());}else if (buttonsChoosedNum 4){Application.Quit();}}}private void PlayChangeButton() { if(keyChangeClip ! null){SoundManager.Instance.Play(keyChangeClip);}}private void initButton(int n) {buttonsChoosedNum n;for(int i 0; i buttons.Length; i){if (i n){buttons[i].enabled true;}else {buttons[i].enabled false;}}}}
这个是游戏首页用的类主要管是否显示按键设置页面 按键设置页面也在首页是个Canvas对象刚开始会隐藏选择按键设置按钮并确定后才会展示。
3.KeyConfigScript.cs
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.UIElements;public class KeyConfigScript : MonoBehaviour
{bool flashText false;public static KeyConfigScript Instance null;[SerializeField] Canvas canvas;public AudioClip keyChangeClip;public AudioClip keySelectClip;/*wsadjkliqeuocvjk*/public UnityEngine.UI.Image[] buttons new UnityEngine.UI.Image[19];[SerializeField] Text[] buttonsText new Text[19];[SerializeField] ScrollRect scrollRect;private Action nowScreenAction;private int buttonsChoosedNum 0;bool alphaKeyPressTextShow true;//检测按键bool canInput false;bool canMove true;private void Awake(){// If there is not already an instance of SoundManager, set it to this.if (Instance null){Instance this;}//If an instance already exists, destroy whatever this object is to enforce the singleton.else if (Instance ! this){Destroy(gameObject);}//Set SoundManager to DontDestroyOnLoad so that it wont be destroyed when reloading our scene.DontDestroyOnLoad(gameObject);//默认隐藏Close();}private void OnEnable(){}private void initKeysObjects() {//读取下当前配置文件获得每个按键配置的名称和值for (int i 0; i buttons.Length; i){if (i 16){buttons[i].enabled false;}else{buttonsText[i].text FileManager.LanguageWasd[i] FileManager.WasdNumKeysStr[i];//初始化方法只有0才是if (i 0){buttons[i].enabled true;}else{buttons[i].enabled false;}}}}private void ChooseButton(){if ( Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown((KeyCode)FileManager.WasdNumKeys[0]) ){if (buttonsChoosedNum 0){buttons[buttonsChoosedNum].enabled false;buttonsChoosedNum--;buttons[buttonsChoosedNum].enabled true;PlayChangeButton();ScrollUpOrDown(buttonsChoosedNum, false);}else{//移动到最后一个buttons[buttonsChoosedNum].enabled false;buttonsChoosedNum buttons.Length - 1;buttons[buttonsChoosedNum].enabled true;PlayChangeButton();}}if (Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown((KeyCode)FileManager.WasdNumKeys[1]) ){if (buttonsChoosedNum buttons.Length - 1){buttons[buttonsChoosedNum].enabled false;buttonsChoosedNum;buttons[buttonsChoosedNum].enabled true;PlayChangeButton();ScrollUpOrDown(buttonsChoosedNum, true);}else{//移动到第一个buttons[buttonsChoosedNum].enabled false;buttonsChoosedNum 0;buttons[buttonsChoosedNum].enabled true;PlayChangeButton();}}}private void SelectButton(){//如果是可以移动状态if (canMove){//如果按下选择键if ( Input.GetKeyDown(KeyCode.Return) || Input.GetKeyDown((KeyCode)FileManager.WasdNumKeys[13]) ) {//播放声音PlaySelectButton();//关闭移动canMove false;//如果是这些键特殊处理然后返回switch (buttonsChoosedNum){case 16:StartCoroutine(FlashTitleTextSecond(Reset));return;case 17:StartCoroutine(FlashTitleTextSecond(SaveAndClose));return;case 18:StartCoroutine(FlashTitleTextSecond(Close));return;}//如果是普通键//闪烁标志位打开flashText true;//字幕闪烁StartCoroutine(FlashTitleText());}}else{BeforeSettingKey();}}private void BeforeSettingKey(){if (Input.anyKeyDown){//需要看这个键能不能设置bool canSetting false;foreach (KeyCode key in System.Enum.GetValues(typeof(KeyCode))){if (Input.GetKeyDown(key)){// 检查按键是否为可打印字符if ((key KeyCode.A key KeyCode.Z) || (key KeyCode.Alpha0 key KeyCode.Alpha9)){// 将字符转换为 ASCII 码string keyStr key.ToString();//char keyChar keyStr[0];int asciiValue (int)key;Debug.Log($按下的键 {key} 对应的 ASCII 码值是{asciiValue});SettingKey(keyStr, asciiValue);canSetting true;break;}else{// 非字母数字键你可以根据需求处理这些按键Debug.Log($按下了非字符键{key});if (key KeyCode.Space){SettingKey(Space, 32);canSetting true;break;}else if (key KeyCode.Return){SettingKey(Enter, 13);canSetting true;break;}else if (key KeyCode.UpArrow){SettingKey(Up, 273);canSetting true;break;}else if (key KeyCode.DownArrow){SettingKey(Down, 274);canSetting true;break;}else if (key KeyCode.LeftArrow){SettingKey(Left, 276);canSetting true;break;}else if (key KeyCode.RightArrow){SettingKey(Right, 275);canSetting true;break;}}}}//如果可以设置再进行后续操作if (canSetting){//停止闪烁StartCoroutine(ResetTitleText());}}}private void SettingKey(string str, int ascII){//更新设置的信息FileManager.WasdNumKeysStrTemp[buttonsChoosedNum] str;FileManager.WasdNumKeysTemp[buttonsChoosedNum] ascII;//更新按键文本信息buttonsText[buttonsChoosedNum].text FileManager.LanguageWasd[buttonsChoosedNum] str;}private IEnumerator FlashTitleText(){while (flashText) { alphaKeyPressTextShow true;yield return new WaitForSeconds(0.1f);alphaKeyPressTextShow false;yield return new WaitForSeconds(0.1f);}}private IEnumerator FlashTitleTextSecond(Funcbool func){for (int i0; i5; i){alphaKeyPressTextShow true;yield return new WaitForSeconds(0.1f);alphaKeyPressTextShow false;yield return new WaitForSeconds(0.1f);}alphaKeyPressTextShow true;yield return new WaitForSeconds(0.1f);//闪烁完毕后才能移动canMove true;//闪烁完毕后执行func();}private IEnumerator ResetTitleText(){flashText false;yield return new WaitForSeconds(0.2f);alphaKeyPressTextShow true;buttonsText[buttonsChoosedNum].enabled true;canMove true;}private void PlayChangeButton(){if (keyChangeClip ! null){SoundManager.Instance.Play(keyChangeClip);}}private void PlaySelectButton(){if (keySelectClip ! null){SoundManager.Instance.Play(keySelectClip);}}//先从配置文件读取配置信息void Start(){}// Update is called once per framevoid Update(){if (canInput){buttonsText[buttonsChoosedNum].enabled alphaKeyPressTextShow;if (canMove){ChooseButton();}SelectButton();}}//翻页现在不用private void ScrollUpOrDown(int count, bool isDown){/*if(count 8 isDown){scrollRect.normalizedPosition new Vector2(0, 0);}else if (count 7 !isDown) {scrollRect.normalizedPosition new Vector2(0, 1);}*/}public void Show(Action action) {//翻页现在不用//scrollRect.normalizedPosition new Vector2(0, 0);alphaKeyPressTextShow true;buttonsChoosedNum 0;initKeysObjects();this.nowScreenAction action;Debug.Log(show Key Config);canvas.enabled true;canInput true;}private bool Reset(){for (int i 0; i 16; i){//先把text内容改了buttonsText[i].text FileManager.LanguageWasd[i] FileManager.DefaultWasdNumKeysStr[i];//然后把临时数组改了FileManager.WasdNumKeysStrTemp[i] FileManager.DefaultWasdNumKeysStr[i];FileManager.WasdNumKeysTemp[i] FileManager.DefaultWasdNumKeys[i];}return true;}private bool Save(){for (int i 0; i 16; i){//给实际用的赋值FileManager.WasdNumKeysStr[i] FileManager.WasdNumKeysStrTemp[i];FileManager.WasdNumKeys[i] FileManager.WasdNumKeysTemp[i];}//写入文件FileManager.SaveKeyConfig();return true;}private bool SaveAndClose(){Save();Close();return true;}public bool Close() {canvas.enabled false;canInput false;//上一个屏幕改为接受输入if(nowScreenAction ! null){nowScreenAction();}return true;//nowScreenManager.GetComponentTitleScreen().inputDetected false;}}
这个类就是按键设置页面用的类有光标上下移动功能、按钮选择后让字幕闪烁的功能、再次按键后设置新按键功能。
四、备注
unity代码比较复杂直接复制粘贴是无法使用的每人的场景元素也不一样很多变量会不存在仅供参考。
以上就是个人编写的设置按键的代码大致逻辑如下
1.游戏启动后先判断有没有本地配置文件如果有、就读取本地配置文件并初始化如果没有就用内置配置文件初始化并把内置配置文件保存到本地。
2.进入按键选择页面时上下方向键移动光标按回车可以选择按键此时按键闪烁再按另一个键可以设置为新按键。
3.有恢复默认设置功能有保存并退出功能有直接退出功能如果选保存并退出才把设置的临时按键数组赋值到实际使用的按键数组并保存到本地配置文件。