Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Sprite size in a prefab

Discussion in 'Scripting' started by Madpam, May 21, 2015.

  1. Madpam

    Madpam

    Joined:
    May 21, 2015
    Posts:
    4
    Hi. My script randomly takes one prefab and add it to the end of the scene. How can I determine sprite's size in a prefab? Or size of the prefab?
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    What exactly do you mean by "size"?
     
    krougeau likes this.
  3. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    451
    If the prefab in question is a sprite, you should locate the original sprite in your Assets folder (wherever you have it stored), click on it, and then examine the information at the bottom of the preview pane, which will give you the sprite's dimensions (see bottom-most circle). Other than that, you may be interested in checking the Pixels Per Unit settings and the Max Size settings (upper two circles), which control how the image is displayed in Unity.



    If, however, your sprite is packed into a prefab game object, you'll want to find that prefab in your Assets folder and check it's Scale settings.
     
    Madpam likes this.
  4. Madpam

    Madpam

    Joined:
    May 21, 2015
    Posts:
    4
    Ok, here an image of a prefab. Is it possible to know:
    1) The width of the prefab in units.
    2) Size of a child object, which has only sprite renderer (background)
    3) Size of a child object with a collider (floor)
     
  5. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    451
    Ah, I think I see now, at least for the most part... If these are all sprite's, then you might want to look into checking their bounds via scripting. http://docs.unity3d.com/ScriptReference/Bounds.html

    Try putting this script on each object you wish to test that has a Sprite Renderer (make a new C# script and name it BoundsTest):
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BoundsTest : MonoBehaviour {
    5.  
    6.     private Vector3 size;
    7.     private Renderer rend;
    8.  
    9.     void Start()
    10.     {
    11.         rend = GetComponent<Renderer>();
    12.         size = rend.bounds.size;
    13.         Debug.Log(gameObject.name + "'s width is: " + size.x);
    14.         Debug.Log(gameObject.name + "'s heigth is: " + size.y);
    15.         Debug.Log(gameObject.name + "'s depth is: " + size.z);
    16.     }
    17. }
    That'll give you the width, height, and depth of the sprite in world units. Tested it myself, seems to work fine.
     
    Madpam and JoeStrout like this.
  6. Madpam

    Madpam

    Joined:
    May 21, 2015
    Posts:
    4
    Thank you! I add
    Code (CSharp):
    1. room.transform.FindChild("floor").GetComponent<Renderer>();
    2. size = rend.bounds.size;
    3. Debug.Log(size.x);
    One more quastion here. Is it bad for perfomance to use FindChild?
     
    krougeau likes this.
  7. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    451

    To my limited knowledge, any sort of "Find" tends to ding performance, yes. Best to assign variables directly wherever possible (or use singletons), and if you must use Find, make sure to declare the variables and then do your Finds in Awake or Start so that they don't have to be found again. Using Finds in Update or any other code that executes constantly can cause quite a mess (as can declaring "new" variables, ie, "whatever = new Vector3(yada, yada, yada)"). There are some great blog posts and forum threads about code optimization around here someplace, sorry I don't have the links handy.
     
    Madpam likes this.
  8. Madpam

    Madpam

    Joined:
    May 21, 2015
    Posts:
    4
    Thank you again!
     
    krougeau likes this.