-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssetLoader.cs
More file actions
56 lines (49 loc) · 1.68 KB
/
AssetLoader.cs
File metadata and controls
56 lines (49 loc) · 1.68 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
using System.IO;
using UnityEngine;
namespace FinneanTweaks
{
internal class AssetLoader
{
public static Sprite one
{
get
{
if (m_one == null)
{
m_one = LoadInternal("icons", "One.png", new Vector2Int(100, 100));
}
return m_one;
}
}
public static Sprite m_one;
public static Sprite two
{
get
{
if (m_two == null)
{
m_two = LoadInternal("icons", "Two.png", new Vector2Int(100, 100));
}
return m_two;
}
}
public static Sprite m_two;
public static Sprite LoadInternal(string folder, string file, Vector2Int size)
{
return Image2Sprite.Create($"{Main.ModEntry.Path}Assets{Path.DirectorySeparatorChar}{folder}{Path.DirectorySeparatorChar}{file}", size);
}
// Loosely based on https://forum.unity.com/threads/generating-sprites-dynamically-from-png-or-jpeg-files-in-c.343735/
public static class Image2Sprite
{
public static string icons_folder = "";
public static Sprite Create(string filePath, Vector2Int size)
{
//Main.logger.Log("Creating sprite");
var bytes = File.ReadAllBytes(icons_folder + filePath);
var texture = new Texture2D(size.x, size.y, TextureFormat.ARGB32, false);
_ = texture.LoadImage(bytes);
return Sprite.Create(texture, new Rect(0, 0, size.x, size.y), new Vector2(0, 0));
}
}
}
}