键位配置持久化

This commit is contained in:
ALIENJACK\alien 2024-08-06 18:52:11 +08:00
parent 580de2d245
commit b8c116aa28
4 changed files with 42 additions and 136 deletions

View File

@ -1,126 +0,0 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 66fc8233a79cd254f8d005452dcd4ac0, type: 3}
m_Name: ControlSettings
m_EditorClassIdentifier:
Player1:
UP:
buttonType: 1
keyCode: 119
DOWN:
buttonType: 2
keyCode: 115
LEFT:
buttonType: 4
keyCode: 97
RIGHT:
buttonType: 8
keyCode: 100
A:
buttonType: 16
keyCode: 107
B:
buttonType: 32
keyCode: 106
SELECT:
buttonType: 64
keyCode: 118
START:
buttonType: 128
keyCode: 98
MIC:
buttonType: 256
keyCode: 0
Player2:
UP:
buttonType: 1
keyCode: 0
DOWN:
buttonType: 2
keyCode: 0
LEFT:
buttonType: 4
keyCode: 0
RIGHT:
buttonType: 8
keyCode: 0
A:
buttonType: 16
keyCode: 0
B:
buttonType: 32
keyCode: 0
SELECT:
buttonType: 64
keyCode: 0
START:
buttonType: 128
keyCode: 0
MIC:
buttonType: 256
keyCode: 0
Player3:
UP:
buttonType: 1
keyCode: 0
DOWN:
buttonType: 2
keyCode: 0
LEFT:
buttonType: 4
keyCode: 0
RIGHT:
buttonType: 8
keyCode: 0
A:
buttonType: 16
keyCode: 0
B:
buttonType: 32
keyCode: 0
SELECT:
buttonType: 64
keyCode: 0
START:
buttonType: 128
keyCode: 0
MIC:
buttonType: 256
keyCode: 0
Player4:
UP:
buttonType: 1
keyCode: 0
DOWN:
buttonType: 2
keyCode: 0
LEFT:
buttonType: 4
keyCode: 0
RIGHT:
buttonType: 8
keyCode: 0
A:
buttonType: 16
keyCode: 0
B:
buttonType: 32
keyCode: 0
SELECT:
buttonType: 64
keyCode: 0
START:
buttonType: 128
keyCode: 0
MIC:
buttonType: 256
keyCode: 0

View File

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: fa3a6bfd9566da84eb494ff280abe34c
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant:

View File

@ -101,7 +101,7 @@ namespace AxibugEmuOnline.Client
public ControllerState GetControllerState() public ControllerState GetControllerState()
{ {
var mapper = Resources.Load<NesControllerMapper>("NES/ControlSettings"); var mapper = NesControllerMapper.Get();
return mapper.CreateState(); return mapper.CreateState();
} }
} }

View File

@ -1,16 +1,37 @@
using System; using System;
using System.IO;
using UnityEngine; using UnityEngine;
using VirtualNes.Core; using VirtualNes.Core;
namespace AxibugEmuOnline.Client namespace AxibugEmuOnline.Client
{ {
public class NesControllerMapper : ScriptableObject public class NesControllerMapper
{ {
private static readonly string ConfigFilePath = $"{Application.persistentDataPath}/NES/ControllerMapper.json";
public MapperSetter Player1 = new MapperSetter(); public MapperSetter Player1 = new MapperSetter();
public MapperSetter Player2 = new MapperSetter(); public MapperSetter Player2 = new MapperSetter();
public MapperSetter Player3 = new MapperSetter(); public MapperSetter Player3 = new MapperSetter();
public MapperSetter Player4 = new MapperSetter(); public MapperSetter Player4 = new MapperSetter();
public NesControllerMapper()
{
Player1.UP.keyCode = KeyCode.W;
Player1.DOWN.keyCode = KeyCode.S;
Player1.LEFT.keyCode = KeyCode.A;
Player1.RIGHT.keyCode = KeyCode.D;
Player1.B.keyCode = KeyCode.J;
Player1.A.keyCode = KeyCode.K;
Player1.SELECT.keyCode = KeyCode.V;
Player1.START.keyCode = KeyCode.B;
}
public void Save()
{
var jsonStr = JsonUtility.ToJson(this);
File.WriteAllText(ConfigFilePath, jsonStr);
}
public ControllerState CreateState() public ControllerState CreateState()
{ {
var state1 = Player1.GetButtons(); var state1 = Player1.GetButtons();
@ -21,6 +42,25 @@ namespace AxibugEmuOnline.Client
return new ControllerState(state1, state2, state3, state4); return new ControllerState(state1, state2, state3, state4);
} }
private static NesControllerMapper s_setting;
public static NesControllerMapper Get()
{
if (s_setting == null)
{
try
{
var json = File.ReadAllText($"{Application.persistentDataPath}/Nes/ControllerMapper.json");
s_setting = JsonUtility.FromJson<NesControllerMapper>(json);
}
catch
{
s_setting = new NesControllerMapper();
}
}
return s_setting;
}
[Serializable] [Serializable]
public class Mapper public class Mapper
{ {