Repository navigation
Expand file tree
/
Copy pathImageUtils.cs
More file actions
49 lines (45 loc) · 1.81 KB
/
Copy pathImageUtils.cs
File metadata and controls
49 lines (45 loc) · 1.81 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
using System;
using System.IO;
using UnityEngine;
public static class ImageUtils
{
public static Sprite LoadSprite(string FilePath, float PixelsPerUnit = 100f, SpriteMeshType spriteType = SpriteMeshType.Tight)
{
Texture2D texture2D = ImageUtils.LoadTexture(FilePath, TextureFormat.DXT5);
if (!texture2D)
{
return null;
}
return Sprite.Create(texture2D, new Rect(0f, 0f, (float)texture2D.width, (float)texture2D.height), new Vector2(0f, 0f), PixelsPerUnit, 0u, spriteType);
}
public static Texture2D LoadTexture(string FilePath, TextureFormat format = TextureFormat.DXT5)
{
if (File.Exists(FilePath))
{
byte[] data = File.ReadAllBytes(FilePath);
Texture2D texture2D = new Texture2D(2, 2, format, false);
if (texture2D.LoadImage(data))
{
return texture2D;
}
}
else
{
Debug.Log("File not found " + FilePath);
}
return null;
}
public static Texture2D Render2Texture(RenderTexture rt, TextureFormat format = TextureFormat.DXT5)
{
RenderTexture.active = rt;
Texture2D texture2D = new Texture2D(rt.width, rt.height, format, false);
texture2D.ReadPixels(new Rect(0f, 0f, (float)rt.width, (float)rt.height), 0, 0, false);
texture2D.Apply();
RenderTexture.active = null;
return texture2D;
}
public static Sprite Texture2Sprite(Texture2D tex)
{
return Sprite.Create(tex, new Rect(0f, 0f, (float)tex.width, (float)tex.height), new Vector2(0.5f, 0.5f), 100f, 0u, SpriteMeshType.Tight);
}
}