1
This commit is contained in:
21
Unity/Extensions/Steamwork/BITKit.Steamwork.asmdef
Normal file
21
Unity/Extensions/Steamwork/BITKit.Steamwork.asmdef
Normal file
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"name": "BITKit.Steamwork",
|
||||
"rootNamespace": "",
|
||||
"references": [
|
||||
"GUID:14fe60d984bf9f84eac55c6ea033a8f4",
|
||||
"GUID:68bd7fdb68ef2684e982e8a9825b18a5",
|
||||
"GUID:a209c53514018594f9f482516f2a6781",
|
||||
"GUID:f51ebe6a0ceec4240a699833d6309b23"
|
||||
],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": false,
|
||||
"precompiledReferences": [],
|
||||
"autoReferenced": true,
|
||||
"defineConstraints": [
|
||||
"STEAMWORKS_NET"
|
||||
],
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
111
Unity/Extensions/Steamwork/SteamUtils.cs
Normal file
111
Unity/Extensions/Steamwork/SteamUtils.cs
Normal file
@@ -0,0 +1,111 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Steamworks;
|
||||
namespace BITKit.Steam
|
||||
{
|
||||
public class BITSteamUtils
|
||||
{
|
||||
#region DomainReload
|
||||
[RuntimeInitializeOnLoadMethod]
|
||||
static void Reload()
|
||||
{
|
||||
_Cache = new();
|
||||
_AvatarTaskList = new();
|
||||
}
|
||||
#endregion
|
||||
#region Variables
|
||||
|
||||
static Dictionary<CSteamID, Texture2D> _Cache = new Dictionary<CSteamID, Texture2D>();
|
||||
|
||||
static ulong _AvatarTaskCount = 0;
|
||||
static Dictionary<ulong, Callback<AvatarImageLoaded_t>> _AvatarTaskList = new Dictionary<ulong, Callback<AvatarImageLoaded_t>>();
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region Functions
|
||||
|
||||
public static void GetUserAvatar(CSteamID steamID, System.Action<Texture2D> callback)
|
||||
{
|
||||
if (_Cache.ContainsKey(steamID))
|
||||
{
|
||||
callback(_Cache[steamID]);
|
||||
return;
|
||||
}
|
||||
|
||||
int userAvatar = SteamFriends.GetLargeFriendAvatar(steamID);
|
||||
uint imageWidth;
|
||||
uint imageHeight;
|
||||
|
||||
bool restartAvatarLoad = true;
|
||||
bool success = SteamUtils.GetImageSize(userAvatar, out imageWidth, out imageHeight);
|
||||
|
||||
if (success && imageWidth > 0 && imageHeight > 0)
|
||||
{
|
||||
byte[] data = new byte[imageWidth * imageHeight * 4];
|
||||
var returnTex = new Texture2D((int)imageWidth, (int)imageHeight, TextureFormat.RGBA32, false, false);
|
||||
|
||||
success = SteamUtils.GetImageRGBA(userAvatar, data, (int)(imageWidth * imageHeight * 4));
|
||||
if (success)
|
||||
{
|
||||
restartAvatarLoad = false;
|
||||
returnTex.LoadRawTextureData(data);
|
||||
returnTex.Apply();
|
||||
|
||||
//NOTE: texture loads upside down, so we flip it to normal...
|
||||
var result = FlipTexture(returnTex);
|
||||
|
||||
_Cache.Add(steamID, result);
|
||||
|
||||
callback(result);
|
||||
|
||||
Texture2D.DestroyImmediate(returnTex);
|
||||
}
|
||||
}
|
||||
|
||||
if (restartAvatarLoad)
|
||||
{
|
||||
ulong key = _AvatarTaskCount;
|
||||
_AvatarTaskCount++;
|
||||
|
||||
var task = new Callback<AvatarImageLoaded_t>(delegate (AvatarImageLoaded_t param)
|
||||
{
|
||||
GetUserAvatar(steamID, callback);
|
||||
_AvatarTaskList.Remove(key);
|
||||
});
|
||||
|
||||
_AvatarTaskList.Add(key, task);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region Utils
|
||||
|
||||
static Texture2D FlipTexture(Texture2D original)
|
||||
{
|
||||
Texture2D flipped = new Texture2D(original.width, original.height);
|
||||
|
||||
int xN = original.width;
|
||||
int yN = original.height;
|
||||
|
||||
for (int i = 0; i < xN; i++)
|
||||
{
|
||||
for (int j = 0; j < yN; j++)
|
||||
{
|
||||
flipped.SetPixel(i, yN - j - 1, original.GetPixel(i, j));
|
||||
}
|
||||
}
|
||||
|
||||
flipped.Apply();
|
||||
|
||||
return flipped;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
33
Unity/Extensions/Steamwork/SteamworkSystem.cs
Normal file
33
Unity/Extensions/Steamwork/SteamworkSystem.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Steamworks;
|
||||
using BITKit.SubSystems;
|
||||
namespace BITKit.Steam
|
||||
{
|
||||
public class SteamworkSystem : SubBITSystem
|
||||
{
|
||||
public override void OnStart()
|
||||
{
|
||||
if (SteamAPI.Init())
|
||||
{
|
||||
Debug.Log("Steamwork已初始化完成");
|
||||
BehaviourHelper.Add(Init);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log("Steamwork初始化失败");
|
||||
}
|
||||
}
|
||||
void Init()
|
||||
{
|
||||
var userId = SteamUser.GetSteamID();
|
||||
BITSteamUtils.GetUserAvatar(userId, OnAvatarCallback);
|
||||
Data.Set<string>("SteamName", SteamFriends.GetPersonaName());
|
||||
}
|
||||
void OnAvatarCallback(Texture2D texture)
|
||||
{
|
||||
Data.Set<Texture2D>("SteamAvatar", texture);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user