Generate Sprite inside a region

Hi,

I’m looking for a way to put sprites inside a region like below :-

1492726--83412--$scene.png

And below is the inspector views :-

1492726--83413--$scene2.png

This is my code :-

[CustomEditor(typeof(TreeSpriteGenerator))]
public class TreeSpriteGeneratorEditor : Editor {

	private Vector3 topLeft, bottomLeft,
	topRight, bottomRight;

	public void OnSceneGUI() {
	
		var verts = DrawBounds();

		Handles.DrawPolyLine(verts);

		var t = target as TreeSpriteGenerator;
		var sprites = ((TreeSpriteGenerator)target).sprites;
		var distribution = ((TreeSpriteGenerator)target).distribution;
		var freq = ((TreeSpriteGenerator)target).spriteFrequency;
		int i=0, size = sprites.Length;
		float width = bottomRight.x - bottomLeft.x,
			height =  topLeft.y - bottomLeft.y;
	}

	private Vector3[] DrawBounds()
	{
		var t = (TreeSpriteGenerator)target;

		return new Vector3[] {
			bottomLeft = new Vector3(t.transform.position.x - t.transform.localScale.x, t.transform.position.y - t.transform.localScale.y, 0f),
			bottomRight = new Vector3(t.transform.position.x + t.transform.localScale.x, t.transform.position.y - t.transform.localScale.y, 0f),
			topRight = new Vector3(t.transform.position.x + t.transform.localScale.x, t.transform.position.y + t.transform.localScale.y, 0f),
			topLeft = new Vector3(t.transform.position.x - t.transform.localScale.x, t.transform.position.y + t.transform.localScale.y, 0f),
			bottomLeft
		};
	}

Thanks in advance.

Heya! It’s actually simpler than you think! There’s a built in class called Rect which can hold the zone size and position, and using that it’s pretty easy to use Random.Range to get the actual position. Here’s an example of getting a random point inside a 5^2 square centered at origin:

Rect zone = new Rect(-2.5,-2.5,5,5);
Vector3 point = new Vector3(Random.Range(zone.xMin, zone.xMax), 0, Random.Range(zone.yMin, zone.yMax));

Tell me how it goes! :smile:

Hi AngryGenius,

Yeah, it works.

I wrote the code like below to make sure the object always at the center whenever you resize/scale it.

Btw, thank you so much. I din’t know that it was this simple. :smile:

var zone = new Rect(_parent.transform.position.x -_parent.transform.localScale.x/2, _parent.transform.position.y -_parent.transform.localScale.y/2, _parent.transform.localScale.x, _parent.transform.localScale.y);