Giter Club home page Giter Club logo

assetpathattribute's Introduction

AssetPathAttribute

AssetPathAttribute is a Unity attribute used in Unity to allow you to have drag and drop asset references in the inspector but really only serialize string paths.

Why?

If you use object references in Unity when you load one asset every asset it references is also loaded into memory and this is not really ideal. The other option is to load everything from resources by using it's path. So instead of having a direct reference you have a string field. These are really hard to work with as it's easy to make a mistake and if the asset moves and has become invalid you will not know. You also can't jump between objects very quickly. This is a huge hit to usability. Asset Path Attribute combines the best of both options into one using some inspector magic.

How it works

In the top box we have the inspector in normal mode. As you can see there are object references. However when you go to debug mode you will see we have string paths. This is what AssetPathAttribute does. The code for the asset path inspector drawn above is the following

[CreateAssetMenu]
public class ValueExample : ScriptableObject
{
   [AssetPath.Attribute(typeof(GameObject))]
   public string m_PlayerPrefab;

   [AssetPath.Attribute(typeof(Transform))]
   public string m_PlayerSpawn;

   [AssetPath.Attribute(typeof(AudioSource))]
   public string m_PlayerAudioSource;
}

As seen above all you need to do is add the AssetPath.Attribute(Type) above a string field. When Unity goes and draws that string field it will instead use the AssetPathDrawer.cs and create your object field. If you put the attribute above a invalid type (anything but a string) the inspector will show the following.

Instead of just defaulting back to the normal inspector (which would be really easy to do) I display a error just so you know you are currently referencing the object directly.

Valid Targets

Asset Path Attribute just piggybacks off the Unity serialization system and can't draw object fields for types that Unity does not support. Any type that inherits from UnityEngine.Object can be used with AssetPathAttribute.

Helper Functions

When working with Asset Path Attribute it store all paths as project paths i.e all start at the base Assets/ folder. In terms of runtime this is not useful to you since to load objects they need to be in a Resources folder. To convert a Project Path to a Resources path you can use the following.

public class UIManager
{
   [AssetPath.Attribute(typeof(UnityEngine.UI.Image))]
   public string m_UIImage;

   public void ConvertExample()
   {
       // Takes a path from 'Assets/Resources/Player/PlayerIcon.prefab'
       // and converts it to `Player/PlayerIcon.prefab'
       string resourcesPath = AssetPath.ConvertToResourcesPath(m_UIImage);
   }
}

You can then go ahead and load the asset using Resources.Load<T>(string path) There is a second function that you can also use that will load the object for you.

[AssetPath.Attribute(typeof(GameObject))]
public string m_PlayerPrefab;

public void CreatePlayer()
{
   // Converts our path to a resources path and then loads the object.
   GameObject player = AssetPath.Load<GameObject>(m_PlayerPrefab);
}

The advantage of using this function is it's much cleaner and also in the Editor if the object could not be find in a Resources folder an attempt to load it using AssetDatabase.Load(string path). This is really useful for any editor windows that might be using AssetPathAttribute.

TODO:

  • Validate that the type sent in to AssetPath.Attributes constructor is serializable. Display a error string if it's not.

Meta

Handcrafted by Byron Mayne [twitter โ€ข github]

Released under the MIT License.

If you have any feedback or suggestions for AssetPathAttribute feel free to contact me.

assetpathattribute's People

Contributors

byronmayne avatar sov3rain avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.