Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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
0
Question by DJCrashFest · Nov 05, 2018 at 05:53 PM · inspectorstruct

Help with Structs and Custom Inspector

Currently trying to build a custom inspector, and I've hit a bit of a wall. Haven't had much experience with Unity, so bear with me if this seems easily obvious.

I have a given struct:

     public AttackProfileInfo[] attackProfiles;
      
     [Serializable]
     public struct AttackProfileInfo
     {
         public float animationDuration;
         public HitProfile hitProfile;
         public float initialDamage;
     }
 

And I am not sure how to get the relevant information to display in a custom inspector. I tried using SerializedObject/SerializedProperty with no success.

     SerializedObject profiles;
     SerializedProperty animationDuration;
      
     profiles = new SerializedObject(PlayableCharacters.AttackProfileInfo);
     animationDuration = profiles.FindProperty("animationDuration");
     EditorGUILayout.PropertyField(animationDuration);
 

new SerializedObject isn't working. "PlayableCharacters.AttackProfileInfo is a type, which is not valid in the given context".

Any guidance on where I'm going wrong here would be muchly appreciated. Thanks in advance.

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

2 Replies

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

Answer by DJCrashFest · Nov 21, 2018 at 06:14 PM

After some time, I have figured out the answer I was looking for. Turns out making this is an array added a bit more of the complexity.

First step, is to search out the original variable name, so in this case:

 var attackProfileArray = serializedObject.FindProperty("attackProfiles");

From there, I set a simple indexer variable for looping:

 int indexer = 0;

Then you can use a combination of GetArrayElementAtIndex with FindPropertyRelative, which gets the field in the struct, so:

 hitProfileArray.GetArrayElementAtIndex(indexer).FindPropertyRelative("animationDuration").floatValue = EditorGUILayout.FloatField("Animation Duration", PlayableCharacters.attackProfiles[indexer].animationDuration);
 hitProfileArray.GetArrayElementAtIndex(indexer).FindPropertyRelative("hitProfile").objectReferenceValue = (HitProfile)EditorGUILayout.ObjectField("HitProfile", PlayableCharacters.attackProfiles[indexer].hitProfile, typeof(HitProfile), true);

Which is all put into a loop, so it can be iterated. The loop is surrounded with BeginChangeCheck() and EndChangeCheck()).

If changes are detected with EndChangeCheck(), then serializedObject.ApplyModifiedProperties() is called.

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 jellevda · Sep 30, 2020 at 08:46 AM 0
Share

Thanks, been looking for a solution to this for ages!

avatar image
0

Answer by hectorux · Nov 05, 2018 at 08:18 PM

Try to use a class instead of a struct, in Unity i have never used struct, also to make a custom inspector you need a script with the editorBehviour and set which script you want to set custom.

Comment
Add comment · Show 2 · 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 DJCrashFest · Nov 06, 2018 at 04:47 PM 0
Share

Thanks for the reply. Unfortunately, I cannot change how the structs have been created. This is also only a sample of the code since this is part of a larger editor script.

avatar image hectorux DJCrashFest · Nov 06, 2018 at 05:17 PM 0
Share

Usually, with this kind of things i make a for an inside it putfor every custom variable inside the structure i made Edit: Ex: for(int i=0;i

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

98 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

Related Questions

custom struct not showing up in inspector? 2 Answers

Help with a custom inspector field. 0 Answers

Fields using Struct not showing up in Inspector. 1 Answer

Change variable in inspector depending on enum using struct 0 Answers

How to serialize an array of classes 1 Answer


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