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 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.
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?