Hello, again here in need for advice.
This function is loading my memory:
public List<MyBaseCompanion> creatures;
public SpriteRenderer C1;
public SpriteRenderer C2;
public SpriteRenderer C3;
public void DisplayCreature(){
if (level <= 0) {
level = 1;
}else {
int temp1 =Random.Range ((int)(level / 10)*10, (int)(level/10+1)*10);
int temp2 =Random.Range ((int)(level / 10)*10, (int)(level/10+1)*10);
int temp3 =Random.Range ((int)(level / 10)*10, (int)(level/10+1)*10);
C1.sprite = creatures [temp1].StandingSprite as Sprite;
C2.sprite = creatures [temp2].StandingSprite as Sprite;
C3.sprite = creatures [temp3].StandingSprite as Sprite;
}
MyBaseCompanion is ScriptableObject and holds my references to my Sprites.
When I call this function often it basicly loads all sprites into memory (+300 mb). ( At least thats what im thinking based on researching)
Threads about asset managment and garbage collection left me confused wheter im on the right track or not.
So if someone could hint me what methods or system come in handy for my poblem I will gladly look into them.
Thank you.
I’m confused, all the “database” scriptable objects I’ve seen (and I’m fairly new to them) are a single object that contains a list to store the data and a bunch of accessor functions… if that’s the case why are you setting up a list of scriptable objects like that?
because I havent seen code where they where used as single object, containing a List. I am also new to them and in general programming.They were recommended to me for my task. How to utulize them best I couldnt possibly know right.
So I wanted to make each creature and companion a single script and then add them if known/needed which occured legit to me .Gave them an Id number and wrote a function that sorts them to the right index , to access them. Seemed natural to me and it works just fine for me.
But thats a lil bit offtopic.I added that as Info as it might be related, but I think its not.
My concern is that when I render a sprite1 it will be load into RAM. And when I now load sprite2 in the renderer it will again add to memory without realising the unused space of sprite1. So when I swapp trough 100 sprites I have a huge usage of RAM and I basicly wanna know how to free unused memory after changing sprite.Cannot find statisfying Info that address my problem.
using UnityEngine;
using System.Collections;
public class MyBaseCompanion : ScriptableObject {
public int ID;
public string Name;
public string Description;
public Sprite StandingSprite;
public Sprite faceSprite;
public Sprite attackSprite;
public float DmgRatio;
public float SpeedRatio;
public NewBehaviourScript ActiveSkill;
public NewBehaviourScript PassiveSkill;
}
You can replicate this problem by simply putting a sprite into your scene and then swap Sprites in Spriterenderer in playmode.Must be different Sprites of course.Adds up if you have many.
public class LoadRespurces : MonoBehaviour {
int i = 150;
public SpriteRenderer untzi;
public void LoadMySprites(){
i++;
untzi.sprite = Resources.Load ("character_" + i,typeof(Sprite)) as Sprite;
Resources.UnloadUnusedAssets ();
}
I tried now to load them from resource and then call UnloadUnusedAssets();
I monitored RAM and it didnt add up memory as oposed when you simply pass a Reference in the spriterenderer.
Guess for a large quantity of sprites/images you should rather use Resource as it offers of a method to unload them from your RAM.
Somebody pls correct me there if I talk nonsence.