Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
3
Question by Nemox · Feb 08, 2015 at 08:36 PM · editordatacustomattributemetadata

Custom attributes?

So I'd like to understand a bit about when to use attributes. I understand that they add metadata about objects. I have a node-based editor I'm making, and each node needs a name, description, and a position in the editor. This information is irrelevant in-game, so would I use an attribute to handle that data?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
15
Best Answer

Answer by sysameca · Feb 08, 2015 at 10:32 PM

Attributes are powerful way to provide meta information which can be associated with methods properties fields and etc and can be retrieved in runtime. This meta information is usually accessed through reflection and from there you can do whatever you want with that "marked" field, method, object and so forth. I will give you a short example because i feel sleepy.

Say you want to mark some fields and give the position meta information so later on you can work with it. First let's create the attribute:

 [AttributeUsage(AttributeTargets.Field)]
 public class PositionAttribute : Attribute
 {
     public readonly Vector2 position;
 
     public PositionAttribute(int x, int y)
     {
         position.x = x;
         position.y = y;
     }
 }

Now let's mark some script fields with that attribute:

 public class Node : MonoBehaviour
 {
     [Position(1, 0)]
     public object node;
 }
 

So now let's create a small custom inspector for the Node.

 using UnityEngine;
 using UnityEditor;
 using System.Reflection;
 using System;
 
 [CustomEditor(typeof(Node))]
 class NodeCustomEditor : Editor
 {
     private void OnEnable()
     {
         MonoBehaviour[] sceneActive = GameObject.FindObjectsOfType<MonoBehaviour>();
 
         foreach (MonoBehaviour mono in sceneActive)
         {
             Type monoType = mono.GetType();
 
             // Retreive the fields from the mono instance
             FieldInfo[] objectFields = monoType.GetFields(BindingFlags.Instance | BindingFlags.Public);
 
             // search all fields and find the attribute [Position]
             for (int i = 0; i < objectFields.Length; i++)
             {
                 PositionAttribute attribute = Attribute.GetCustomAttribute(objectFields[i], typeof(PositionAttribute)) as PositionAttribute;
 
                 // if we detect any attribute print out the data.
                 if (attribute != null)
                 {
                     Debug.Log(attribute.position.x); // The attribute position Y for that instance variable "1"
                     Debug.Log(attribute.position.y); // The attribute position X for that instance variable "0"
                     Debug.Log(mono.GetType()); // The type of the mono script "Node"
                     Debug.Log(objectFields[i].Name); // The name of the marked variable - "node"
                 }
             }
         }
     }
 }
 

As you see when you mark that field you get access to exclusive information which you can't get in languages that does not support reflection. From here it's up to you how you will use that data. I hope i brought some small light about the Attributes. These are extremely powerful feature and if you learn how to use them you have another tool in your box :)

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
avatar image
3

Answer by Alanisaac · Feb 08, 2015 at 09:38 PM

Regardless of whether its relevant in-game what you're describing sounds like the data of a node rather than metadata.

A field or property could be used to store each node's name, description, and position.

An attribute could be used, for example, to say that the name should not exceed 100 characters, or that the position field can't have negative x and y.

Those restrictions are not the data of each node but information about that data, or as you mentioned, "metadata". Does that make sense? See also http://unity3d.com/learn/tutorials/modules/intermediate/scripting/attributes

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
avatar image
1

Answer by Xepherys · Oct 01, 2021 at 01:53 PM

Hate to raise a dead post, but I came across this one a few weeks ago while working on something and ended up writing a brief blog post about how I was using custom attributes to make a custom inspector more user-friendly:

https://www.labyrintheer.com/2021/10/01/unity-custom-attributes-and-custom-editors/

Hope that might help someone.

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

22 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

Related Questions

Editor Window with settings and Asset Creation 1 Answer

Gibt es eine Lösung/Einstellung für: Hierarchy Custom / My Sort? 1 Answer

Custom Editor: show asset folders in ObjectField 2 Answers

MetaData class can't be loaded and failed to load window layout 1 Answer

Extending an editor control 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