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

Creating Sprite at Runtime with AssetBundle. Bounds change as compared to the original image.

Discussion in '2D' started by Sam-K, Aug 12, 2014.

  1. Sam-K

    Sam-K

    Joined:
    Mar 23, 2013
    Posts:
    27
    Code (CSharp):
    1. IEnumerator Start () {
    2.         GameObject test = new GameObject ("SampleBundle");
    3.         test.AddComponent (typeof(SpriteRenderer));
    4.  
    5.         yield return StartCoroutine (DownloadManager.Instance.WaitDownload ("MyBundle.assetBundle"));
    6.  
    7.         WWW www = DownloadManager.Instance.GetWWW("MyBundle.assetBundle");
    8.         AssetBundle bundle = www.assetBundle;
    9.         Texture2D tex = bundle.Load (assetName) as Texture2D;
    10.         Sprite sprite = GameObject.Find ("New Sprite").GetComponent<SpriteRenderer> ().sprite;
    11.         Debug.Log (sprite.bounds);
    12.         test.GetComponent<SpriteRenderer> ().sprite = Sprite.Create(tex, sprite.rect, new Vector2(0.5f,0.5f));
    13.         Debug.Log (test.GetComponent<SpriteRenderer> ().bounds);
    14.         Debug.Log (test.GetComponent<SpriteRenderer> ().sprite.bounds);
    15.         //        test.GetComponent<SpriteRenderer> ().sprite = Resources.Load <Sprite>(assetName);
    16.     }
    Im facing a problem I have sprite in scene. When i load a sprite from resources its exactly the same as the one in the scene. But when i Load it from AssetBundle the bounds of the sprite change and if i do ti like
    Code (CSharp):
    1. Sprite.Create(tex, sprite.rect, new Vector2(0.5f,0.5f), 80);
    Then it works but its not the correct way.


    Output:
    Center: (0.0, 0.0, 0.0), Extents: (6.4, 4.0, 0.1)
    Center: (0.0, 0.0, 0.0), Extents: (5.1, 3.2, 0.1)
    Screenshot mages is attached for referece.
     

    Attached Files: