新增GamePadManager类,用于管理Unity手柄连接相关功能
This commit is contained in:
parent
864b5879e1
commit
35a5f03a66
@ -21,7 +21,6 @@ namespace AxibugEmuOnline.Client.ClientCore
|
||||
public static AppLogin login;
|
||||
public static AppChat chat;
|
||||
public static UserDataManager user;
|
||||
//public static AppNetGame netgame;
|
||||
public static AppEmu emu;
|
||||
/// <summary>
|
||||
/// nes Rom库
|
||||
@ -36,6 +35,7 @@ namespace AxibugEmuOnline.Client.ClientCore
|
||||
public static AppRoom roomMgr;
|
||||
public static AppSettings settings;
|
||||
public static AppShare share;
|
||||
public static GamePadManager gamePadMgr;
|
||||
private static object gameSavMgr;
|
||||
static bool bTest;
|
||||
static string mTestSrvIP;
|
||||
@ -70,7 +70,6 @@ namespace AxibugEmuOnline.Client.ClientCore
|
||||
chat = new AppChat();
|
||||
user = new UserDataManager();
|
||||
emu = new AppEmu();
|
||||
//netgame = new AppNetGame();
|
||||
httpAPI = new HttpAPI();
|
||||
if (bUseLocalWebApi)
|
||||
httpAPI.WebHost = mLocalWebApi;
|
||||
@ -80,6 +79,8 @@ namespace AxibugEmuOnline.Client.ClientCore
|
||||
roomMgr = new AppRoom();
|
||||
share = new AppShare();
|
||||
gameSavMgr = new AppGameSavMgr();
|
||||
gamePadMgr = new GamePadManager();
|
||||
|
||||
bTest = isTest;
|
||||
mTestSrvIP = testSrvIP;
|
||||
var go = new GameObject("[AppAxibugEmuOnline]");
|
||||
@ -202,6 +203,9 @@ namespace AxibugEmuOnline.Client.ClientCore
|
||||
private static void Tick()
|
||||
{
|
||||
nesRomLib.ExecuteFetchRomInfo();
|
||||
starRomLib.ExecuteFetchRomInfo();
|
||||
|
||||
gamePadMgr.Update();
|
||||
}
|
||||
|
||||
public static Coroutine StartCoroutine(IEnumerator itor)
|
||||
|
@ -1,4 +1,5 @@
|
||||
using AxibugEmuOnline.Client.ClientCore;
|
||||
using System.Text;
|
||||
using UnityEngine;
|
||||
|
||||
namespace AxibugEmuOnline.Client
|
||||
|
@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5fe26f58ab822c44888b86305c5326e0
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,26 @@
|
||||
namespace AxibugEmuOnline.Client.Manager
|
||||
{
|
||||
public partial class GamePadManager
|
||||
{
|
||||
/// <summary>
|
||||
/// 被Unity所识别的通用GamePad类
|
||||
/// </summary>
|
||||
public class GamePad
|
||||
{
|
||||
internal GamePadInfo m_info;
|
||||
public int Index => m_info.Index;
|
||||
public string Name => m_info.Name;
|
||||
public bool Offline { get; internal set; }
|
||||
|
||||
internal GamePad(GamePadInfo info)
|
||||
{
|
||||
m_info = info;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"{Index}:{Name}{(Offline ? "(Offline)" : string.Empty)}";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c2c0a06020f65a747af5490a6112361b
|
@ -0,0 +1,121 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
|
||||
namespace AxibugEmuOnline.Client.Manager
|
||||
{
|
||||
public partial class GamePadManager
|
||||
{
|
||||
#region Events
|
||||
public delegate void GamePadConnectedHandle(GamePad newConnectGamePad);
|
||||
/// <summary> 当一个手柄连接时触发 </summary>
|
||||
public event GamePadConnectedHandle OnGamePadConnected;
|
||||
|
||||
public delegate void GamePadDisConnectedHandle(GamePad disConnectGamePad);
|
||||
/// <summary> 当一个手柄断开时触发 </summary>
|
||||
public event GamePadDisConnectedHandle OnGamePadDisConnected;
|
||||
#endregion
|
||||
|
||||
Dictionary<GamePadInfo, GamePad> m_gamePads = new Dictionary<GamePadInfo, GamePad>();
|
||||
HashSet<GamePadInfo> m_temp = new HashSet<GamePadInfo>();
|
||||
|
||||
public void Update()
|
||||
{
|
||||
m_temp.Clear();
|
||||
foreach (var info in m_gamePads.Keys)
|
||||
m_temp.Add(info); //记录需要被移除的手柄
|
||||
|
||||
var devices = Input.GetJoystickNames();
|
||||
for (int i = 0; i < devices.Length; i++)
|
||||
{
|
||||
var info = new GamePadInfo { Index = i, Name = devices[i] };
|
||||
m_temp.Remove(info);
|
||||
|
||||
if (!m_gamePads.ContainsKey(info))
|
||||
{
|
||||
m_gamePads[info] = new GamePad(info);
|
||||
OnGamePadConnected?.Invoke(m_gamePads[info]);
|
||||
};
|
||||
}
|
||||
|
||||
foreach (var info in m_temp)
|
||||
{
|
||||
if (m_gamePads.TryGetValue(info, out GamePad gp))
|
||||
{
|
||||
m_gamePads.Remove(info);
|
||||
gp.Offline = true;
|
||||
OnGamePadDisConnected?.Invoke(gp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有已连接的手柄,返回的结果顺序与手柄序号无关
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public GamePad[] GetGamePads()
|
||||
{
|
||||
return m_gamePads.Values.ToArray();
|
||||
}
|
||||
|
||||
internal struct GamePadInfo : IEquatable<GamePadInfo>, IComparable<GamePadInfo>
|
||||
{
|
||||
internal int Index;
|
||||
internal string Name;
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj is GamePadInfo)
|
||||
{
|
||||
return Equals((GamePadInfo)obj);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool Equals(GamePadInfo other)
|
||||
{
|
||||
return Index == other.Index && Name == other.Name;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
// Custom hash code implementation without HashCombine
|
||||
int hash = 17;
|
||||
hash = hash * 31 + Index.GetHashCode();
|
||||
hash = hash * 31 + (Name != null ? Name.GetHashCode() : 0);
|
||||
return hash;
|
||||
}
|
||||
|
||||
public int CompareTo(GamePadInfo other)
|
||||
{
|
||||
int indexComparison = Index.CompareTo(other.Index);
|
||||
if (indexComparison != 0)
|
||||
{
|
||||
return indexComparison;
|
||||
}
|
||||
return string.Compare(Name, other.Name, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
public static bool operator ==(GamePadInfo left, GamePadInfo right)
|
||||
{
|
||||
return left.Equals(right);
|
||||
}
|
||||
|
||||
public static bool operator !=(GamePadInfo left, GamePadInfo right)
|
||||
{
|
||||
return !(left == right);
|
||||
}
|
||||
|
||||
public static bool operator <(GamePadInfo left, GamePadInfo right)
|
||||
{
|
||||
return left.CompareTo(right) < 0;
|
||||
}
|
||||
|
||||
public static bool operator >(GamePadInfo left, GamePadInfo right)
|
||||
{
|
||||
return left.CompareTo(right) > 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ce4d215abea527e4a8cf1103cbfecf6b
|
Loading…
Reference in New Issue
Block a user