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