doulikedarkness

[UNITY] BundleLoading

Apr 15th, 2021 (edited)
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.65 KB | None | 0 0
  1. using UnityEngine.UI;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class FitnessGrills : MonoBehaviour
  6. {
  7.     [SerializeField] private string m_loadPath;
  8.     [SerializeField] private Image m_imageToSet;
  9.  
  10.     [SerializeField] private Sprite[] m_spritesToLoad;
  11.  
  12.     private AssetBundle m_assetBundle;
  13.     public Sprite spr;
  14.  
  15.     public void LoadRndGirlOnImage()
  16.     {
  17.         m_assetBundle = LoadBundle();
  18.         SetPicFromBundle(m_assetBundle);
  19.     }
  20.  
  21.     private AssetBundle LoadBundle()
  22.     {
  23.         Debug.Log(m_assetBundle);
  24.         if (m_assetBundle == null)
  25.         {
  26.             m_assetBundle = AssetBundle.LoadFromFile(m_loadPath);
  27.  
  28.             Debug.Log(m_assetBundle == null ? "<color=yellow>[FitnessGrills]</color> Bundle cannot be loaded or url missmatch" : "<color=yellow>[FitnessGrills]</color> Bundle succesfully loaded!");
  29.         }
  30.  
  31.         return m_assetBundle;
  32.  
  33.     }
  34.  
  35.     private void SetPicFromBundle(AssetBundle _bundle)
  36.     {
  37.         if (_bundle != null)
  38.         {
  39.             Texture2D _texture = _bundle?.LoadAsset<Texture2D>(GetRandomAssetFromBundle());
  40.  
  41.             m_imageToSet.rectTransform.sizeDelta = new Vector2(_texture.width, _texture.height);
  42.             m_imageToSet.sprite = Sprite.Create(_texture, new Rect(0f, 0f, _texture.width, _texture.height), Vector2.zero);
  43.         }
  44.     }
  45.  
  46.     private string GetRandomAssetFromBundle()
  47.     {
  48.         int randomInt = Random.Range(0, m_spritesToLoad.Length);
  49.  
  50.         for (int i = 0; i < m_spritesToLoad.Length; i++)
  51.             if (i == randomInt)
  52.                 return m_spritesToLoad[randomInt].name;
  53.  
  54.         return string.Empty;
  55.     }
  56. }
  57.  
Add Comment
Please, Sign In to add comment