Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
1
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.

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

Goods and bads about using Resources.load 4 Answers

Problem in loading images from resources.load(). 2 Answers

Dynamically Load Resources As Needed 0 Answers

Best way to save reference to assets in Resources Folder 0 Answers

Requesting help utilizing resources.load to auto assign rigidbody variable in a class list 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges