@@ -93,6 +93,36 @@ public static T[] RangeSubset<T>(this T[] array, int startIndex, int length) {
9393 Array . Copy ( array , startIndex , subset , 0 , length ) ;
9494 return subset ;
9595 }
96+
97+ // [Source: https://forum.unity.com/threads/encoding-vector2-and-vector3-variables-into-single-int-or-float-and-back.448346/]
98+ // [Commentor: emotitron]
99+ public static int encodeVector3ToInt ( Vector3 v ) {
100+ //Vectors must stay within the -512 to 512 range per axis - no error handling coded here
101+ //Add 512 to get numbers into the 0-1024 range rather than -512 to 512 range
102+ //Multiply by 10 to save one decimal place from rounding
103+ int xcomp = Mathf . RoundToInt ( ( v . x * 10 ) ) + 512 ;
104+ int ycomp = Mathf . RoundToInt ( ( v . y * 10 ) ) + 512 ;
105+ int zcomp = Mathf . RoundToInt ( ( v . z * 10 ) ) + 512 ;
106+ return xcomp + ycomp * 1024 + zcomp * 1048576 ;
107+ }
108+ public static Vector3 decodeVector3FromInt ( int i ) {
109+ //Get the leftmost bits first. The fractional remains are the bits to the right.
110+ // 1024 is 2 ^ 10 - 1048576 is 2 ^ 20 - just saving some calculation time doing that in advance
111+ float z = Mathf . Floor ( i / 1048576 ) ;
112+ float y = Mathf . Floor ( ( i - z * 1048576 ) / 1024 ) ;
113+ float x = ( i - y * 1024 - z * 1048576 ) ;
114+ // subtract 512 to move numbers back into the -512 to 512 range rather than 0 - 1024
115+ return new Vector3 ( ( x - 512 ) / 10 , ( y - 512 ) / 10 , ( z - 512 ) / 10 ) ;
116+ }
117+ public static SVector3 decodeSVector3FromInt ( int i ) {
118+ //Get the leftmost bits first. The fractional remains are the bits to the right.
119+ // 1024 is 2 ^ 10 - 1048576 is 2 ^ 20 - just saving some calculation time doing that in advance
120+ float z = Mathf . Floor ( i / 1048576 ) ;
121+ float y = Mathf . Floor ( ( i - z * 1048576 ) / 1024 ) ;
122+ float x = ( i - y * 1024 - z * 1048576 ) ;
123+ // subtract 512 to move numbers back into the -512 to 512 range rather than 0 - 1024
124+ return new SVector3 ( ( x - 512 ) / 10 , ( y - 512 ) / 10 , ( z - 512 ) / 10 ) ;
125+ }
96126 }
97127
98128 [ System . Serializable ]
@@ -114,6 +144,20 @@ public static bool CheckOrCreateDirectory(string dirPath) {
114144 public static bool CheckFileExists ( string filePath ) {
115145 return File . Exists ( filePath ) ;
116146 }
147+ public static bool DeleteDirectory ( string dirPath ) {
148+ if ( CheckDirectoryExists ( dirPath ) ) {
149+ Directory . Delete ( dirPath , true ) ;
150+ return true ;
151+ }
152+ return false ;
153+ }
154+ public static bool DeleteFile ( string filePath ) {
155+ if ( CheckFileExists ( filePath ) ) {
156+ File . Delete ( filePath ) ;
157+ return true ;
158+ }
159+ return false ;
160+ }
117161
118162 public static string ConvertToJSON < T > ( T data ) {
119163 return JsonUtility . ToJson ( data , true ) ;
@@ -157,4 +201,45 @@ public static string[] ReadCSV(TextAsset asset) {
157201 return asset . text . Split ( new string [ ] { "," , "\n " } , StringSplitOptions . None ) ;
158202 }
159203 }
204+
205+ // Source: https://forum.unity.com/threads/debug-drawarrow.85980/
206+ [ System . Serializable ]
207+ public class DrawingMethods {
208+ public static void ForGizmos ( Vector3 pos , Vector3 direction , float arrowHeadLength = 0.25f , float arrowHeadAngle = 20.0f ) {
209+ Gizmos . DrawRay ( pos , direction ) ;
210+
211+ Vector3 right = Quaternion . LookRotation ( direction ) * Quaternion . Euler ( 0 , 180 + arrowHeadAngle , 0 ) * new Vector3 ( 0 , 0 , 1 ) ;
212+ Vector3 left = Quaternion . LookRotation ( direction ) * Quaternion . Euler ( 0 , 180 - arrowHeadAngle , 0 ) * new Vector3 ( 0 , 0 , 1 ) ;
213+ Gizmos . DrawRay ( pos + direction , right * arrowHeadLength ) ;
214+ Gizmos . DrawRay ( pos + direction , left * arrowHeadLength ) ;
215+ }
216+
217+ public static void ForGizmos ( Vector3 pos , Vector3 direction , Color color , float arrowHeadLength = 0.25f , float arrowHeadAngle = 20.0f ) {
218+ Gizmos . color = color ;
219+ Gizmos . DrawRay ( pos , direction ) ;
220+
221+ Vector3 right = Quaternion . LookRotation ( direction ) * Quaternion . Euler ( 0 , 180 + arrowHeadAngle , 0 ) * new Vector3 ( 0 , 0 , 1 ) ;
222+ Vector3 left = Quaternion . LookRotation ( direction ) * Quaternion . Euler ( 0 , 180 - arrowHeadAngle , 0 ) * new Vector3 ( 0 , 0 , 1 ) ;
223+ Gizmos . DrawRay ( pos + direction , right * arrowHeadLength ) ;
224+ Gizmos . DrawRay ( pos + direction , left * arrowHeadLength ) ;
225+ }
226+
227+ public static void ForDebug ( Vector3 pos , Vector3 direction , float arrowHeadLength = 0.25f , float arrowHeadAngle = 20.0f ) {
228+ Debug . DrawRay ( pos , direction ) ;
229+
230+ Vector3 right = Quaternion . LookRotation ( direction ) * Quaternion . Euler ( 0 , 180 + arrowHeadAngle , 0 ) * new Vector3 ( 0 , 0 , 1 ) ;
231+ Vector3 left = Quaternion . LookRotation ( direction ) * Quaternion . Euler ( 0 , 180 - arrowHeadAngle , 0 ) * new Vector3 ( 0 , 0 , 1 ) ;
232+ Debug . DrawRay ( pos + direction , right * arrowHeadLength ) ;
233+ Debug . DrawRay ( pos + direction , left * arrowHeadLength ) ;
234+ }
235+
236+ public static void ForDebug ( Vector3 pos , Vector3 direction , Color color , float arrowHeadLength = 0.25f , float arrowHeadAngle = 20.0f ) {
237+ Debug . DrawRay ( pos , direction , color ) ;
238+
239+ Vector3 right = Quaternion . LookRotation ( direction ) * Quaternion . Euler ( 0 , 180 + arrowHeadAngle , 0 ) * new Vector3 ( 0 , 0 , 1 ) ;
240+ Vector3 left = Quaternion . LookRotation ( direction ) * Quaternion . Euler ( 0 , 180 - arrowHeadAngle , 0 ) * new Vector3 ( 0 , 0 , 1 ) ;
241+ Debug . DrawRay ( pos + direction , right * arrowHeadLength , color ) ;
242+ Debug . DrawRay ( pos + direction , left * arrowHeadLength , color ) ;
243+ }
244+ }
160245}
0 commit comments