• 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 GeroNL · Feb 23 at 04:09 AM · editoreditor-scripting

How to get size property like Vector 3?

How can i get the size or length of property like vector3?


my problem is when i do like this :

 do
 {
      SerializedProperty property = soTarget.FindProperty(Prop.name).Copy();
      // this was fine for float, string and etc that have 1 property
      // but when property.type is Vector3, in the next iteration it will be error 
      // cause can't finding x, y, z(Properties of Vector3)
 }
 while (Prop.NextVisible(true));

now i do manualy like these:


 private SIZE_OF_TYPE GetCountByProperty(SerializedProperty property)
 {
     if (property.propertyType == SerializedPropertyType.Vector3)
     {
         return SIZE_OF_TYPE.Vector3;
     }
     return SIZE_OF_TYPE.Default;
 }

  enum SIZE_OF_TYPE { 
     Default = 0,
     Vector3 = 3,
 }

but how to get it automatically (doesn't define one by one by self)?

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
0

Answer by Ermiq · Feb 24 at 10:26 AM

Try the Type.GetFields() and Type.GetProperties() methods. You'll need to use System and System.Reflections namespaces.

 Type myType = typeof(Vector3);
 FieldInfo[] fields = myType.GetFields();
 PropertyInfo[] properties = myType.GetProperties();
 int amountOfFieldsVector3 = fields.Length;
 int amountOfPropertiesVector3 = properties.Length;
 

Not sure if that will work though, because the Vector3 is not a class but a structure.
Info about GetFields(): https://docs.microsoft.com/dotnet/api/system.type.getfields?view=net-5.0

About GetProperties(): https://docs.microsoft.com/dotnet/api/system.type.getproperties?view=net-5.0#System_Type_GetProperties_System_Reflection_BindingFlags_

Comment
Add comment · Show 1 · 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 GeroNL · 2 days ago 0
Share

sorry, it can't, the value of both are wrong, does not like what i expect.

avatar image
0

Answer by VoidPhoenix96 · Feb 23 at 04:11 AM

 GameObject.Transform.LocalScale 

Should work.

Comment
Add comment · Show 4 · 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 GeroNL · Feb 23 at 04:30 AM 0
Share

thanks for for reply, but sorry, im not ask about scale, but maybe i asking with question that have multi meaning, cause i don not know properly about it too.


but what i mean was size / length of Vector3(what i mean about size/ length is when we make a property of vector 3, in the inspector will up with 3 property, there is x, y and z), not my gameobject scale.

avatar image VoidPhoenix96 GeroNL · Feb 23 at 09:48 PM 0
Share

So you want to make a Vector3  variable if I understand correctly, this should work: Public Vector3 variableName = new Vector3(0,0,0)

avatar image GeroNL VoidPhoenix96 · Feb 24 at 05:58 AM 0
Share

yes, i want to make it, but i want to make it in editor script not on monobehaviour class. and i want to make it by iteration, but the problem is i can not get the count of vector property.

Show more comments
avatar image
0

Answer by Namey5 · yesterday

There's no real correct way to do this, however one way I can think would be to count the actual bytes in the struct and compare them with the size of a single element;

 public static int NumberOfElements (System.Type a_ObjectType, int a_SizeOfOneElement)
 {
     //Use marshalling to get the size of an allocation for this type (Vector3 would be 3 * sizeof (float)
     int objectSize = System.Runtime.InteropServices.Marshal.SizeOf (a_ObjectType);
     //Divide by the size of a single element to get the number of elements
     return objectSize / a_SizeOfOneElement;
 }
 
 ...
 
 //The Vector classes are made up of floats, so pass that as the size of an element
 int elementCount = NumberOfElements (System.Type.GetType (property.type), sizeof (float));

Haven't tested this, but I can't think of any other way really ( @Ermiq has the most 'correct' method, but the Vector classes' fields don't necessarily map the way you might expect).

Comment
Add comment · Show 1 · 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 GeroNL · yesterday 0
Share

Hei, thanks for answering, but it still can not do with that, the values of objectSize is 32 for vector3, and for second parameter it manually input, it will be same way as i did in GetCountByProperty function with enum SIZE_OF_TYPE , but thanks for it.

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

164 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 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 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

Hide default editor Gizmo for move, rotete, scale 0 Answers

Event when exiting PolygonCollider2D edit mode 1 Answer

AssetPostprocessor: How to persist changes to a model 1 Answer

Custom Editor Access to Scene Objects Variables (in the same way as UnityEvents) 1 Answer

Is it possible to detect drag and drop in hierachy window? 0 Answers

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