Elin Decompiled Documentation EA 23.153 Nightly
Loading...
Searching...
No Matches
SpriteData.cs
Go to the documentation of this file.
1using System;
2using System.Globalization;
3using System.IO;
4using System.Text;
5using IniParser;
6using IniParser.Model;
7using UnityEngine;
8
9public class SpriteData
10{
11 public DateTime date;
12
13 public Texture2D tex;
14
15 public Texture2D texSnow;
16
17 public string path;
18
19 public Sprite[] sprites;
20
21 public Sprite[] spritesSnow;
22
23 public int frame = 1;
24
25 public int scale = 50;
26
27 public float time = 0.2f;
28
29 public void Init()
30 {
31 try
32 {
33 if (File.Exists(path + ".ini"))
34 {
35 IniData iniData = new FileIniDataParser().ReadFile(path + ".ini", Encoding.UTF8);
36 frame = int.Parse(iniData.GetKey("frame") ?? "1");
37 scale = int.Parse(iniData.GetKey("scale") ?? "50");
38 time = float.Parse(iniData.GetKey("time") ?? "0.2", CultureInfo.InvariantCulture);
39 }
40 }
41 catch (Exception message)
42 {
43 Debug.Log(message);
44 Debug.Log("exception: Failed to parse:" + path + ".ini");
45 time = 0.2f;
46 scale = 50;
47 }
48 }
49
50 public Sprite[] GetSprites()
51 {
52 Validate();
53 return sprites;
54 }
55
56 public Sprite GetSprite(bool snow = false)
57 {
58 Validate();
59 if (!snow || spritesSnow == null)
60 {
61 return sprites[0];
62 }
63 return spritesSnow[0];
64 }
65
66 public void Validate()
67 {
68 DateTime lastWriteTime = File.GetLastWriteTime(path + ".png");
69 bool flag = date.Year != 1 && date == lastWriteTime;
70 if (!flag)
71 {
72 Load(flag, ref tex, ref sprites, "");
73 if (File.Exists(path + "_snow.png"))
74 {
75 Load(flag, ref texSnow, ref spritesSnow, "_snow");
76 }
77 date = lastWriteTime;
78 }
79 }
80
81 public void Load(bool dateMatched, ref Texture2D tex, ref Sprite[] sprites, string suffix)
82 {
83 if (dateMatched && (bool)tex)
84 {
85 return;
86 }
87 if ((bool)tex)
88 {
89 Texture2D texture2D = IO.LoadPNG(path + suffix + ".png");
90 if (texture2D.width != tex.width || texture2D.height != tex.height)
91 {
92 Debug.Log(texture2D.width + "/" + texture2D.height + "/" + path);
93 tex.Reinitialize(texture2D.width, texture2D.height);
94 }
95 tex.SetPixels32(texture2D.GetPixels32());
96 tex.Apply();
97 UnityEngine.Object.Destroy(texture2D);
98 }
99 else
100 {
101 tex = IO.LoadPNG(path + suffix + ".png");
102 int num = tex.width / frame;
103 int height = tex.height;
104 sprites = new Sprite[frame];
105 for (int i = 0; i < frame; i++)
106 {
107 sprites[i] = Sprite.Create(tex, new Rect(i * num, 0f, num, height), new Vector2(0.5f, 0.5f), 100f, 0u, SpriteMeshType.FullRect);
108 }
109 }
110 }
111}
void Init()
Definition: SpriteData.cs:29
Sprite[] sprites
Definition: SpriteData.cs:19
Sprite[] spritesSnow
Definition: SpriteData.cs:21
Texture2D tex
Definition: SpriteData.cs:13
string path
Definition: SpriteData.cs:17
void Validate()
Definition: SpriteData.cs:66
DateTime date
Definition: SpriteData.cs:11
Texture2D texSnow
Definition: SpriteData.cs:15
Sprite GetSprite(bool snow=false)
Definition: SpriteData.cs:56
float time
Definition: SpriteData.cs:27
void Load(bool dateMatched, ref Texture2D tex, ref Sprite[] sprites, string suffix)
Definition: SpriteData.cs:81
Sprite[] GetSprites()
Definition: SpriteData.cs:50