Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by Aggressor · Apr 22, 2015 at 04:58 AM · c#variablestypeserializedproperty

What does a [SomeName] Above A Variable Mean?

I know there is [System.Serializable] put above things that are meant to show up in the inspector.

But I find a lot of plugins I use have things like this

 [PrimaryKey] 
 public int StarShipID { get; set; }

What is the name of the thing [Whatever] called? How does one make these and define what it does?

Comment
Add comment · Show 4
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 Priyanshu · Apr 22, 2015 at 05:25 AM 1
Share

They are called Attributes Unity Tutorial: Attributes

avatar image fafase · Apr 22, 2015 at 05:31 AM 1
Share

They somehow add quality or ...attribute to a variable or class or else. They are meta data, meaning you cannot modify them as they are not part of the runtime process and belong to the manifest.

It is like the postcode of a building, you can change the building, the shape of it, who goes in and other things but an attribute would define the postcoe and you cannot modify that, only can you access it and use.

Attributes will also define the variable/class just like the postcode would define if the building is in East side, $$anonymous$$anhattan or the Bronx.

Whether you want to modify the attribute you have to rebuild the app.

avatar image HarshadK · Apr 22, 2015 at 05:36 AM 1
Share

There's a nice tutorial series on Attributes and Reflection in C# by Jamie $$anonymous$$ing: C# Hello World Attributes if you want to know how they work.

avatar image Aggressor · Apr 22, 2015 at 05:45 AM 0
Share

Hey thanks guys, this is really going to help me level up my coding with Unity. XD

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Flavioli · Apr 22, 2015 at 09:45 AM

As others mentioned, those are attributes. They allow you to provide more data or functionality to classes, methods, properties, fields, etc.. Actually, if you make a custom attribute, you'll see that there isn't too much magic behind how they behave; here is a small (albeit a bit silly) example of creating a custom attribute and then using it in your own code.

Declare the silly attribute:

 public class MyValueAttribute : System.Attribute 
 {
     public int SomeValue = 0;
     public MyValueAttribute(int value)
     {
         this.SomeValue = value;
     }
 }


To apply it to a class (note you do not need to call it by its full name when used in square brackets, though you can actually do so if you want):

 [MyValue(5)]
 public class Joystick : MonoBehaviour
 {
    // ... //


You can employ Reflection to actually make "use" of this silly attribute (using the word "use" loosely):

 object[] attributes = typeof(Joystick).GetCustomAttributes(typeof(MyValueAttribute), false);
 if (attributes.Length > 0) 
 {
     MyValueAttribute myAttribute = attributes[0] as MyValueAttribute;
     System.Diagnostics.Debug.Assert(myAttribute.SomeValue == 5);
 }



As you might imagine, you can probably find a more practical way to use attributes, though I find that they are more interesting and applicable when designing frameworks than in everyday code. One of my favorite uses for attributes is when applied to enum values, which can be quite nifty in many cases.

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
0

Answer by Thaer.M · Apr 22, 2015 at 10:01 AM

see this link maybe help you

https://github.com/Anaryu/aetherion/blob/master/Assets/SimpleSQL/Demos/Scripts/SciFi%20Data/StarShip.cs

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

23 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Problem with var in C# (Vector3, transform and similiar) 1 Answer

Type.GetType(string) does not work in Unity 2 Answers

Variable declaration. Public vs indirect public? 3 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