Search Unity

Generate Sprite inside a region

Discussion in 'Scripting' started by adriannungka, Jan 22, 2014.

  1. adriannungka

    adriannungka

    Joined:
    Jul 15, 2013
    Posts:
    2
    Hi,

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

    $scene.png

    And below is the inspector views :-

    $scene2.png

    This is my code :-
    Code (csharp):
    1.  
    2. [CustomEditor(typeof(TreeSpriteGenerator))]
    3. public class TreeSpriteGeneratorEditor : Editor {
    4.  
    5.     private Vector3 topLeft, bottomLeft,
    6.     topRight, bottomRight;
    7.  
    8.     public void OnSceneGUI() {
    9.    
    10.         var verts = DrawBounds();
    11.  
    12.         Handles.DrawPolyLine(verts);
    13.  
    14.         var t = target as TreeSpriteGenerator;
    15.         var sprites = ((TreeSpriteGenerator)target).sprites;
    16.         var distribution = ((TreeSpriteGenerator)target).distribution;
    17.         var freq = ((TreeSpriteGenerator)target).spriteFrequency;
    18.         int i=0, size = sprites.Length;
    19.         float width = bottomRight.x - bottomLeft.x,
    20.             height =  topLeft.y - bottomLeft.y;
    21.     }
    22.  
    23.     private Vector3[] DrawBounds()
    24.     {
    25.         var t = (TreeSpriteGenerator)target;
    26.  
    27.         return new Vector3[] {
    28.             bottomLeft = new Vector3(t.transform.position.x - t.transform.localScale.x, t.transform.position.y - t.transform.localScale.y, 0f),
    29.             bottomRight = new Vector3(t.transform.position.x + t.transform.localScale.x, t.transform.position.y - t.transform.localScale.y, 0f),
    30.             topRight = new Vector3(t.transform.position.x + t.transform.localScale.x, t.transform.position.y + t.transform.localScale.y, 0f),
    31.             topLeft = new Vector3(t.transform.position.x - t.transform.localScale.x, t.transform.position.y + t.transform.localScale.y, 0f),
    32.             bottomLeft
    33.         };
    34.     }
    35.  
    Thanks in advance.
     
  2. AngryGenius

    AngryGenius

    Joined:
    Jan 21, 2014
    Posts:
    14
    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:

    Code (csharp):
    1.  
    2. Rect zone = new Rect(-2.5,-2.5,5,5);
    3. Vector3 point = new Vector3(Random.Range(zone.xMin, zone.xMax), 0, Random.Range(zone.yMin, zone.yMax));
    Tell me how it goes! :D
     
  3. adriannungka

    adriannungka

    Joined:
    Jul 15, 2013
    Posts:
    2
    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. :D

    Code (csharp):
    1.  
    2.  
    3. 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);
    4.        
    5.