-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextureUtility.cs
32 lines (30 loc) · 1004 Bytes
/
TextureUtility.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using System.IO;
using UnityEngine.UIElements;
namespace CommonAPI {
/// <summary>
/// Utility methods for dealing with textures.
/// </summary>
public static class TextureUtility {
/// <summary>
/// Quickly creates a Sprite from a Texture2D.
/// </summary>
public static Sprite CreateSpriteFromTexture(Texture2D texture) {
return Sprite.Create(texture, new Rect(0f, 0f, texture.width, texture.height), new Vector2(0.5f, 0.5f), 100f);
}
/// <summary>
/// Loads a sprite from a PNG/JPG image.
/// </summary>
public static Sprite LoadSprite(string path) {
var texture = new Texture2D(1, 1);
var imageData = File.ReadAllBytes(path);
texture.LoadImage(imageData);
return CreateSpriteFromTexture(texture);
}
}
}