-
Notifications
You must be signed in to change notification settings - Fork 4
/
ResourceUtils.cs
78 lines (67 loc) · 2.25 KB
/
ResourceUtils.cs
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
using System;
using System.Collections.Generic;
using ColossalFramework.Plugins;
using ColossalFramework.UI;
using UnityEngine;
namespace MoreBeautification
{
public static class ResourceUtils
{
public static T Load<T>(string name) where T : UnityEngine.Object
{
T[] ts = ResourceUtils.Load<T>(new string[] { name });
if (ts.Length >= 1)
{
return ts[0];
}
else
{
return null;
}
}
public static T[] Load<T>(string[] names) where T : UnityEngine.Object
{
List<T> ts = new List<T>();
foreach (T t in Resources.FindObjectsOfTypeAll<T>())
{
if (Array.Exists(names, n => n == t.name))
{
ts.Add(t);
}
}
return ts.ToArray();
}
public static void Dump<T>() where T : UnityEngine.Object
{
foreach (T t in Resources.FindObjectsOfTypeAll<T>())
{
DebugOutputPanel.AddMessage(PluginManager.MessageType.Message, t.name);
}
}
public static UITextureAtlas CreateAtlas(string[] names)
{
Texture2D[] sprites = ResourceUtils.Load<Texture2D>(names);
UITextureAtlas atlas = new UITextureAtlas();
atlas.material = new Material(ResourceUtils.GetUIAtlasShader());
Texture2D texture = new Texture2D(0, 0);
Rect[] rects = texture.PackTextures(sprites, 0);
for (int i = 0; i < rects.Length; ++i)
{
Texture2D sprite = sprites[i];
Rect rect = rects[i];
UITextureAtlas.SpriteInfo spriteInfo = new UITextureAtlas.SpriteInfo();
spriteInfo.name = sprite.name;
spriteInfo.texture = sprite;
spriteInfo.region = rect;
spriteInfo.border = new RectOffset();
atlas.AddSprite(spriteInfo);
}
atlas.material.mainTexture = texture;
return atlas;
}
private static Shader GetUIAtlasShader()
{
return UIView.GetAView().defaultAtlas.material.shader;
}
}
}