分离和整理代码,去掉不必要携程,保存上次游戏名称

This commit is contained in:
sin365 2024-08-08 16:24:18 +08:00
parent 89bab5f308
commit d553052006
12 changed files with 168 additions and 95 deletions

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: 8068b465f3dd6864f807fa17ca4bd813 guid: fae13461b22160c47b5ee9964058b719
PluginImporter: PluginImporter:
externalObjects: {} externalObjects: {}
serializedVersion: 2 serializedVersion: 2

View File

@ -838,7 +838,8 @@ MonoBehaviour:
btnStop: {fileID: 1268518242613896174} btnStop: {fileID: 1268518242613896174}
btnStart: {fileID: 3471319445208116035} btnStart: {fileID: 3471319445208116035}
btnRomDir: {fileID: 0} btnRomDir: {fileID: 0}
mChangeRomName: mslug3 bQuickTestRom: 0
mQuickTestRom:
--- !u!114 &3471319444260926879 --- !u!114 &3471319444260926879
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -1665,7 +1666,7 @@ GameObject:
m_Component: m_Component:
- component: {fileID: 3471319445290242363} - component: {fileID: 3471319445290242363}
m_Layer: 5 m_Layer: 5
m_Name: KeyPad m_Name: tfKeyPad
m_TagString: Untagged m_TagString: Untagged
m_Icon: {fileID: 0} m_Icon: {fileID: 0}
m_NavMeshLayer: 0 m_NavMeshLayer: 0
@ -1691,9 +1692,9 @@ RectTransform:
- {fileID: 809123066073945339} - {fileID: 809123066073945339}
- {fileID: 5041497614654165726} - {fileID: 5041497614654165726}
- {fileID: 175721420956369396} - {fileID: 175721420956369396}
- {fileID: 3471319446131231223}
- {fileID: 3471319446251103890}
- {fileID: 4705889093872201058} - {fileID: 4705889093872201058}
- {fileID: 3471319446251103890}
- {fileID: 3471319446131231223}
m_Father: {fileID: 3471319444310732615} m_Father: {fileID: 3471319444310732615}
m_RootOrder: 1 m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
@ -2557,7 +2558,7 @@ RectTransform:
m_Children: m_Children:
- {fileID: 3471319445650276455} - {fileID: 3471319445650276455}
m_Father: {fileID: 3471319445290242363} m_Father: {fileID: 3471319445290242363}
m_RootOrder: 9 m_RootOrder: 11
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 1} m_AnchorMin: {x: 0, y: 1}
m_AnchorMax: {x: 0, y: 1} m_AnchorMax: {x: 0, y: 1}
@ -3393,7 +3394,7 @@ RectTransform:
m_Children: m_Children:
- {fileID: 4705889095025944828} - {fileID: 4705889095025944828}
m_Father: {fileID: 3471319445290242363} m_Father: {fileID: 3471319445290242363}
m_RootOrder: 11 m_RootOrder: 9
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0} m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 1, y: 1} m_AnchorMax: {x: 1, y: 1}

View File

