Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question How to dynamically add Circle and Rectangle Sprites

Discussion in 'Editor & General Support' started by aarelovich, Sep 4, 2023.

  1. aarelovich

    aarelovich

    Joined:
    Dec 4, 2019
    Posts:
    6
    Hello.

    I have a use case (not a video game) where I need to dynamically add Circle and Square sprites (as I don't know before hand how many I will need to use) to an empty container game object. All I need to do once I add them is change the position, scale, rotation and color.

    I can't seem to find a code to do this as all examples that I've found seem to load resources from disk, but when I add a Circle or Square via menu there is not path as to which resource is being loaded.

    Just to be clear I have the 2D Sprite package install that gives me these primitives.

    Can anyone help me on how I can do this?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,969
    There's sample code in my
    testfingerpolygons
    scene within my MakeGeo project to do this sort of thing.

    You would need to generate the points you want and feed them into the above routine. There's also examples in there to make square and circle shapes in general, so perhaps those are closer to what you want. Either way, all the steps for what you need are in that project.

    MakeGeo is presently hosted at these locations:

    https://bitbucket.org/kurtdekker/makegeo

    https://github.com/kurtdekker/makegeo

    https://gitlab.com/kurtdekker/makegeo

    https://sourceforge.net/p/makegeo
     
  3. aarelovich

    aarelovich

    Joined:
    Dec 4, 2019
    Posts:
    6
    Thank you for your answer! I actually ended up finding out how to do this by myself. But I will be sure to read the code in case there are better solutions than my own.

    Simply for reference, I found the path of the Circle.png asset for the Sprite. Copied said asset to the Assets/Resources directory of my project and Imported the sprite like this:

    Code (CSharp):
    1.       // We create the game object. With the circle and a counter of the circles added. Just so they all have unique names.
    2.       SpriteCounter++;
    3.       GameObject go = new GameObject("Circle" + SpriteCounter);
    4.      
    5.       // The sprite is loaded from the circle image as textrue 2D. And the color of the circle is set.
    6.       SpriteRenderer renderer = go.AddComponent<SpriteRenderer>();
    7.       renderer.color = color;
    8.       Texture2D tex = Resources.Load<Texture2D>("Circle");      
    9.  
    10.       // This loading function load the pixels per unit to be equal to the texture width (which should be equal to it's height).
    11.       // So that means that the loaded sprite will ocuppy a single unit of world space in height and width.
    12.       Sprite sprite = Sprite.Create(tex, new UnityEngine.Rect(0.0f,0.0f,tex.width,tex.height), new Vector2(0.5f, 0.5f), (float) tex.width);
    13.       renderer.sprite = sprite;
    14.       Debug.Log($"Sprite Bounds: {sprite.bounds}");
    15.