Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine.UI;
- using System.Collections.Generic;
- using UnityEngine;
- public class FitnessGrills : MonoBehaviour
- {
- [SerializeField] private string m_loadPath;
- [SerializeField] private Image m_imageToSet;
- [SerializeField] private Sprite[] m_spritesToLoad;
- private AssetBundle m_assetBundle;
- public Sprite spr;
- public void LoadRndGirlOnImage()
- {
- m_assetBundle = LoadBundle();
- SetPicFromBundle(m_assetBundle);
- }
- private AssetBundle LoadBundle()
- {
- Debug.Log(m_assetBundle);
- if (m_assetBundle == null)
- {
- m_assetBundle = AssetBundle.LoadFromFile(m_loadPath);
- Debug.Log(m_assetBundle == null ? "<color=yellow>[FitnessGrills]</color> Bundle cannot be loaded or url missmatch" : "<color=yellow>[FitnessGrills]</color> Bundle succesfully loaded!");
- }
- return m_assetBundle;
- }
- private void SetPicFromBundle(AssetBundle _bundle)
- {
- if (_bundle != null)
- {
- Texture2D _texture = _bundle?.LoadAsset<Texture2D>(GetRandomAssetFromBundle());
- m_imageToSet.rectTransform.sizeDelta = new Vector2(_texture.width, _texture.height);
- m_imageToSet.sprite = Sprite.Create(_texture, new Rect(0f, 0f, _texture.width, _texture.height), Vector2.zero);
- }
- }
- private string GetRandomAssetFromBundle()
- {
- int randomInt = Random.Range(0, m_spritesToLoad.Length);
- for (int i = 0; i < m_spritesToLoad.Length; i++)
- if (i == randomInt)
- return m_spritesToLoad[randomInt].name;
- return string.Empty;
- }
- }
Add Comment
Please, Sign In to add comment