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 liquid0257 · Apr 22, 2012 at 10:03 AM · switchenum

Enum Method vs Switch

Hi all,

Some help would be great. I had this as a switch statement before but was not getting the desired effect. I want when I change the widget type in the Inspector the Morph Targets Size to update. Some help would be great. Currently, I get an error CS1519 at line 22 and parsing error at line 26. Please go easy on me as I'm still learning C#

Thanx

 using UnityEngine;
 using System;
 
 [System.Serializable]
 public class MorphProp // properties per morph  
 {
     public string morphName = ""; // slider name
     public Mesh targetMesh; // additive target
     public float blendWeight = 0; // slider weight
 }
 
 [System.Serializable]
 public class ParadigmList // list widgets containing morphs
 {
     public string widgetName = ""; // group of sliders name
     public MorphProp[] morphTargets;
     public enum Paradigms {
         None {
             public void doSomething (){
                 morphTargets = new MorphProp[0];
             }
         },
         Linear {
             public void doSomething(){
                 morphTargets = new MorphProp[1];
             }
         },
         Bilinear {
             public void doSomething() {
                 morphTargets = new MorphProp[2];
             }
         },
         Triangular {
             public void doSomething() {
                 morphTargets = new MorphProp[3];
             }
         },
         Parametric_Axis {
             public void doSomething() {
                 morphTargets = new MorphProp[4];
             }
         },
         Parametric_Corners {
             public void doSomething() {
                 morphTargets = new MorphProp[4];
             }
         }
         
         public abstract void doSomething();
     };
     public Paradigms widgetType = Paradigms.None; // default to none 
 }
 
 public class MorphOperator : MonoBehaviour
 {
     public string clusterName = ""; // name affected points baked in art
     public Mesh baseMesh; // 
     public ParadigmList[] morphList;
 
     void Start()
     {
     }
         
     void Update()
     {
     }
 }
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 fafase · Apr 22, 2012 at 10:04 AM 2
Share

You need to properly write your code, it is simply impossible to read. Highlight the code section and click 101010. Also, at first glance, you gave the whole script, stick to the relevant part only.

avatar image Bunny83 · Apr 22, 2012 at 11:28 AM 0
Share

@liquid0257:

  1. Highlight your code

  2. Press the 101 010 Button in the edit toolbar

  3. done

avatar image Kleptomaniac · Apr 22, 2012 at 11:43 AM 0
Share

It's literally that simple ...

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Apr 22, 2012 at 12:04 PM

You can't so it this way. Enums are just a collection of named, constant integer values. They can't contain functions / function-pointers or any other types.

In C# you're able to create a property which has a get and set method called when ever you read or write from / to the property. Unfortunately such properties can't be serialized be Unity and therefore aren't visible in the default inspector.

You can create a custom editor for your class that handles this.

 public enum Paradigms
 {
    None, Linear, Bilinear, Triangular, Parametric_Axis, Parametric_Corners
 }
 [SerializeField]
 private Paradigms m_WidgetType = Paradigms.None;
 
 // This is a property
 public Paradigms WidgetType
 {
     get { return m_WidgetType; }
     set
     {
         if (m_WidgetType != value)  // Only do the switch when a different value has been assigned
         switch(m_WidgetType)
         {
         case Paradigms.None:
            morphTargets = new MorphProp[0];
            break;
         case Paradigms.Linear:
            morphTargets = new MorphProp[1];
            break;
         
         //[...]
         }
         m_WidgetType = value;
     }
 }

Now you have to create an Editor for your class and building your own inspector GUI. You might want to use the EditorGUILayout.EnumPopup to show the usual enum popup in the inspector.

 //[...]
 target.WidgetType = (Paradigms)EditorGUILayout.EnumPopup("WidgetType", target.WidgetType);
 //[...]

Now when you change the EnumPopup in the inspector the WidgetType property of your selected script will get it's set-method called with the new selected value. The property will automatically create the corresponding morphTargets size.

If you want to trigger the creation manually (i don't see a reason for that), you just have to put the switch in your doSomething method and remove the abstract.

Abstract methods will force the whole class to be abstract so you can't create an instance of this class, only a subclass that implements the method. Inheritance is a pain in Unity when it comes to serialization. You have to derive your class from ScriptableObject in this case.

However, I'm still not sure what exactly you want to achieve. Should those variables only be edited / changed in the editor or also at runtime?

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 liquid0257 · Apr 22, 2012 at 09:38 PM 0
Share

Hi Bunny83,

Thanx for this. Next I was gonna resort to making it all custom using "using UnityEditor;". So this helps me to confirm I should lean to that method.

The variables do not need to be changed at runtime, just in the editor. Gonna try the above out.

Cheers

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Optimising if else statements to switch and enums, or are there better methods? 1 Answer

Problem with my weapons switch statement C# 3 Answers

Cycling through enum with key press 1 Answer

Changing enum values 1 Answer

Enum switch statement error 2 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