-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.cs
129 lines (116 loc) · 3.98 KB
/
Utils.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using System;
using System.IO;
using UnityEngine;
namespace Binocle
{
/// <summary>
/// Generic utility class. Most of the junk ends in there.
/// </summary>
public static class Utils
{
public static string RemoveFileExtension(string name)
{
int extensionPosition = name.LastIndexOf(".");
if (extensionPosition >= 0) name = name.Substring(0, extensionPosition);
return name;
}
/// <summary>
/// Creates a boxed monochromatic sprite.
/// This is highly inefficient and should be only used for debug.
/// It would be much better to create a shared one-pixel texture and set the color of the sprite instead
/// </summary>
/// <param name="width">box width</param>
/// <param name="height">box height</param>
/// <param name="color">the color of the sprite</param>
/// <returns></returns>
public static Sprite CreateBoxSprite(int width, int height, Color color)
{
Texture2D t = new Texture2D(width, height, UnityEngine.TextureFormat.RGBA32, false);
Color[] colors = new Color[width * height];
for (int i = 0; i < width * height; i++)
{
colors[i] = color;
}
t.SetPixels(0, 0, width, height, colors, 0);
t.Apply();
Sprite sprite = Sprite.Create(t, new Rect(0, 0, t.width, t.height), new Vector2(0.5f, 0.5f), 1);
return sprite;
}
public static string ReadTextFile(string sFileName)
{
//Debug.Log("Reading " + sFileName);
//Check to see if the filename specified exists, if not try adding '.txt', otherwise fail
string sFileNameFound = "";
if (File.Exists(sFileName))
{
//Debug.Log("Reading '" + sFileName + "'.");
sFileNameFound = sFileName; //file found
}
else if (File.Exists(sFileName + ".txt"))
{
sFileNameFound = sFileName + ".txt";
}
else
{
Debug.Log("Could not find file '" + sFileName + "'.");
return null;
}
StreamReader sr;
try
{
sr = new StreamReader(sFileNameFound);
}
catch (System.Exception e)
{
Debug.LogWarning("Something went wrong with read. " + e.Message);
return null;
}
string fileContents = sr.ReadToEnd();
sr.Close();
return fileContents;
}
public static void WriteTextFile(string sFilePathAndName, string sTextContents)
{
StreamWriter sw = new StreamWriter(sFilePathAndName);
sw.WriteLine(sTextContents);
sw.Flush();
sw.Close();
}
public static string GetVersion()
{
string v = string.Empty;
string versionTextFileNameAndPath = "version.txt";
string versionText = Utils.ReadTextFile(versionTextFileNameAndPath);
if (versionText != null)
{
v = versionText;
}
return v;
}
public static float Approach(float start, float end, float shift)
{
if (start < end)
{
return Math.Min(start + shift, end);
}
else
{
return Math.Max(start - shift, end);
}
}
public static Vector2 Approach(this Vector2 val, Vector2 target, float maxMove)
{
if ((maxMove == 0f) || (val == target))
{
return val;
}
Vector2 vector = target - val;
if (vector.magnitude < maxMove)
{
return target;
}
vector.Normalize();
return (val + ((Vector2) (vector * maxMove)));
}
}
}