//using UnityEngine; //#if UNITY_EDITOR //using UnityEditor; //#endif //using System.Collections; //using System.IO; //using System; //using System.Collections.Generic; //using System.Reflection; //using System.Text; //namespace Gaia //{ // public class Utils : MonoBehaviour // { // #region Asset directory helpers // /// // /// Get raw gaia asset directory // /// // /// Base gaia directory // //public static string GetGaiaAssetDirectory() // //{ // // string path = Path.Combine(Application.dataPath, Gaia.GaiaConstants.AssetDir); // // return path.Replace('\\', '/'); // //} // /// // /// Get the asset directory for a particular featiure type // /// // /// // /// Path of feature type // //public static string GetGaiaAssetDirectory(Gaia.GaiaConstants.FeatureType featureType) // //{ // // string path = Path.Combine(Application.dataPath, Gaia.GaiaConstants.AssetDir); // // path = Path.Combine(path, featureType.ToString()); // // return path.Replace('\\', '/'); // //} // /// // /// Get a list of the Gaia stamps for the feature type provided // /// // /// // /// // //public static List GetGaiaStampsList(Gaia.GaiaConstants.FeatureType featureType) // //{ // // return new List(System.IO.Directory.GetFiles(GetGaiaAssetDirectory(featureType), "*.jpg")); // //} // /// // /// Get the full asset path for a specific asset type and name // /// // /// The type of feature this asset is // /// The file name of the asset // /// Fully qualified path of the asset // //public static string GetGaiaAssetPath(Gaia.GaiaConstants.FeatureType featureType, string assetName) // //{ // // string path = GetGaiaAssetDirectory(featureType); // // path = Path.Combine(GetGaiaAssetDirectory(featureType), assetName); // // return path.Replace('\\', '/'); // //} // /// // /// Get the full asset path for a specific asset type and name // /// // /// The type of feature this asset is // /// The file name of the asset // /// Fully qualified path of the asset // //public static string GetGaiaStampAssetPath(Gaia.GaiaConstants.FeatureType featureType, string assetName) // //{ // // string path = GetGaiaAssetDirectory(featureType); // // path = Path.Combine(GetGaiaAssetDirectory(featureType), "Data"); // // path = Path.Combine(path, assetName); // // return path.Replace('\\', '/'); // //} // /// // /// Parse a stamp preview texture to work out where the stamp lives // /// // /// Source texture // /// //// public static string GetGaiaStampPath(Texture2D source) //// { //// string path = ""; ////#if UNITY_EDITOR //// path = UnityEditor.AssetDatabase.GetAssetPath(source); ////#endif //// string fileName = Path.GetFileName(path); //// path = Path.Combine(Path.GetDirectoryName(path), "Data"); //// path = Path.Combine(path, fileName); //// path = Path.ChangeExtension(path, ".bytes"); //// path = path.Replace('\\', '/'); //// return path; //// } // /// // /// Check to see if this actually a valid stamp - needs a .jpg and a .bytes file // /// // /// Source texture // /// //// public static bool CheckValidGaiaStampPath(Texture2D source) //// { //// string path = ""; ////#if UNITY_EDITOR //// path = UnityEditor.AssetDatabase.GetAssetPath(source); ////#endif //// //path = GetGaiaAssetDirectory() + path.Replace(Gaia.GaiaConstants.AssetDirFromAssetDB, ""); //// // Check to see if we have a jpg file //// if (Path.GetExtension(path).ToLower() != ".jpg") //// { //// return false; //// } //// //Check to see if we have asset file //// string fileName = Path.GetFileName(path); //// path = Path.Combine(Path.GetDirectoryName(path), "Data"); //// path = Path.Combine(path, fileName); //// path = Path.ChangeExtension(path, ".bytes"); //// path = path.Replace('\\', '/'); //// if (System.IO.File.Exists(path)) //// { //// return true; //// } //// else //// { //// return false; //// } //// } // /// // /// Create all the Gaia asset directories for scans to go into // /// //// public static void CreateGaiaAssetDirectories() //// { ////#if UNITY_EDITOR //// string path = Path.Combine(Application.dataPath, Gaia.GaiaConstants.AssetDir); //// try //// { //// bool addedDir = false; //// foreach (Gaia.GaiaConstants.FeatureType feature in Enum.GetValues(typeof(Gaia.GaiaConstants.FeatureType))) //// { //// path = GetGaiaAssetDirectory(feature); //// if (!Directory.Exists(path)) //// { //// Directory.CreateDirectory(path); //// path = Path.Combine(path, "Data"); //// Directory.CreateDirectory(path); //// addedDir = true; //// } //// } //// if (addedDir) //// { //// AssetDatabase.Refresh(); //// } //// } //// catch (Exception e) //// { //// Debug.LogError(string.Format("Failed to create directory {0} : {1}", path, e.Message)); //// } ////#endif //// } // /// // /// Get all objects of the given type at the location in the path. Only works in the editor. // /// // /// Type of object to load // /// The path to look in // /// List of those objects // public static T[] GetAtPath(string path) // { // ArrayList al = new ArrayList(); //#if UNITY_EDITOR // string[] fileEntries = Directory.GetFiles(Application.dataPath + "/" + path); // foreach (string fileName in fileEntries) // { // int index = fileName.LastIndexOf("/"); // string localPath = "Assets/" + path; // if (index > 0) // localPath += fileName.Substring(index); // UnityEngine.Object t = UnityEditor.AssetDatabase.LoadAssetAtPath(localPath, typeof(T)); // if (t != null) // al.Add(t); // } //#endif // T[] result = new T[al.Count]; // for (int i = 0; i < al.Count; i++) // result[i] = (T)al[i]; // return result; // } // #endregion // #region Image helpers // /// // /// Make the texture supplied into a normal map // /// // /// Texture to convert // public static void MakeTextureNormal(Texture2D texture) // { // if (texture == null) // { // return; // } //#if UNITY_EDITOR // string assetPath = AssetDatabase.GetAssetPath(texture); // var tImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter; // if (tImporter != null && tImporter.textureType != TextureImporterType.NormalMap) // { // tImporter.textureType = TextureImporterType.NormalMap; // tImporter.SaveAndReimport(); // AssetDatabase.Refresh(); // } //#endif // } // /// // /// Make the texture supplied readable // /// // /// Texture to convert // public static void MakeTextureReadable(Texture2D texture) // { // if (texture == null) // { // return; // } //#if UNITY_EDITOR // string assetPath = AssetDatabase.GetAssetPath(texture); // var tImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter; // if (tImporter != null && tImporter.isReadable != true) // { // tImporter.isReadable = true; // tImporter.SaveAndReimport(); // AssetDatabase.Refresh(); // } //#endif // } // /// // /// Make the texture supplied uncompressed // /// // /// Texture to convert // public static void MakeTextureUncompressed(Texture2D texture) // { // if (texture == null) // { // return; // } //#if UNITY_EDITOR // string assetPath = AssetDatabase.GetAssetPath(texture); // var tImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter; // if (tImporter != null && tImporter.textureCompression != TextureImporterCompression.Uncompressed) // { // tImporter.textureCompression = TextureImporterCompression.Uncompressed; // tImporter.SaveAndReimport(); // AssetDatabase.Refresh(); // } //#endif // } // /// // /// Compress / encode a single layer map file to an image // /// // /// Single layer map in format x,y // /// Output image name - image image index and extension will be added // /// True if a png is wanted // /// True if a jpg is wanted // public static void CompressToSingleChannelFileImage(float[,] input, string imageName, TextureFormat imageStorageFormat = Gaia.GaiaConstants.defaultTextureFormat, bool exportPNG = true, bool exportJPG = true) // { // int width = input.GetLength(0); // int height = input.GetLength(1); // Texture2D exportTexture = new Texture2D(width, height, imageStorageFormat, false); // Color pixelColor = new Color(); // pixelColor.a = 1f; // pixelColor.r = pixelColor.g = pixelColor.b = 0f; // for (int x = 0; x < width; x++) // { // for (int y = 0; y < height; y++) // { // pixelColor.r = pixelColor.b = pixelColor.g = input[x, y]; // exportTexture.SetPixel(x, y, pixelColor); // } // } // exportTexture.Apply(); // // Write JPG // if (exportJPG) // { // ExportJPG(imageName, exportTexture); // } // // Write PNG // if (exportPNG) // { // ExportPNG(imageName, exportTexture); // } // //Lose the texture // DestroyImmediate(exportTexture); // } // /// // /// Compress / encode a multi layer map file to an image // /// // /// Multi layer map in format x,y,layer // /// Output image name - image image index and extension will be added // /// True if a png is wanted // /// True if a jpg is wanted // public static void CompressToMultiChannelFileImage(float[,,] input, string imageName, TextureFormat imageStorageFormat = Gaia.GaiaConstants.defaultTextureFormat, bool exportPNG = true, bool exportJPG = true) // { // int width = input.GetLength(0); // int height = input.GetLength(1); // int layers = input.GetLength(2); // int images = (layers + 3) / 4; // for (int image = 0; image < images; image++) // { // Texture2D exportTexture = new Texture2D(width, width, imageStorageFormat, false); // Color pixelColor = new Color(); // int layer = image * 4; // for (int x = 0; x < width; x++) // { // for (int y = 0; y < height; y++) // { // pixelColor.r = layer < layers ? input[x, y, layer] : 0f; // pixelColor.g = (layer + 1) < layers ? input[x, y, (layer + 1)] : 0f; // pixelColor.b = (layer + 2) < layers ? input[x, y, (layer + 2)] : 0f; // pixelColor.a = (layer + 3) < layers ? input[x, y, (layer + 3)] : 0f; // exportTexture.SetPixel(x, y, pixelColor); // } // } // exportTexture.Apply(); // // Write JPG // if (exportJPG) // { // byte[] jpgBytes = exportTexture.EncodeToJPG(); // Gaia.Utils.WriteAllBytes(imageName + image + ".jpg", jpgBytes); // } // // Write PNG // if (exportPNG) // { // byte[] pngBytes = exportTexture.EncodeToPNG(); // Gaia.Utils.WriteAllBytes(imageName + image + ".png", pngBytes); // } // //Lose the texture // DestroyImmediate(exportTexture); // } // } // /// // /// Compress / encode a multi layer map file to an image // /// // /// Multi layer map in format x,y,layer // /// Output image name - image image index and extension will be added // /// True if a png is wanted // /// True if a jpg is wanted // public static void CompressToMultiChannelFileImage(string imageName, HeightMap r, HeightMap g, HeightMap b, HeightMap a, TextureFormat imageStorageFormat, GaiaConstants.ImageFileType imageFileType) // { // int width = 0; // int height = 0; // if (r != null) // { // width = r.Width(); // height = r.Depth(); // } // else if (g != null) // { // width = g.Width(); // height = g.Depth(); // } // else if (b != null) // { // width = b.Width(); // height = b.Depth(); // } // else if (a != null) // { // width = a.Width(); // height = a.Depth(); // } // if (string.IsNullOrEmpty(imageName)) // { // Debug.LogError("Cannot write image - no name supplied!"); // return; // } // if (width == 0 || height == 0) // { // Debug.LogError("Cannot write image - invalid dimensions : " + width + ", " + height); // return; // } // Texture2D exportTexture = new Texture2D(width, height, imageStorageFormat, true, false); // Color pixelColor = new Color(); // for (int x = 0; x < width; x++) // { // for (int y = 0; y < height; y++) // { // pixelColor.r = r != null ? r[x, y] : 0f; // pixelColor.g = g != null ? g[x, y] : 0f; // pixelColor.b = b != null ? b[x, y] : 0f; // pixelColor.a = a != null ? a[x, y] : 1f; // exportTexture.SetPixel(x, y, pixelColor); // } // } // exportTexture.Apply(); //#if UNITY_2017_1_OR_NEWER // switch (imageFileType) // { // case GaiaConstants.ImageFileType.Jpg: // byte[] jpgBytes = ImageConversion.EncodeToJPG(exportTexture, 100); // Gaia.Utils.WriteAllBytes(imageName + ".jpg", jpgBytes); // break; // case GaiaConstants.ImageFileType.Png: // byte[] pngBytes = ImageConversion.EncodeToPNG(exportTexture); // Gaia.Utils.WriteAllBytes(imageName + ".png", pngBytes); // break; // case GaiaConstants.ImageFileType.Exr: // byte[] exrBytes = ImageConversion.EncodeToEXR(exportTexture, Texture2D.EXRFlags.CompressZIP); // Gaia.Utils.WriteAllBytes(imageName + ".exr", exrBytes); // break; // } //#else // switch (imageFileType) // { // case GaiaConstants.ImageFileType.Jpg: // byte[] jpgBytes = exportTexture.EncodeToJPG(); // Gaia.Utils.WriteAllBytes(imageName + ".jpg", jpgBytes); // break; // case GaiaConstants.ImageFileType.Png: // byte[] pngBytes = exportTexture.EncodeToPNG(); // Gaia.Utils.WriteAllBytes(imageName + ".png", pngBytes); // break; // case GaiaConstants.ImageFileType.Exr: // byte[] exrBytes = exportTexture.EncodeToEXR(Texture2D.EXRFlags.CompressZIP); // Gaia.Utils.WriteAllBytes(imageName + ".exr", exrBytes); // break; // } //#endif //#if UNITY_EDITOR // AssetDatabase.Refresh(); //#endif // //Lose the texture // DestroyImmediate(exportTexture); // } // /// // /// Convert the supplied texture to an array based on grayscale value // /// // /// Input texture - must be read enabled // /// Texture as grayscale array // public static float[,] ConvertTextureToArray(Texture2D texture) // { // float[,] array = new float[texture.width, texture.height]; // for (int x = 0; x < texture.width; x++) // { // for (int z = 0; z < texture.height; z++) // { // array[x, z] = texture.GetPixel(x, z).grayscale; // } // } // return array; // } // /// // /// Decompress a single channel from the provided file into a float array. // /// // /// File to process // /// Take data from R channel // /// Take data from G channel // /// Take data from B channel // /// Take data from A channel // /// Array of float values from the selected channel // public static float[,] DecompressFromSingleChannelFileImage(string fileName, int width, int height, TextureFormat imageStorageFormat = Gaia.GaiaConstants.defaultTextureFormat, bool channelR = true, bool channelG = false, bool channelB = false, bool channelA = false) // { // float[,] retArray = null; // if (System.IO.File.Exists(fileName)) // { // byte[] bytes = Gaia.Utils.ReadAllBytes(fileName); // Texture2D importTexture = new Texture2D(width, height, imageStorageFormat, false); // importTexture.LoadImage(bytes); // retArray = new float[width, height]; // for (int x = 0; x < width; x++) // { // for (int y = 0; y < height; y++) // { // retArray[x, y] = importTexture.GetPixel(x, y).r; // } // } // //Lose the texture // DestroyImmediate(importTexture); // } // else // { // Debug.LogError("Unable to find " + fileName); // } // return retArray; // } // /// // /// Decompress a single channel from the provided file into a float array. // /// // /// File to process // /// Take data from R channel // /// Take data from G channel // /// Take data from B channel // /// Take data from A channel // /// Array of float values from the selected channel // public static float[,] DecompressFromSingleChannelTexture(Texture2D importTexture, bool channelR = true, bool channelG = false, bool channelB = false, bool channelA = false) // { // if ((importTexture == null) || importTexture.width <= 0 || importTexture.height <= 0) // { // Debug.LogError("Unable to import from texture"); // return null; // } // float[,] retArray = new float[importTexture.width, importTexture.height]; // if (channelR) // { // for (int x = 0; x < importTexture.width; x++) // { // for (int y = 0; y < importTexture.height; y++) // { // retArray[x, y] = importTexture.GetPixel(x, y).r; // } // } // } // else if (channelG) // { // for (int x = 0; x < importTexture.width; x++) // { // for (int y = 0; y < importTexture.height; y++) // { // retArray[x, y] = importTexture.GetPixel(x, y).g; // } // } // } // else if (channelB) // { // for (int x = 0; x < importTexture.width; x++) // { // for (int y = 0; y < importTexture.height; y++) // { // retArray[x, y] = importTexture.GetPixel(x, y).b; // } // } // } // if (channelA) // { // for (int x = 0; x < importTexture.width; x++) // { // for (int y = 0; y < importTexture.height; y++) // { // retArray[x, y] = importTexture.GetPixel(x, y).a; // } // } // } // return retArray; // } // /// // /// Export a texture to jpg // /// // /// File name to us - will have .jpg appended // /// Texture source // public static void ExportJPG(string fileName, Texture2D texture) // { // byte[] bytes = texture.EncodeToJPG(); // Gaia.Utils.WriteAllBytes(fileName + ".jpg", bytes); // } // /// // /// Export a texture to png // /// // /// File name to us - will have .png appended // /// Texture source // public static void ExportPNG(string fileName, Texture2D texture) // { // byte[] bytes = texture.EncodeToPNG(); // Gaia.Utils.WriteAllBytes(fileName + ".png", bytes); // } // /// // /// Will import the raw file provided - it assumes that it is in a square 16 bit PC format // /// // /// Fully qualified file name // /// File contents or null // public static float[,] LoadRawFile(string fileName) // { // if (!System.IO.File.Exists(fileName)) // { // Debug.LogError("Could not locate heightmap file : " + fileName); // return null; // } // float[,] heights = null; // using (FileStream fileStream = File.OpenRead(fileName)) // { // using (BinaryReader br = new BinaryReader(fileStream)) // { // int mapSize = Mathf.CeilToInt(Mathf.Sqrt(fileStream.Length / 2)); // heights = new float[mapSize, mapSize]; // for (int x = 0; x < mapSize; x++) // { // for (int y = 0; y < mapSize; y++) // { // heights[x, y] = (float)(br.ReadUInt16() / 65535.0f); // } // } // } // fileStream.Close(); // } // return heights; // } // #endregion // #region Mesh helpers // /// // /// Create a mesh for the heightmap // /// // /// // /// // /// // /// // public static Mesh CreateMesh(float[,] heightmap, Vector3 targetSize) // { // //Need to sample these to not blow unity mesh sizes // int width = heightmap.GetLength(0); // int height = heightmap.GetLength(1); // int targetRes = 1; // Vector3 targetOffset = Vector3.zero - (targetSize / 2f); // Vector2 uvScale = new Vector2(1.0f / (width - 1), 1.0f / (height - 1)); // //Choose best possible target res // for (targetRes = 1; targetRes < 100; targetRes++) // { // if (((width / targetRes) * (height / targetRes)) < 65000) // { // break; // } // } // targetSize = new Vector3(targetSize.x / (width - 1) * targetRes, targetSize.y, targetSize.z / (height - 1) * targetRes); // width = (width - 1) / targetRes + 1; // height = (height - 1) / targetRes + 1; // Vector3[] vertices = new Vector3[width * height]; // Vector2[] uvs = new Vector2[width * height]; // Vector3[] normals = new Vector3[width * height]; // Color[] colors = new Color[width * height]; // int[] triangles = new int[(width - 1) * (height - 1) * 6]; // // Build vertices and UVs // for (int y = 0; y < height; y++) // { // for (int x = 0; x < width; x++) // { // colors[y * width + x] = Color.black; // normals[y * width + x] = Vector3.up; // //vertices[y * w + x] = Vector3.Scale(targetSize, new Vector3(-y, heightmap[x * tRes, y * tRes], x)) + targetOffset; // vertices[y * width + x] = Vector3.Scale(targetSize, new Vector3(x, heightmap[x * targetRes, y * targetRes], y)) + targetOffset; // uvs[y * width + x] = Vector2.Scale(new Vector2(x * targetRes, y * targetRes), uvScale); // } // } // // Build triangle indices: 3 indices into vertex array for each triangle // int index = 0; // for (int y = 0; y < height - 1; y++) // { // for (int x = 0; x < width - 1; x++) // { // triangles[index++] = (y * width) + x; // triangles[index++] = ((y + 1) * width) + x; // triangles[index++] = (y * width) + x + 1; // triangles[index++] = ((y + 1) * width) + x; // triangles[index++] = ((y + 1) * width) + x + 1; // triangles[index++] = (y * width) + x + 1; // } // } // Mesh mesh = new Mesh(); // mesh.vertices = vertices; // mesh.colors = colors; // mesh.normals = normals; // mesh.uv = uvs; // mesh.triangles = triangles; // mesh.RecalculateBounds(); // mesh.RecalculateNormals(); // return mesh; // } // /// // /// Return the bounds of both the object and any colliders it has // /// // /// Game object to check // public static Bounds GetBounds(GameObject go) // { // Bounds bounds = new Bounds(go.transform.position, Vector3.zero); // foreach (Renderer r in go.GetComponentsInChildren()) // { // bounds.Encapsulate(r.bounds); // } // foreach (Collider c in go.GetComponentsInChildren()) // { // bounds.Encapsulate(c.bounds); // } // return bounds; // } // #endregion // #region Direction helpers // /// // /// Rotate a direction vector left 90% around X axis // /// // /// Direction vector // /// Rotated direction vector // Vector3 Rotate90LeftXAxis(Vector3 input) // { // return new Vector3(input.x, -input.z, input.y); // } // /// // /// Rotate a direction vector right 90% around X axis // /// // /// Direction vector // /// Rotated direction vector // Vector3 Rotate90RightXAxis(Vector3 input) // { // return new Vector3(input.x, input.z, -input.y); // } // /// // /// Rotate a direction vector left 90% around Y axis // /// // /// Direction vector // /// Rotated direction vector // Vector3 Rotate90LeftYAxis(Vector3 input) // { // return new Vector3(-input.z, input.y, input.x); // } // /// // /// Rotate a direction vector right 90% around Y axis // /// // /// Direction vector // /// Rotated direction vector // Vector3 Rotate90RightYAxis(Vector3 input) // { // return new Vector3(input.z, input.y, -input.x); // } // /// // /// Rotate a direction vector left 90% around Z axis // /// // /// Direction vector // /// Rotated direction vector // Vector3 Rotate90LeftZAxis(Vector3 input) // { // return new Vector3(input.y, -input.x, input.z); // } // /// // /// Rotate a direction vector right 90% around Y axis // /// // /// Direction vector // /// Rotated direction vector // Vector3 Rotate90RightZAxis(Vector3 input) // { // return new Vector3(-input.y, input.x, input.z); // } // #endregion // #region Math helpers // /// // /// Return true if the values are approximately equal // /// // /// Parameter A // /// Parameter B // /// Threshold to test for // /// True if approximately equal // public static bool Math_ApproximatelyEqual(float a, float b, float threshold) // { // if (a == b || Mathf.Abs(a - b) < threshold) // { // return true; // } // else // { // return false; // } // } // /// // /// Return true if the values are approximately equal // /// // /// Parameter A // /// Parameter B // /// True if approximately equal // public static bool Math_ApproximatelyEqual(float a, float b) // { // return Math_ApproximatelyEqual(a, b, float.Epsilon); // } // /// // /// Return true if the value is a power of 2 // /// // /// Value to be checked // /// True if a power of 2 // public static bool Math_IsPowerOf2(int value) // { // return (value & (value - 1)) == 0; // } // /// // /// Returned value clamped in range of min to max // /// // /// Min value // /// Max value // /// Value to check // /// Clamped value // public static float Math_Clamp(float min, float max, float value) // { // if (value < min) // { // return min; // } // if (value > max) // { // return max; // } // return value; // } // /// // /// Return mod of value // /// // /// Value // /// Mod value // /// Mode of value // public static float Math_Modulo(float value, float mod) // { // return value - mod * (float)Math.Floor(value / mod); // } // /// // /// Return mod of value // /// // /// Value // /// Mod value // /// Mode of value // public static int Math_Modulo(int value, int mod) // { // return (int)(value - mod * (float)Math.Floor((float)value / mod)); // } // /// // /// Linear interpolation between two values // /// // /// Value 1 // /// Value 2 // /// Fraction // /// // public static float Math_InterpolateLinear(float value1, float value2, float fraction) // { // return value1 * (1f - fraction) + value2 * fraction; // } // /// // /// Smooth interpolation between two values // /// // /// Value 1 // /// Value 2 // /// Fraction // /// // public static float Math_InterpolateSmooth(float value1, float value2, float fraction) // { // if (fraction < 0.5f) // { // fraction = 2f * fraction * fraction; // } // else // { // fraction = 1f - 2f * (fraction - 1f) * (fraction - 1f); // } // return value1 * (1f - fraction) + value2 * fraction; // } // /// // /// Calculate the distance between two points // /// // /// X1 // /// Y1 // /// X2 // /// Y2 // /// // public static float Math_Distance(float x1, float y1, float x2, float y2) // { // return Mathf.Sqrt(((x1 - x2) * (x1 - x2)) + ((y1 - y2) * (y1 - y2))); // } // public static float Math_InterpolateSmooth2(float v1, float v2, float fraction) // { // float fraction2 = fraction * fraction; // fraction = 3 * fraction2 - 2f * fraction * fraction2; // return v1 * (1f - fraction) + v2 * fraction; // } // public static float Math_InterpolateCubic(float v0, float v1, float v2, float v3, float fraction) // { // float p = (v3 - v2) - (v0 - v1); // float q = (v0 - v1) - p; // float r = v2 - v0; // float fraction2 = fraction * fraction; // return p * fraction * fraction2 + q * fraction2 + r * fraction + v1; // } // /// // /// Rotate the point around the pivot - used to handle rotation // /// // /// Point to move // /// Pivot // /// Angle to pivot // /// New location // public static Vector3 RotatePointAroundPivot(Vector3 point, Vector3 pivot, Vector3 angle) // { // Vector3 dir = point - pivot; // dir = Quaternion.Euler(angle) * dir; // point = dir + pivot; // return point; // } // #endregion // #region File helpers // /// // /// Remove any characters that could cause issues with files names from the source passed in // /// // /// The source file name // /// A destination string with rubbish removed // public static string FixFileName(string sourceFileName) // { // StringBuilder sb = new StringBuilder(); // foreach (char c in sourceFileName) // { // if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_') // { // sb.Append(c); // } // } // return sb.ToString(); // } // /// // /// Open a file for reading // /// // /// File to open // /// Filestream of the opened file // public static FileStream OpenRead(string path) // { // return new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read); // } // /// // /// Returns the content of the file // /// // /// File to read // /// Content of the file // public static string ReadAllText(String path) // { // if (path == null) // throw new ArgumentNullException("path"); // if (path.Length == 0) // throw new ArgumentException("Argument_EmptyPath"); // using (StreamReader sr = new StreamReader(path, Encoding.UTF8, true, 1024)) // return sr.ReadToEnd(); // } // /// // /// Write the content to a file // /// // /// File to write // /// Content to write // public static void WriteAllText(String path, String contents) // { // if (path == null) // throw new ArgumentNullException("path"); // if (path.Length == 0) // throw new ArgumentException("Argument_EmptyPath"); // if (path == null) // throw new ArgumentNullException("contents"); // if (contents.Length == 0) // throw new ArgumentException("Argument_EmptyContents"); // using (StreamWriter sw = new StreamWriter(path, false, Encoding.UTF8, 1024)) // sw.Write(contents); // } // /// // /// Read all bytes of the supplied file // /// // /// File to read // /// Byte array of the files contents // public static byte[] ReadAllBytes(string path) // { //#if UNITY_WEBPLAYER // var file = Resources.Load(path) as TextAsset; // if (file != null) // { // return file.bytes; // } // return new byte[0]; //#else // using (FileStream s = OpenRead(path)) // { // long size = s.Length; // // limited to 2GB according to http://msdn.microsoft.com/en-us/library/system.io.file.readallbytes.aspx // if (size > Int32.MaxValue) // throw new IOException("Reading more than 2GB with this call is not supported"); // int pos = 0; // int count = (int)size; // byte[] result = new byte[size]; // while (count > 0) // { // int n = s.Read(result, pos, count); // if (n == 0) // throw new IOException("Unexpected end of stream"); // pos += n; // count -= n; // } // return result; // } //#endif // } // /// // /// Write the byte array to the supplied file // /// // /// File to write // /// Byte array to write // public static void WriteAllBytes(string path, byte[] bytes) // { // using (Stream stream = File.Create(path)) // { // stream.Write(bytes, 0, bytes.Length); // } // } // /// // /// Write a scriptable object out into a new asset that can be shared // /// // /// The scriptable object to be saved as an asset // public static void CreateAsset() where T : ScriptableObject // { //#if UNITY_EDITOR // T asset = ScriptableObject.CreateInstance(); // string path = AssetDatabase.GetAssetPath(Selection.activeObject); // if (path == "") // { // path = "Assets"; // } // else if (Path.GetExtension(path) != "") // { // path = path.Replace(Path.GetFileName(AssetDatabase.GetAssetPath(Selection.activeObject)), ""); // } // string assetPathAndName = AssetDatabase.GenerateUniqueAssetPath(path + "/New " + typeof(T).ToString() + ".asset"); // AssetDatabase.CreateAsset(asset, assetPathAndName); // AssetDatabase.SaveAssets(); // AssetDatabase.Refresh(); // EditorUtility.FocusProjectWindow(); // Selection.activeObject = asset; //#endif // } // /// // /// Get the path of the unity object supplied // /// // /// // /// // public static string GetAssetPath(UnityEngine.Object uo) // { // string path = ""; //#if UNITY_EDITOR // path = Path.Combine(Application.dataPath, AssetDatabase.GetAssetPath(uo)); // path = path.Replace("/Assets", ""); // path = path.Replace("\\", "/"); //#endif // return path; // } // /// // /// Wrap the scriptable object up so that it can be transferred without causing unity errors // /// // /// // public static string WrapScriptableObject(ScriptableObject so) // { // string newpath = ""; //#if UNITY_EDITOR // string path = GetAssetPath(so); // if (File.Exists(path)) // { // newpath = Path.ChangeExtension(path, "bytes"); // UnityEditor.FileUtil.CopyFileOrDirectory(path, newpath); // } // else // { // Debug.LogError("There is no file at the path supplied: " + path); // } //#endif // return newpath; // } // public static void UnwrapScriptableObject(string path, string newpath) // { //#if UNITY_EDITOR // if (File.Exists(path)) // { // if (!File.Exists(newpath)) // { // UnityEditor.FileUtil.CopyFileOrDirectory(path, newpath); // } // else // { // Debug.LogError("There is already a file with this name at the path supplied: " + newpath); // } // } // else // { // Debug.LogError("There is no file at the path supplied: " + path); // } //#endif // } // public static string WrapGameObjectAsPrefab(GameObject go) // { //#if UNITY_EDITOR //#if UNITY_2018_3_OR_NEWER // string name = go.name; // UnityEngine.Object prefab = PrefabUtility.SaveAsPrefabAsset(new GameObject(), "Assets/" + name + ".prefab"); // PrefabUtility.SavePrefabAsset(go); // AssetDatabase.Refresh(); // return AssetDatabase.GetAssetPath(prefab); //#else // string name = go.name; // UnityEngine.Object prefab = PrefabUtility.CreateEmptyPrefab("Assets/" + name + ".prefab"); // PrefabUtility.ReplacePrefab(go, prefab); // AssetDatabase.Refresh(); // return AssetDatabase.GetAssetPath(prefab); //#endif //#else // return ""; //#endif // } // #endregion // #region Adhoc helpers // /// // /// Check to see if a game objects layer is in the layer mask supplied // /// // /// Game object to check // /// Layer maskt to check // /// True if it is in the mask // public static bool IsInLayerMask(GameObject obj, LayerMask mask) // { // return ((mask.value & (1 << obj.layer)) > 0); // } // /// // /// Check to see if the textures are the same based on the instance id // /// // /// First texture // /// Second texture // /// If true will do an instance ID check // /// True if same instance id // public static bool IsSameTexture(Texture2D tex1, Texture2D tex2, bool checkID = false) // { // if (tex1 == null || tex2 == null) // { // return false; // } // if (checkID) // { // if (tex1.GetInstanceID() != tex2.GetInstanceID()) // { // return false; // } // return true; // } // if (tex1.name != tex2.name) // { // return false; // } // if (tex1.width != tex2.width) // { // return false; // } // if (tex1.height != tex2.height) // { // return false; // } // return true; // } // /// // /// Check to see if the game objects are the same based on the instance id // /// // /// First game object // /// Second game object // /// If true will do an instance ID check // /// True if same instance id // public static bool IsSameGameObject(GameObject go1, GameObject go2, bool checkID = false) // { // if (go1 == null || go2 == null) // { // return false; // } // //Check the instance id // if (checkID) // { // if (go1.GetInstanceID() != go2.GetInstanceID()) // { // return false; // } // return true; // } // //Check the name // if (go1.name != go2.name) // { // return false; // } // return true; // } // /// // /// Get the asset path of the first thing that matches the name // /// // /// File name to search for // /// // public static string GetAssetPath(string fileName) // { //#if UNITY_EDITOR // string fName = Path.GetFileNameWithoutExtension(fileName); // string[] assets = AssetDatabase.FindAssets(fName, null); // for (int idx = 0; idx < assets.Length; idx++) // { // string path = AssetDatabase.GUIDToAssetPath(assets[idx]); // if (Path.GetFileName(path) == fileName) // { // return path; // } // } //#endif // return ""; // } // /// // /// Get the asset path of the first thing that matches the name // /// // /// Name to search for // /// Type to search for // /// // public static string GetAssetPath(string name, string type) // { //#if UNITY_EDITOR // string[] assets = AssetDatabase.FindAssets(name, null); // string[] file; // for (int idx = 0; idx < assets.Length; idx++) // { // string path = AssetDatabase.GUIDToAssetPath(assets[idx]); // //Make sure its an exact match // file = Path.GetFileName(path).Split('.'); // if (file.GetLength(0) != 2) // { // continue; // } // if (file[0] != name) // { // continue; // } // if (file[1] != type) // { // continue; // } // return path; // } //#endif // return ""; // } // /// // /// Return GaiaSettings or null; // /// // /// Gaia settings or null if not found // public static GaiaSettings GetGaiaSettings() // { // return GetAsset("GaiaSettings.asset", typeof(Gaia.GaiaSettings)) as Gaia.GaiaSettings; // } // /// // /// Returns the first asset that matches the file path and name passed. Will try // /// full path first, then will try just the file name. // /// // /// File name as standalone or fully pathed // /// Object or null if it was not found // public static UnityEngine.Object GetAsset(string fileNameOrPath, Type assetType) // { //#if UNITY_EDITOR // if (!string.IsNullOrEmpty(fileNameOrPath)) // { // UnityEngine.Object obj = AssetDatabase.LoadAssetAtPath(fileNameOrPath, assetType); // if (obj != null) // { // return obj; // } // else // { // string path = Utils.GetAssetPath(Path.GetFileName(fileNameOrPath)); // if (!string.IsNullOrEmpty(path)) // { // return AssetDatabase.LoadAssetAtPath(path, assetType); // } // } // } //#endif // return null; // } // /// // /// Return the first prefab that exactly matches the given name from within the current project // /// // /// Asset to search for // /// Returns the prefab or null // public static GameObject GetAssetPrefab(string name) // { //#if UNITY_EDITOR // string path = Utils.GetAssetPath(name, "prefab"); // if (!string.IsNullOrEmpty(path)) // { // return AssetDatabase.LoadAssetAtPath(path); // } //#endif // return null; // } // /// // /// Return the first scriptable that exactly matches the given name from within the current project // /// // /// Asset to search for // /// Returns the prefab or null // public static ScriptableObject GetAssetScriptableObject(string name) // { //#if UNITY_EDITOR // string path = Utils.GetAssetPath(name, "asset"); // if (!string.IsNullOrEmpty(path)) // { // return AssetDatabase.LoadAssetAtPath(path); // } //#endif // return null; // } // /// // /// Return the first texture that exactly matches the given name from within the current project // /// // /// Asset to search for // /// Returns the texture or null // public static Texture2D GetAssetTexture2D(string name) // { //#if UNITY_EDITOR // string[] assets = AssetDatabase.FindAssets(name, null); // for (int idx = 0; idx < assets.Length; idx++) // { // string path = AssetDatabase.GUIDToAssetPath(assets[idx]); // if (path.Contains(".jpg") || path.Contains(".psd") || path.Contains(".png")) // { // //Make sure its an exact match // string filename = Path.GetFileNameWithoutExtension(path); // if (filename == name) // { // return AssetDatabase.LoadAssetAtPath(path); // } // } // } //#endif // return null; // } // /// // /// Get the specified type if it exists // /// // /// Name of the type to load // /// Selected type or null // public static Type GetType(string TypeName) // { // // Try Type.GetType() first. This will work with types defined // // by the Mono runtime, in the same assembly as the caller, etc. // var type = Type.GetType(TypeName); // // If it worked, then we're done here // if (type != null) // return type; // // If the TypeName is a full name, then we can try loading the defining assembly directly // if (TypeName.Contains(".")) // { // // Get the name of the assembly (Assumption is that we are using // // fully-qualified type names) // var assemblyName = TypeName.Substring(0, TypeName.IndexOf('.')); // // Attempt to load the indicated Assembly // try // { // var assembly = Assembly.Load(assemblyName); // if (assembly == null) // return null; // // Ask that assembly to return the proper Type // type = assembly.GetType(TypeName); // if (type != null) // return type; // } // catch (Exception) // { // //Debug.Log("Unable to load assemmbly : " + ex.Message); // } // } // // If we still haven't found the proper type, we can enumerate all of the // // loaded assemblies and see if any of them define the type // var currentAssembly = Assembly.GetCallingAssembly(); // { // // Load the referenced assembly // if (currentAssembly != null) // { // // See if that assembly defines the named type // type = currentAssembly.GetType(TypeName); // if (type != null) // return type; // } // } // //All loaded assemblies // Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies(); // for (int asyIdx = 0; asyIdx < assemblies.GetLength(0); asyIdx++) // { // type = assemblies[asyIdx].GetType(TypeName); // if (type != null) // { // return type; // } // } // var referencedAssemblies = currentAssembly.GetReferencedAssemblies(); // foreach (var assemblyName in referencedAssemblies) // { // // Load the referenced assembly // var assembly = Assembly.Load(assemblyName); // if (assembly != null) // { // // See if that assembly defines the named type // type = assembly.GetType(TypeName); // if (type != null) // return type; // } // } // // The type just couldn't be found... // return null; // } // #endregion // } //}