/// <summary> /// Gets the first empty space in <paramref name="atlas"/> that has at least the size of <paramref name="pixelScale"/>. /// </summary> /// <param name="atlas">The <see cref="dfAtlas"/> to find the empty space in.</param> /// <param name="pixelScale">The required size of the empty space.</param> /// <returns>The rect of the empty space divided by the atlas texture's size.</returns> public static Rect FindFirstValidEmptySpace(this dfAtlas atlas, IntVector2 pixelScale) { if (atlas == null || atlas.Texture == null || !atlas.Texture.IsReadable()) { return(new Rect(0f, 0f, 0f, 0f)); } Vector2Int point = new Vector2Int(0, 0); int pointIndex = -1; List <RectInt> rects = atlas.GetPixelRegions(); //float x = 0; //float y = 0; while (true) { bool shouldContinue = false; foreach (RectInt rint in rects) { if (rint.DoseOverlap(new RectInt(point, pixelScale.ToVector2Int()))) { shouldContinue = true; pointIndex++; if (pointIndex >= rects.Count) { return(new Rect(0f, 0f, 0f, 0f)); } point = rects[pointIndex].max + Vector2Int.one; if (point.x > atlas.Texture.width || point.y > atlas.Texture.height) { atlas.ResizeAtlas(new IntVector2(atlas.Texture.width * 2, atlas.Texture.height * 2)); } break; } bool shouldBreak = false; foreach (RectInt rint2 in rects) { RectInt currentRect = new RectInt(point, pixelScale.ToVector2Int()); if (rint2.x < currentRect.x || rint2.y < currentRect.y) { continue; } else { if (currentRect.DoseOverlap(rint2)) { shouldContinue = true; shouldBreak = true; pointIndex++; if (pointIndex >= rects.Count) { return(new Rect(0f, 0f, 0f, 0f)); } point = rects[pointIndex].max + Vector2Int.one; if (point.x > atlas.Texture.width || point.y > atlas.Texture.height) { atlas.ResizeAtlas(new IntVector2(atlas.Texture.width * 2, atlas.Texture.height * 2)); } break; } } } if (shouldBreak) { break; } } if (shouldContinue) { continue; } RectInt currentRect2 = new RectInt(point, pixelScale.ToVector2Int()); if (currentRect2.xMax > atlas.Texture.width || currentRect2.yMax > atlas.Texture.height) { atlas.ResizeAtlas(new IntVector2(atlas.Texture.width * 2, atlas.Texture.height * 2)); } break; } RectInt currentRect3 = new RectInt(point, pixelScale.ToVector2Int()); Rect rect = new Rect((float)currentRect3.x / atlas.Texture.width, (float)currentRect3.y / atlas.Texture.height, (float)currentRect3.width / atlas.Texture.width, (float)currentRect3.height / atlas.Texture.height); return(rect); }