Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by alexanderameye · Dec 25, 2016 at 09:27 PM · arrayliststringenum

How do check which enum value was selected?

I have this enum in one script:

     public enum TypeOfRotation
         {
                     SingleRotation,
                     LoopedRotation,
         }
         public TypeOfRotation RotationType;

And then in another script I want set the value of elementNameProperty (a string) to be whatever was selected in the enum (SingleRotation or LoopedRotation). The following code doesn't work and I don't quite know how to achieve this.

 RotationTimeline.elementNameProperty = RotationTimeline.GetItem(0).FindPropertyRelative("TypeOfRotation").enumNames;
Comment
Add comment · Show 3
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 alexanderameye · Dec 25, 2016 at 09:27 PM 0
Share

$$anonymous$$erry Christmas to all by the way!

avatar image TBruce alexanderameye · Dec 25, 2016 at 10:44 PM 0
Share

$$anonymous$$erry Christmas to you @alexanderameye. Could you please show me what the script/structure RotationTimeline looks like?

avatar image alexanderameye TBruce · Dec 25, 2016 at 10:49 PM 0
Share

Sure!

I'll give you the 2 scripts I used:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using $$anonymous$$alee;
 
 public class Rotations : $$anonymous$$onoBehaviour {
 
     public List<RotationsChild> RotationTimeline;
   
       [System.Serializable]
     public class RotationsChild
     {
 
         public string SingleRotation = "Single";
         [HideInInspector]
         public string LoopedRotation;
 

         public enum TypeOfRotation
         {
                     SingleRotation,
                     LoopedRotation,
         }
         public TypeOfRotation RotationType;
 
         public float InitialAngle = 0f;
         public float FinalAngle = 90f;
         public float Speed = 4f;
 
     }
 
     class Container {
  
     [SerializeField]
     private RotationsChild[] RotationTimeline;
 }
 
     [System.Serializable]
     public class RotationsChildList : ReorderableArray<RotationsChild> {
     }
 }

and then script number 2

 using UnityEngine;
 using UnityEditor;
 using System.Collections;
 using $$anonymous$$alee.Editor;
 
 [CanEdit$$anonymous$$ultipleObjects]
 [CustomEditor(typeof(Rotations))]
 public class RotationsEditor : Editor
 {
 
     private ReorderableList RotationTimeline;
 
     void OnEnable()
     {
         RotationTimeline = new ReorderableList(serializedObject.FindProperty("RotationTimeline"));
     }
 
     public override void OnInspectorGUI()
     {
 
         //Ins$$anonymous$$d of checking the value of 'SingleRotation' here, I want it to check what element of the RotationType enum was selected
         RotationTimeline.elementNameProperty = RotationTimeline.GetItem(0).FindPropertyRelative("SingleRotation").stringValue;
 
         serializedObject.Update();
         RotationTimeline.DoLayoutList();
         serializedObject.Apply$$anonymous$$odifiedProperties();
     }
 }
 

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by bart1259 · Dec 26, 2016 at 05:23 PM

try RotationType.ToString();

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 TBruce · Dec 26, 2016 at 12:04 AM

Hi @alexanderameye, If I understand you correctly then you are looking for something like this

 public static T ParseEnum<T>(string value)
 {
     return (T) System.Enum.Parse(typeof(T), value, true);
 }

 public override void OnInspectorGUI()
 {
     SerializedProperty property = RotationTimeline.serializedProperty.GetArrayElementAtIndex(0).FindPropertyRelative("RotationType");
     Rotations.RotationsChild.TypeOfRotation rotationType = ParseEnum<Rotations.RotationsChild.TypeOfRotation>(property.stringValue);
     
     if (rotationType == Rotations.RotationsChild.TypeOfRotation.SingleRotation)
     {
     }
 
     //Instead of checking the value of 'SingleRotation' here, I want it to check what element of the RotationType enum was selected
     // RotationTimeline.elementNameProperty = RotationTimeline.GetItem(0).FindPropertyRelative("SingleRotation").stringValue;
 
     serializedObject.Update();
     RotationTimeline.DoLayoutList();
     serializedObject.ApplyModifiedProperties();
 }

Lines were getting too long to read, I added the function ParseEnum<>() which returns an enum value from a string. I also broke everything down into multiple lines and added the if comparison statement.

I do mot know what Malee and Malee.Editor are as I did not have the associated scripts, so for testing purposes I had to comment out the uses statements and any code associated. I also had to add the uses statement

 using UnityEditorInternal;

to use ReorderableList.

Comment
Add comment · Show 8 · 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 alexanderameye · Dec 26, 2016 at 08:28 AM 0
Share

$$anonymous$$alee is a class someone wrote and posted on the forums.

'It's an attempt to mimic functionality of the ReorderableList within Unity while adding some extended functionality.'

avatar image alexanderameye · Dec 26, 2016 at 08:47 AM 0
Share

The thing is, $$anonymous$$alee.Editor kind of conflicts with UnityEditorInternal, and doesn't contain definitions for GetArrayElementAtIndex etc. So it's kind of hard for you guys here to answer my question, I'll try to contact the original author of the $$anonymous$$alee scripts ins$$anonymous$$d, thank you for your effort though!

$$anonymous$$

avatar image TBruce alexanderameye · Dec 26, 2016 at 06:43 PM 0
Share

I figured that the $$anonymous$$alee/$$anonymous$$alee.Editor had something to do with a custom ReorderableList. Barring that, did this not work for you? It is not specific to any library.

avatar image alexanderameye TBruce · Dec 26, 2016 at 06:48 PM 0
Share

It did not :/ First it gave me the following error:

 Assets/ReorderableList/Example/Editor/RotationsEditor.cs(16,10): error CS0104: `ReorderableList' is an ambiguous reference between `$$anonymous$$alee.Editor.ReorderableList' and `UnityEditorInternal.ReorderableList'

What lead me too remove the UnityEditorInternal, but that gave me the next error

 Assets/ReorderableList/Example/Editor/RotationsEditor.cs(32,50): error CS1061: Type `$$anonymous$$alee.Editor.ReorderableList' does not contain a definition for `serializedProperty' and no extension method `serializedProperty' of type `$$anonymous$$alee.Editor.ReorderableList' could be found. Are you missing an assembly reference?
 


Show more comments
avatar image TBruce alexanderameye · Dec 31, 2016 at 05:55 AM 0
Share

Hi @alexanderameye,

Sorry for the long silence (holidays). I seem to have misunderstood what you were ai$$anonymous$$g for and am still not 100% sure what you are looking to do. But I have created the following three Unity packages

  1. Uses the UnityEditorInternal

  2. Uses the $$anonymous$$alee framework (I found $$anonymous$$alee in this discussion).

  3. Uses the $$anonymous$$alee framework - This $$anonymous$$alee version has a custom editor (The only way I could set the default values with this)

Note: I did not know what fields that you wanted to show so I have added the following items to be displayed (you can add or remove items easily from either of the versions)

  1. SingleRotation

  2. RotationType

  3. InitialAngle

  4. FinalAngle

  5. Speed

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

69 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

Related Questions

Cant figure out how to implement a inventory system 1 Answer

can't save values from another class to a list 1 Answer

I have trouble understanding arrays and enums. When and how? 0 Answers

Building tycoon game, need help storing variables into an array. 1 Answer

How to call all instances of a script in a list 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