• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by stevesan · Dec 04, 2016 at 01:07 AM · resources.loadguid

Can I do something like Resource.Load, but pass it a GUID instead of a path?

It would be nice to load by GUID, so we could rename stuff without worrying about our Resource.Load calls.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Bunny83 · Dec 04, 2016 at 03:57 AM

Uhm, no. The actual asset GUIDs are only used inside the editor to track an asset inside the project. That GUID is actually in the metadata file and not part of the actual asset.

The point of "Resource.Load" is that you can load resources by name. If you want to directly link stuff (which would be better anyways) you should assign that asset to a serialized variable in the inspector. That way you can rename your asset with inside the editor without any problems.

If you don't have a reasonable class / asset where you could link those assets you can create a dedicated "asset manager" ScriptableObject.

For example something like this:

 //AssetManager.cs
 using UnityEngine;
 using System.Collections.Generic;
 
 [System.Serializable]
 public class AssetItem
 {
     public string name;
     public string guid;
     public Object asset;
 }
 
 [CreateAssetMenu( fileName = "Resources/AssetManager", menuName = "Create AssetManager")]
 public AssetManager : ScriptableObject
 {
     private static AssetManager m_Instance = null;
     public static AssetManager Instance
     {
         get
         {
             if (m_Instance == null)
                 m_Instance = Resources.Load("AssetManager");
             return m_Instance;
         }
     }
     public List<AssetItem> assets;
     public static T FindByName<T>(string aName) where T : Object
     {
         foreach(var item in Instance.assets)
         {
             T tmp = item.asset as T;
             if(item.name == aName)
                 return tmp;
         }
         return null;
     }
     public static T FindByGuid<T>(string aGuid) where T : Object
     {
         foreach(var item in Instance.assets)
         {
             T tmp = item.asset as T;
             if(item.guid == aGuid && tmp != null)
                 return tmp;
         }
         return null;
     }
 }

This class allows you to create a ScriptableObject which you should save into your Resources folder and give it the name "AssetManager". Now you can simply edit it in the inspector. Just add as many assets you like by increasing the list count, and dragging your assets into the "asset" slot of each element. You can give them a freely choosen name and guid (which both are just strings).

It provides two static methods which allows you to find and return an asset from that list based on the custom name or guid you have given it. Since those are generic methods you can directly let it cast the result to the wanted type, just like Resources.Load does.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

56 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Get file in project using it's path 1 Answer

when loading a textasset from .txt file, the quotations disappear?? 1 Answer

Load file name form Resources folder with special characters 1 Answer

Change Sprite within an image component with a script 1 Answer

Do we need to Load Referenced assets of a Dynamically loaded prefab, when those assets are also in Resources folder ? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges