Elin Decompiled Documentation EA 23.315 Nightly
Loading...
Searching...
No Matches
SpriteData.cs
Go to the documentation of this file.
1using System;
2using System.IO;
3using System.Text;
4using IniParser;
5using IniParser.Model;
6using UnityEngine;
7
8public class SpriteData
9{
10 public string id;
11
12 public string path;
13
14 public Texture2D tex;
15
16 public Sprite[] sprites;
17
18 public DateTime dateTex;
19
20 public DateTime dateIni;
21
22 public DateTime datePref;
23
24 public int frame = 1;
25
26 public int scale = 50;
27
28 public float time = 0.2f;
29
31
33
34 public void Init()
35 {
36 try
37 {
38 id = Path.GetFileNameWithoutExtension(path);
40 LoadPref();
41 }
42 catch (Exception exception)
43 {
44 Debug.LogException(exception);
45 Debug.LogError("#sprite failed " + id + "/" + path);
46 }
47 }
48
49 public void LoadPref()
50 {
51 pref = null;
52 if (File.Exists(path + ".pref"))
53 {
54 pref = SourcePref.ReadFromIni(path + ".pref");
55 }
56 }
57
58 public void LoadAnimationIni()
59 {
60 if (!File.Exists(path + ".ini"))
61 {
62 frame = 1;
63 scale = 50;
64 time = 0.2f;
65 return;
66 }
67 try
68 {
69 IniData iniData = new FileIniDataParser().ReadFile(path + ".ini", Encoding.UTF8);
70 frame = Mathf.Max(1, (!int.TryParse(iniData.GetKey("frame"), out var result)) ? 1 : result);
71 scale = (int.TryParse(iniData.GetKey("scale"), out var result2) ? result2 : 50);
72 time = (float.TryParse(iniData.GetKey("time"), out var result3) ? Mathf.Max(0.016f, result3) : 0.2f);
73 }
74 catch (Exception exception)
75 {
76 Debug.LogException(exception);
77 }
78 }
79
80 public void LoadSprites()
81 {
82 Texture2D texture2D = IO.LoadPNG(path + ".png");
83 Debug.Log(texture2D.width + "/" + texture2D.height + "/" + path);
84 if ((bool)tex)
85 {
86 if (texture2D.width != tex.width || texture2D.height != tex.height)
87 {
88 tex.Reinitialize(texture2D.width, texture2D.height);
89 }
90 tex.SetPixels32(texture2D.GetPixels32());
91 tex.Apply();
92 UnityEngine.Object.Destroy(texture2D);
93 }
94 else
95 {
96 tex = texture2D;
97 }
98 int num = tex.width / frame;
99 int height = tex.height;
100 if (sprites == null || sprites.Length != frame)
101 {
102 sprites = new Sprite[frame];
103 }
104 Vector2 pivot = new Vector2(0.5f, 0.5f);
105 for (int i = 0; i < frame; i++)
106 {
107 sprites[i] = Sprite.Create(tex, new Rect(i * num, 0f, num, height), pivot, 100f, 0u, SpriteMeshType.FullRect);
108 }
109 }
110
111 public Sprite[] GetSprites()
112 {
113 if (sprites == null)
114 {
115 Load();
116 }
117 return sprites;
118 }
119
120 public Sprite GetSprite()
121 {
122 if (sprites == null)
123 {
124 Load();
125 }
126 return sprites.TryGet(0, returnNull: true);
127 }
128
129 public void Validate()
130 {
131 if (IsDirty(".png", ref dateTex) || IsDirty(".ini", ref dateIni) || IsDirty(".pref", ref datePref))
132 {
133 Load();
134 }
135 bool IsDirty(string p, ref DateTime lastChecked)
136 {
137 string text = path + p;
138 if (!File.Exists(text))
139 {
140 return false;
141 }
142 DateTime lastWriteTimeUtc = File.GetLastWriteTimeUtc(text);
143 if (lastWriteTimeUtc == lastChecked)
144 {
145 return false;
146 }
147 lastChecked = lastWriteTimeUtc;
148 return true;
149 }
150 }
151
152 public void Load()
153 {
155 LoadPref();
156 LoadSprites();
157 }
158}
static SourcePref ReadFromIni(string path)
Definition: SourcePref.cs:395
void Init()
Definition: SpriteData.cs:34
Sprite[] sprites
Definition: SpriteData.cs:16
Texture2D tex
Definition: SpriteData.cs:14
bool tryFixPrefNotLoadedAtStart
Definition: SpriteData.cs:32
void LoadAnimationIni()
Definition: SpriteData.cs:58
void Load()
Definition: SpriteData.cs:152
DateTime datePref
Definition: SpriteData.cs:22
void LoadSprites()
Definition: SpriteData.cs:80
string path
Definition: SpriteData.cs:12
void LoadPref()
Definition: SpriteData.cs:49
DateTime dateTex
Definition: SpriteData.cs:18
void Validate()
Definition: SpriteData.cs:129
SourcePref pref
Definition: SpriteData.cs:30
string id
Definition: SpriteData.cs:10
float time
Definition: SpriteData.cs:28
DateTime dateIni
Definition: SpriteData.cs:20
Sprite GetSprite()
Definition: SpriteData.cs:120
Sprite[] GetSprites()
Definition: SpriteData.cs:111