@ -265,9 +265,17 @@ PrefabInstance:
propertyPath: m_LocalEulerAnglesHint.z propertyPath: m_LocalEulerAnglesHint.z
value: 0 value: 0
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 3471319444260926877, guid: 1cb6308e67105bf48aea153ebcdb2d76, type: 3}
propertyPath: bQuickTestRom
value: 0
objectReference: {fileID: 0}
- target: {fileID: 3471319444260926877, guid: 1cb6308e67105bf48aea153ebcdb2d76, type: 3}
propertyPath: mQuickTestRom
value: 1944
objectReference: {fileID: 0}
- target: {fileID: 3471319444260926877, guid: 1cb6308e67105bf48aea153ebcdb2d76, type: 3} - target: {fileID: 3471319444260926877, guid: 1cb6308e67105bf48aea153ebcdb2d76, type: 3}
propertyPath: mChangeRomName propertyPath: mChangeRomName
value: mslug2 value: opwolf
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 3471319445309129982, guid: 1cb6308e67105bf48aea153ebcdb2d76, type: 3} - target: {fileID: 3471319445309129982, guid: 1cb6308e67105bf48aea153ebcdb2d76, type: 3}
propertyPath: m_IsActive propertyPath: m_IsActive

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: bc7cc4529e6fe0b43baa64e8494efcd0
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,35 @@
using UnityEngine;
namespace Assets.Script.UMAME.Common
{
public class UniMAMESetting
{
public static UniMAMESetting instance
{
get
{
if (mInstance == null)
mInstance = new UniMAMESetting();
return mInstance;
}
}
private static UniMAMESetting mInstance;
const string KEY_LASTGAMEROM = "MAME_LASTGAMEROM";
public string LastGameRom
{
get
{
if(PlayerPrefs.HasKey(KEY_LASTGAMEROM))
return PlayerPrefs.GetString(KEY_LASTGAMEROM);
return string.Empty;
}
set
{
PlayerPrefs.SetString(KEY_LASTGAMEROM, value);
}
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8e93a2fd757533948a43218e424841a7
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,17 +1,16 @@
using Assets.Script.UMAME.Common;
using mame; using mame;
using MAME.Core.Common; using MAME.Core.Motion;
using System; using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Threading;
using UnityEngine; using UnityEngine;
using UnityEngine.UI; using UnityEngine.UI;
public class UMAME : MonoBehaviour public class UMAME : MonoBehaviour
{ {
mainMotion mainmotion; MameMainMotion emu;
UniLog mUniLog; UniLog mUniLog;
UniMouse mUniMouse; UniMouse mUniMouse;
UniVideoPlayer mUniVideoPlayer; UniVideoPlayer mUniVideoPlayer;
@ -19,17 +18,19 @@ public class UMAME : MonoBehaviour
UniKeyboard mUniKeyboard; UniKeyboard mUniKeyboard;
UniResources mUniResources; UniResources mUniResources;
public Text mFPS; public Text mFPS;
public Button btnStop; public Button btnStop;
public Button btnStart; public Button btnStart;
public Button btnRomDir; public Button btnRomDir;
public Dictionary<string, RomInfo> ALLGame; public Dictionary<string, RomInfo> ALLGame;
public List<RomInfo> HadGameList = new List<RomInfo>(); public List<RomInfo> HadGameList = new List<RomInfo>();
string mChangeRomName = string.Empty;
public bool bQuickTestRom = false;
public string mQuickTestRom = string.Empty;
Dropdown optionDropdown; Dropdown optionDropdown;
public static System.Diagnostics.Stopwatch sw = System.Diagnostics.Stopwatch.StartNew(); public static System.Diagnostics.Stopwatch sw = System.Diagnostics.Stopwatch.StartNew();
public static bool bStart { get; private set; } public static bool bInGame { get; private set; }
#if UNITY_EDITOR_WIN #if UNITY_EDITOR_WIN
@ -42,25 +43,35 @@ public class UMAME : MonoBehaviour
public static string RomPath => Application.persistentDataPath; public static string RomPath => Application.persistentDataPath;
#endif #endif
public string mChangeRomName = string.Empty;
private void Awake() private void Awake()
{ {
mFPS = GameObject.Find("FPS").GetComponent<Text>(); mFPS = GameObject.Find("FPS").GetComponent<Text>();
optionDropdown = GameObject.Find("optionDropdown").GetComponent<Dropdown>(); optionDropdown = GameObject.Find("optionDropdown").GetComponent<Dropdown>();
mainmotion = new mainMotion(); emu = new MameMainMotion();
mUniLog = new UniLog(); mUniLog = new UniLog();
mUniMouse = this.gameObject.AddComponent<UniMouse>(); mUniMouse = this.gameObject.AddComponent<UniMouse>();
mUniVideoPlayer = this.gameObject.AddComponent<UniVideoPlayer>(); mUniVideoPlayer = this.gameObject.AddComponent<UniVideoPlayer>();
mUniSoundPlayer = GameObject.Find("Audio").transform.GetComponent<UniSoundPlayer>(); mUniSoundPlayer = GameObject.Find("Audio").transform.GetComponent<UniSoundPlayer>();
mUniKeyboard = this.gameObject.AddComponent<UniKeyboard>(); mUniKeyboard = this.gameObject.AddComponent<UniKeyboard>();
mUniResources = new UniResources(); mUniResources = new UniResources();
mChangeRomName = UniMAMESetting.instance.LastGameRom;
mainmotion.Init(RomPath, mUniLog, mUniResources, mUniVideoPlayer, mUniSoundPlayer, mUniKeyboard, mUniMouse); emu.Init(RomPath, mUniLog, mUniResources, mUniVideoPlayer, mUniSoundPlayer, mUniKeyboard, mUniMouse);
ALLGame = mainmotion.GetGameList(); ALLGame = emu.GetGameList();
Debug.Log($"ALLGame:{ALLGame.Count}"); Debug.Log($"ALLGame:{ALLGame.Count}");
#if !UNITY_EDITOR
bQuickTestRom = false;
#endif
if (bQuickTestRom)
mChangeRomName = mQuickTestRom;
GetHadRomList(); GetHadRomList();
if (bQuickTestRom)
LoadGame();
} }
void OnEnable() void OnEnable()
@ -72,51 +83,41 @@ public class UMAME : MonoBehaviour
void LoadGame() void LoadGame()
{ {
mChangeRomName = HadGameList[optionDropdown.value].Name; mChangeRomName = HadGameList[optionDropdown.value].Name;
UniMAMESetting.instance.LastGameRom = mChangeRomName;
StopGame(); StopGame();
mainmotion.LoadRom(mChangeRomName); //读取ROM
if (Machine.bRom) emu.LoadRom(mChangeRomName);
//读取成功
if (emu.bRom)
{ {
m68000Motion.iStatus = 0; //读取ROM之后获得宽高初始化画面
m68000Motion.iValue = 0; emu.GetGameScreenSize(out int _width, out int _height, out IntPtr _framePtr);
Mame.exit_pending = false; Debug.Log($"_width->{_width}, _height->{_height}, _framePtr->{_framePtr}");
mame.Motion.init(); mUniVideoPlayer.Initialize(_width, _height, _framePtr);
mainMotion.t1 = new Thread(Mame.mame_execute); //初始化音频
mainMotion.t1.Start(); mUniSoundPlayer.Initialize();
//开始游戏
StartCoroutine(StartGame()); emu.StartGame();
bInGame = true;
}
else
{
Debug.Log($"ROM加载失败");
} }
} }
private void Update() private void Update()
{ {
mFPS.text = ($"fpsv {mUniVideoPlayer.videoFPS.ToString("F2")} fpsa {mUniSoundPlayer.audioFPS.ToString("F2")}"); mFPS.text = ($"fpsv {mUniVideoPlayer.videoFPS.ToString("F2")} fpsa {mUniSoundPlayer.audioFPS.ToString("F2")}");
} }
IEnumerator StartGame()
{
yield return new WaitForSeconds(2f);
StartEmu();
}
void StartEmu()
{
if (bStart)
{
return;
}
mUniSoundPlayer.Initialize();
mainmotion.GetGameScreenSize(out int _width, out int _height, out IntPtr _framePtr);
mUniVideoPlayer.Initialize(_width, _height, _framePtr);
Mame.mame_pause(false);
bStart = true;
}
void StopGame() void StopGame()
{ {
//如果已经有正在运行的游戏,使其释放 if (bInGame)
if (bStart || Machine.bRom)
{ {
bStart = false; emu.StopGame();
Mame.exit_pending = true; mUniVideoPlayer.StopVideo();
Thread.Sleep(100); mUniSoundPlayer.StopPlay();
bInGame = false;
} }
} }

View File

@ -23,6 +23,7 @@ public class UniKeyboard : MonoBehaviour, IKeyboard
public UILongClickButton btnCD; public UILongClickButton btnCD;
public UILongClickButton btnABC; public UILongClickButton btnABC;
FloatingJoystick mJoystick; FloatingJoystick mJoystick;
public Transform tfKeyPad;
#endregion #endregion
List<UILongClickButton> mUIBtns = new List<UILongClickButton>(); List<UILongClickButton> mUIBtns = new List<UILongClickButton>();
@ -30,7 +31,7 @@ public class UniKeyboard : MonoBehaviour, IKeyboard
void Awake() void Awake()
{ {
mJoystick = GameObject.Find("tfJoystick").GetComponent<FloatingJoystick>(); mJoystick = GameObject.Find("tfJoystick").GetComponent<FloatingJoystick>();
tfKeyPad = GameObject.Find("tfKeyPad").transform;
btnP1 = GameObject.Find("btnP1").GetComponent<UILongClickButton>(); btnP1 = GameObject.Find("btnP1").GetComponent<UILongClickButton>();
btnCoin1 = GameObject.Find("btnCoin1").GetComponent<UILongClickButton>(); btnCoin1 = GameObject.Find("btnCoin1").GetComponent<UILongClickButton>();
btnA = GameObject.Find("btnA").GetComponent<UILongClickButton>(); btnA = GameObject.Find("btnA").GetComponent<UILongClickButton>();
@ -46,6 +47,9 @@ public class UniKeyboard : MonoBehaviour, IKeyboard
btnE.gameObject.SetActive(false); btnE.gameObject.SetActive(false);
btnF.gameObject.SetActive(false); btnF.gameObject.SetActive(false);
#if UNITY_STANDALONE_WIN || UNITY_EDITOR
tfKeyPad.gameObject.SetActive(false);
#endif
dictKeyCfgs.Add(KeyCode.P, MotionKey.EMU_PAUSED); dictKeyCfgs.Add(KeyCode.P, MotionKey.EMU_PAUSED);
dictKeyCfgs.Add(KeyCode.Alpha1, MotionKey.P1_GAMESTART); dictKeyCfgs.Add(KeyCode.Alpha1, MotionKey.P1_GAMESTART);

View File

@ -1,18 +1,23 @@
using MAME.Core.run_interface; using MAME.Core.run_interface;
using UnityEngine; using UnityEngine;
public class UniMouse : MonoBehaviour,IMouse public class UniMouse : MonoBehaviour, IMouse
{ {
static int mX, mY; static int mX, mY;
public byte[] buttons = new byte[2];
void Update() void Update()
{ {
mX = (int)Input.mousePosition.x; mX = (int)Input.mousePosition.x;
mY = (int)Input.mousePosition.y; mY = (int)Input.mousePosition.y;
buttons[0] = Input.GetMouseButton(0) ? (byte)1 : (byte)0;
buttons[1] = Input.GetMouseButton(1) ? (byte)1 : (byte)0;
} }
public void MouseXY(out int X, out int Y) public void MouseXY(out int X, out int Y, out byte[] MouseButtons)
{ {
X = mX; X = mX;
Y = mY; Y = mY * -1;
MouseButtons = buttons;
} }
} }

View File

@ -4,9 +4,6 @@ using UnityEngine;
public class UniSoundPlayer : MonoBehaviour, ISoundPlayer public class UniSoundPlayer : MonoBehaviour, ISoundPlayer
{ {
public int mWrite_position = 0;
public int mPlay_position =0;
[SerializeField] [SerializeField]
private AudioSource m_as; private AudioSource m_as;
private RingBuffer<float> _buffer = new RingBuffer<float>(4096); private RingBuffer<float> _buffer = new RingBuffer<float>(4096);
@ -26,21 +23,30 @@ public class UniSoundPlayer : MonoBehaviour, ISoundPlayer
public void Initialize() public void Initialize()
{ {
m_as.Play(); if (!m_as.isPlaying)
{
m_as.Play();
}
}
public void StopPlay()
{
if (m_as.isPlaying)
{
m_as.Stop();
}
} }
void OnAudioFilterRead(float[] data, int channels) void OnAudioFilterRead(float[] data, int channels)
{ {
if (!UMAME.bStart) return; if (!UMAME.bInGame) return;
int step = channels; int step = channels;
mWrite_position = 0;
for (int i = 0; i < data.Length; i += step) for (int i = 0; i < data.Length; i += step)
{ {
float rawFloat = lastData; float rawFloat = lastData;
if (_buffer.TryRead(out float rawData)) if (_buffer.TryRead(out float rawData))
{ {
rawFloat = rawData; rawFloat = rawData;
mWrite_position++;
} }
data[i] = rawFloat; data[i] = rawFloat;
@ -62,6 +68,8 @@ public class UniSoundPlayer : MonoBehaviour, ISoundPlayer
_buffer.Write(floatdata[i]); _buffer.Write(floatdata[i]);
} }
} }
public float[] ConvertByteArrayToFloatArray(byte[] bytes, int sampleRate, int channels) public float[] ConvertByteArrayToFloatArray(byte[] bytes, int sampleRate, int channels)
{ {
int sampleCount = bytes.Length / (channels * 2); // 16位所以每个样本2字节 int sampleCount = bytes.Length / (channels * 2); // 16位所以每个样本2字节
@ -72,10 +80,6 @@ public class UniSoundPlayer : MonoBehaviour, ISoundPlayer
// 读取左右声道 // 读取左右声道
short left = BitConverter.ToInt16(bytes, i * channels * 2); short left = BitConverter.ToInt16(bytes, i * channels * 2);
short right = BitConverter.ToInt16(bytes, i * channels * 2 + 2); short right = BitConverter.ToInt16(bytes, i * channels * 2 + 2);
//short left = (short)BitConverter.ToUInt16(bytes, i * channels * 2);
//short right = (short)BitConverter.ToUInt16(bytes, i * channels * 2 + 2);
// 转换为-1.0到1.0的浮点数 // 转换为-1.0到1.0的浮点数
floatArray[i] = left / 32767.0f; // 32767是16位整数的最大值 floatArray[i] = left / 32767.0f; // 32767是16位整数的最大值
floatArray[i + 1] = right / 32767.0f; floatArray[i + 1] = right / 32767.0f;
@ -86,22 +90,12 @@ public class UniSoundPlayer : MonoBehaviour, ISoundPlayer
public void BufferWirte(int Off, byte[] Data) public void BufferWirte(int Off, byte[] Data)
{ {
//var current = sw.Elapsed;
//var delta = current - lastElapsed;
//lastElapsed = current;
//FPS = 1d / delta.TotalSeconds;
//for (int i = Off; i < Data.Length; i++)
//{
// _buffer.Write(Data[i]);
//}
} }
public void GetCurrentPosition(out int play_position, out int write_position) public void GetCurrentPosition(out int play_position, out int write_position)
{ {
play_position = mPlay_position; play_position = 0;
write_position = mWrite_position; write_position = 0;
} }
public void SetVolume(int Vol) public void SetVolume(int Vol)
@ -110,5 +104,4 @@ public class UniSoundPlayer : MonoBehaviour, ISoundPlayer
return; return;
m_as.volume = Vol; m_as.volume = Vol;
} }
} }

View File

@ -22,39 +22,46 @@ public class UniVideoPlayer : MonoBehaviour, IVideoPlayer
private TimeSpan lastElapsed; private TimeSpan lastElapsed;
public double videoFPS { get; private set; } public double videoFPS { get; private set; }
bool bInitTexture = false; bool bInit = false;
private void Awake() private void Awake()
{ {
m_drawCanvas = GameObject.Find("GameRawImage").GetComponent<RawImage>(); m_drawCanvas = GameObject.Find("GameRawImage").GetComponent<RawImage>();
m_drawCanvasrect = m_drawCanvas.GetComponent<RectTransform>(); m_drawCanvasrect = m_drawCanvas.GetComponent<RectTransform>();
} }
public void Initialize(int width, int height,IntPtr framePtr) public void Initialize(int width, int height,IntPtr framePtr)
{ {
m_drawCanvas.color = Color.white; m_drawCanvas.color = Color.white;
//384 * 264
mWidth = width;
mHeight = height;
mDataLenght = width * height * 4;
mFrameDataPtr = framePtr;
//m_rawBufferWarper = new Texture2D(mWidth, mHeight,TextureFormat.RGBA32,false);
//MAMEÀ´µÄÊÇBGRA32£¬ºÃºÃºÃ
m_rawBufferWarper = new Texture2D(mWidth, mHeight, TextureFormat.BGRA32, false);
m_rawBufferWarper.filterMode = FilterMode.Point;
if (m_rawBufferWarper == null || mWidth != width || mHeight != height)
{
mWidth = width;
mHeight = height;
mDataLenght = width * height * 4;
mFrameData = new int[mWidth * mHeight];
//MAMEÀ´µÄÊÇBGRA32£¬ºÃºÃºÃ
m_rawBufferWarper = new Texture2D(mWidth, mHeight, TextureFormat.BGRA32, false);
m_rawBufferWarper.filterMode = FilterMode.Point;
}
mFrameDataPtr = framePtr;
m_drawCanvas.texture = m_rawBufferWarper; m_drawCanvas.texture = m_rawBufferWarper;
mFrameData = new int[mWidth * mHeight]; bInit = true;
bInitTexture = true;
float targetWidth = ((float)mWidth / mHeight) * m_drawCanvasrect.rect.height ; float targetWidth = ((float)mWidth / mHeight) * m_drawCanvasrect.rect.height ;
m_drawCanvasrect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, targetWidth); m_drawCanvasrect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, targetWidth);
} }
public void StopVideo()
{
bInit = false;
m_drawCanvas.color = new Color(0,0,0,0);
}
void Update() void Update()
{ {
if (!bInitTexture) return; if (!bInit) return;
//m_rawBufferWarper.LoadRawTextureData(mFrameDataPtr, mFrameData.Length * 4);
m_rawBufferWarper.LoadRawTextureData(mFrameDataPtr, mDataLenght); m_rawBufferWarper.LoadRawTextureData(mFrameDataPtr, mDataLenght);
m_rawBufferWarper.Apply(); m_rawBufferWarper.Apply();
} }