public TextureTree AddTexture(Texture2D texture, int index) { if (!texture) { return(null); } if (HasChildren) { return(Left.AddTexture(texture, index) ?? Right.AddTexture(texture, index)); } if (this.texture) { return(null); } if (rect.width < texture.width || rect.height < texture.height) { return(null); } if (rect.width == texture.width && rect.height == texture.height) { this.texture = texture; this.index = index; name = texture.name; return(this); } InitChildren(); var deltaW = rect.width - texture.width; var deltaH = rect.height - texture.height; if (deltaW > deltaH) { Left.rect = new Rect(rect.xMin, rect.yMin, texture.width, rect.height); Right.rect = new Rect(rect.xMin + texture.width + padding, rect.yMin, rect.width - texture.width - padding, rect.height); } else { Left.rect = new Rect(rect.xMin, rect.yMin, rect.width, texture.height); Right.rect = new Rect(rect.xMin, rect.yMin + texture.height + padding, rect.width, rect.height - texture.height - padding); } return(Left.AddTexture(texture, index)); }