Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Asset Management question...also memory usage/bloat

Discussion in 'Scripting' started by tomjoe, Dec 9, 2015.

  1. tomjoe

    tomjoe

    Joined:
    May 11, 2015
    Posts:
    44
    Hi everyone.
    I was trying to make my assets a little easier to access. i thought if i added a Reference script that holds strings of their locations it might help. then rather than typing out the location(/Prefabs/Ships/StarShip), i type AssetRef.ships.starship. taking advantage of the auto complete.



    Code (CSharp):
    1. public class AssetReference_Script  
    2. {
    3.     //root Asset/Resources/  
    4.     public ShipsReference ships = new ShipsReference();
    5.     public WeaponsReference weapons = new WeaponsReference();
    6. }
    7.  
    8. public class ShipsReference
    9. {  
    10.     public string enemyShip = "/Prefabs/Ships/EnemyShip";
    11.     public string patrolShip1 = "/Prefabs/Ships/PatrolShip1";
    12.     public string starShip = "/Prefabs/Ships/StarShip";
    13. }
    14.  
    15. public class WeaponsReference
    16. {
    17.     public string weapon1 = "Prefabs/Weapons/Weapon1";
    18.     public string weapon2green = "Prefabs/Weapons/Weapon2green";
    19. }

    so in another script i can call this
    Code (CSharp):
    1.  
    2. public class scriptA: MonoBehaviour {
    3.  
    4. public AssetReference_Script assRef =  new AssetReference_Script();
    5. Instantiate(Resources.Load(assRef.ships.starShip))
    6. }

    i am wondering about the memory ramifications of this.
    if i use the public AssetReference_Script assRef = new AssetReference_Script();
    in a script. will it add the whole script AssetReference_Script to my ScriptA? effectively increasing scriptA's memory requirment by AssetReference_Script memory requirement?

    since this AssetReference_Script is going to have hundreds of strings in it, i really don't want to bloat a short script by adding it to it.
    thanks for reading.
     
  2. dani-unity-dev

    dani-unity-dev

    Joined:
    Feb 22, 2015
    Posts:
    174
    Make your string members static instead of creating new instances of your class.

    Code (csharp):
    1. public static class MyStrings
    2. {
    3.     public static readonly string MyString1 = "Whatever";
    4.  
    5.     public static readonly string MyString2 = "SomethingElse";
    6. }
    7.  
    8. // In another script
    9. Resources.Load(MyStrings.MyString1);
    May I ask why are you using Resource.Load? Do you know you can set a reference to the prefab you want to clone in the Editor?
     
    Last edited: Dec 10, 2015
    tomjoe and zombiegorilla like this.
  3. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,987
    Indeed, this is really taking the long way around. If you are going to surface the path in the inspector, just reference the asset directly through the inspector. It's more efficient, stable and less prone to error.
     
    TonyLi and tomjoe like this.
  4. tomjoe

    tomjoe

    Joined:
    May 11, 2015
    Posts:
    44
    thank you both for your advice. ill adopt this in my project.

    is there a way to retain the structure i had? like MyStrings.ships.starShip?
    i tried the following, but it doesn't work.


    Code (CSharp):
    1. public static class MyStrings
    2. {
    3.     public static readonly string MyString1 = "Whatever";
    4.     public static readonly string MyString2 = "SomethingElse";
    5.  
    6. public static readonly MyStrings2 = new MyStrings2();
    7. }
    8. public static class MyStrings2
    9. {
    10. public static string starship = "/s/starShip";
    11. }
    12.  
    13. // In another script
    14. Resources.Load(MyStrings.MyString2.starship);
    it says:
    Member 'MyStrings2.starship' cannot be accessed with an instance reference; qualify it with a type name instead


    I have been trying to avoid making connections in the editor whenever possible. a couple of months ago something happened to my project resulting in all the editor made connections being lost. made me kind of leery of it. i still use it for quick prototyping assets and scripts. i just prefer to do the later drafts fully scripted.
     
  5. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,987
    You can use scriptable objects to store references (and populate them manually or via editor script.). Alternatively, you can use asset bundles. I wouldn't rely too heavily on resources folders, they will be going away at some point.
     
  6. dani-unity-dev

    dani-unity-dev

    Joined:
    Feb 22, 2015
    Posts:
    174
    Yes, leave MyStrings unchanged and make MyStrings2 a struct and not a static class. Also remove static from all members of MyStrings2.

    Code (csharp):
    1. public struct MyStrings2
    2. {
    3.     public readonly string starship = "/s/starShip";
    4. }
    This will work but it doesn't make a lot of sense.
     
  7. tomjoe

    tomjoe

    Joined:
    May 11, 2015
    Posts:
    44
    Thank you so much. i really appreciate everything.
    what doesn't make sense about it? how would you suggest i reference the assets?

    Why would they remove the resources?
     
  8. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,987
    You reference them directly in a public field. This can be in a prefab, scriptable object, or in the scene (though as you have encountered, scene referencing can be fragile, I wouldn't recommend that)

    Resources folder is meant for when you need to access something by file path, typically assets that change outside the scope of the project (and editor scripts/tools). It's rare that you would actually need it in a game. You would normally use asset bundles for that, but they have been pretty bulky and complex, overkill for simple stuff. But they are reworking and making them much more streamlined, eliminating the need for the resources folder.
     
  9. tomjoe

    tomjoe

    Joined:
    May 11, 2015
    Posts:
    44
    Ahh cool, thanks for the info. I've never done anything with asset bundles. thought they were just for file transfers. ill take another look in to them :)