-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathSpritesAsset.cs
More file actions
89 lines (72 loc) · 3.48 KB
/
SpritesAsset.cs
File metadata and controls
89 lines (72 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using UnityEngine;
namespace OpenTibiaUnity.Core.Appearances.Provider
{
public class SpritesAsset
{
public const int AtlasTexture_Width = 512;
public const int AtlasTexture_Height = 512;
private uint _firstSpriteId = 0;
private uint _lastSpriteId = 0;
private uint _spriteType = 0;
private string _fileName = null;
public uint FirstSpriteId { get => _firstSpriteId; }
public uint LastSpriteId { get => _lastSpriteId; }
public uint SpriteType { get => _spriteType; }
public string FileName { get => _fileName; }
public SpritesAsset(uint firstSpriteId, uint lastSpriteId, uint spriteType, string filename) {
_firstSpriteId = firstSpriteId;
_lastSpriteId = lastSpriteId;
_spriteType = spriteType;
_fileName = filename;
}
private static Vector2Int GetSpriteSize(uint spriteType) {
var width = spriteType / 100;
var height = spriteType % 100;
return new Vector2Int((int) width, (int) height);
}
public Rendering.CachedSpriteInformation GetCachedSpriteInformation(uint spriteId, AssetBundle assetBundle) {
Texture2D tex2D = assetBundle.LoadAsset<Texture2D>(_fileName);
if (!tex2D)
return null;
var realSpriteSize = GetSpriteSize(_spriteType) * Constants.FieldSize;
uint realId = spriteId - _firstSpriteId;
int texPerRow = AtlasTexture_Width / realSpriteSize.x;
int x = (int)((realId % texPerRow) * realSpriteSize.x);
int y = (int)(realId / texPerRow * realSpriteSize.y);
y = AtlasTexture_Height - y - (int)realSpriteSize.y;
var spriteRect = new Rect(x / (float)AtlasTexture_Width, y / (float)AtlasTexture_Height, realSpriteSize.x / AtlasTexture_Width, realSpriteSize.y / AtlasTexture_Height);
return new Rendering.CachedSpriteInformation(spriteId, tex2D, spriteRect, realSpriteSize);
}
public static List<SpritesAsset> ParseJsonContents(JArray jArray) {
List<SpritesAsset> spritesAssets = new List<SpritesAsset>();
foreach (var @object in jArray.Children<JObject>()) {
if (!@object.TryGetValue("type", out JToken typeToken) || (string)typeToken != "sprite")
continue;
if (!@object.TryGetValue("file", out JToken fileToken)
|| !@object.TryGetValue("spritetype", out JToken spriteTypeToken)
|| !@object.TryGetValue("firstspriteid", out JToken firstSpriteIdToken)
|| !@object.TryGetValue("lastspriteid", out JToken lastSpriteIdToken))
continue;
try {
spritesAssets.Add(new SpritesAsset(
(uint)firstSpriteIdToken,
(uint)lastSpriteIdToken,
(uint)spriteTypeToken,
(string)fileToken));
} catch (System.InvalidCastException) { }
}
return spritesAssets;
}
public static bool operator !(SpritesAsset instance) {
return instance == null;
}
public static bool operator true(SpritesAsset instance) {
return !!instance;
}
public static bool operator false(SpritesAsset instance) {
return !instance;
}
}
}