- Home /
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()
{
}
}
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.
Highlight your code
Press the 101 010 Button in the edit toolbar
done
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?
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
Follow this Question
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