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

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
guid: 8068b465f3dd6864f807fa17ca4bd813
guid: fae13461b22160c47b5ee9964058b719
PluginImporter:
externalObjects: {}
serializedVersion: 2

View File

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

View File

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

View File

@ -23,6 +23,7 @@ public class UniKeyboard : MonoBehaviour, IKeyboard
public UILongClickButton btnCD;
public UILongClickButton btnABC;
FloatingJoystick mJoystick;
public Transform tfKeyPad;
#endregion
List<UILongClickButton> mUIBtns = new List<UILongClickButton>();
@ -30,7 +31,7 @@ public class UniKeyboard : MonoBehaviour, IKeyboard
void Awake()
{
mJoystick = GameObject.Find("tfJoystick").GetComponent<FloatingJoystick>();
tfKeyPad = GameObject.Find("tfKeyPad").transform;
btnP1 = GameObject.Find("btnP1").GetComponent<UILongClickButton>();
btnCoin1 = GameObject.Find("btnCoin1").GetComponent<UILongClickButton>();
btnA = GameObject.Find("btnA").GetComponent<UILongClickButton>();
@ -46,6 +47,9 @@ public class UniKeyboard : MonoBehaviour, IKeyboard
btnE.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.Alpha1, MotionKey.P1_GAMESTART);

View File

@ -4,15 +4,20 @@ using UnityEngine;
public class UniMouse : MonoBehaviour, IMouse
{
static int mX, mY;
public byte[] buttons = new byte[2];
void Update()
{
mX = (int)Input.mousePosition.x;
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;
Y = mY;
Y = mY * -1;
MouseButtons = buttons;
}
}

View File

@ -4,9 +4,6 @@ using UnityEngine;
public class UniSoundPlayer : MonoBehaviour, ISoundPlayer
{
public int mWrite_position = 0;
public int mPlay_position =0;
[SerializeField]
private AudioSource m_as;
private RingBuffer<float> _buffer = new RingBuffer<float>(4096);
@ -25,22 +22,31 @@ public class UniSoundPlayer : MonoBehaviour, ISoundPlayer
}
public void Initialize()
{
if (!m_as.isPlaying)
{
m_as.Play();
}
}
public void StopPlay()
{
if (m_as.isPlaying)
{
m_as.Stop();
}
}
void OnAudioFilterRead(float[] data, int channels)
{
if (!UMAME.bStart) return;
if (!UMAME.bInGame) return;
int step = channels;
mWrite_position = 0;
for (int i = 0; i < data.Length; i += step)
{
float rawFloat = lastData;
if (_buffer.TryRead(out float rawData))
{
rawFloat = rawData;
mWrite_position++;
}
data[i] = rawFloat;
@ -62,6 +68,8 @@ public class UniSoundPlayer : MonoBehaviour, ISoundPlayer
_buffer.Write(floatdata[i]);
}
}
public float[] ConvertByteArrayToFloatArray(byte[] bytes, int sampleRate, int channels)
{
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 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的浮点数
floatArray[i] = left / 32767.0f; // 32767是16位整数的最大值
floatArray[i + 1] = right / 32767.0f;
@ -86,22 +90,12 @@ public class UniSoundPlayer : MonoBehaviour, ISoundPlayer
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)
{
play_position = mPlay_position;
write_position = mWrite_position;
play_position = 0;
write_position = 0;
}
public void SetVolume(int Vol)
@ -110,5 +104,4 @@ public class UniSoundPlayer : MonoBehaviour, ISoundPlayer
return;
m_as.volume = Vol;
}
}

View File

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