Search Unity

Need some advice on placing series of images

Discussion in 'Getting Started' started by SwingNinja, Dec 12, 2018.

  1. SwingNinja

    SwingNinja

    Joined:
    Mar 13, 2017
    Posts:
    17
    Hi,

    I'm trying to figure out how to place a bunch of images into the scene. I created an array of sprites. Each sprite has it's own x,y position respectively. They're mainly serve as background (no interaction), but they need to be placed in certain order (Items75, then Items74, then ... and so on). Can anyone give me some advice on how to proceed from here? Thanks.


     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Selecting the whole lot and dragging them into the Hierarchy doesn't do it? What about grabbing the Amusement Park parent object, and dragging that in?
     
  3. SwingNinja

    SwingNinja

    Joined:
    Mar 13, 2017
    Posts:
    17
    Sorry, I wasn't very clear. I am trying to do it through a script. I've been looking at texture2D, image, resources.load, etc and got even more confused.
     
  4. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    What about a Scriptable Object with a list of images. Your code could load the SO and spit them out in that order.

    If you want to be fancy, you can make an editor script to make the list sortable/rearrangable to make it easier to work with.
     
  5. SwingNinja

    SwingNinja

    Joined:
    Mar 13, 2017
    Posts:
    17
    This is what I come up with in the past hour or so:
    1. I moved the "Amusement Park" (notice the spelling) folder (or prefab?) under "Assets/Resources" folder.
    2. In the scene, I created Empty that called the script (HelloWorld.cs)
    3. And HelloWorld.cs looks like this:
      1. Code (CSharp):
        1.     void Start () {
        2.         //Debug.Log("Hello World!");
        3.  
        4.         GameObject go = new GameObject();
        5.         SpriteRenderer renderer = go.AddComponent<SpriteRenderer>();
        6.  
        7.         renderer.sprite = Resources.Load("Amusement Park", typeof(Sprite)) as Sprite;
        8.  
        9.         if (renderer.sprite)
        10.             Debug.Log("Hello World!");
      2. It returns nothing. So I assume I didn't call it correctly.
     
  6. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    Is there a reason you don't want to try it the way I suggested?

    Untested code follows:
    Code (CSharp):
    1. // SpriteCollection.cs
    2. // - - -
    3. // Create a new instance of this SO and populate
    4. // the images property with your images
    5. [CreateAssetMenu(menuName="Image Collection")]
    6. public class SpriteCollection : ScriptableObject {
    7.     public List<Image> images = new List<Image>();
    8. }
    9.  
    10.  
    11. // SpriteUser.cs
    12. public class SpriteUser: MonoBehaviour {
    13.     // Assign your SO instance to this property
    14.     public SpriteCollection imageCollection;
    15.  
    16.     void Awake() {
    17.         foreach (Image img in imageCollection.images) {
    18.             GameObject obj = new GameObject();
    19.  
    20.             // Set position, parent, etc as needed
    21.  
    22.             obj.AddComponent<SpriteRenderer>().sprite = img;
    23.         }
    24.     }
    25. }
     
    SwingNinja likes this.
  7. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I got super-ninja'd because I got called away for about 6 hours in the middle of typing this, but so those electrons don't go to waste, here was my suggestion:

    Just make an array of Sprite in your script:

    Code (csharp):
    1.  
    2.     public Sprite spritePrefabs[];
    3.  
    ...attach this script to something in your scene, and then drag all those sprites in and drop them on that property in the Inspector. (Pro tip: hit the little padlock icon on your object so it stays selected, and then you can select ALL the sprites in the Project tab, and drag them onto the array property all at once, rather than doing it one at a time. Just remember to unlock the selected object when done!)

    Then all you have to do is iterate over this list in the code and instantiate each one.
     
    SwingNinja and Schneider21 like this.
  8. SwingNinja

    SwingNinja

    Joined:
    Mar 13, 2017
    Posts:
    17
    Thanks everyone for the tips.

    I finally got it to work with SpriteRenderer array. In the end, it's sort of a combination between the two of proposed solutions. Now I can move on to other issues :)
     
    JoeStrout likes this.