Custom Editor problems

I’m having problems getting types and functions working from javascript examples. can anyone help me with a C# version here is the code

[CustomEditor(typeof(Container))]
public class ContainerEditor : Editor  {
	
	public override void OnInspectorGUI (){
	EditorGUILayout.EnumMaskField ("droptype");
	}
}

this is the script its editing

public class Container :  MonoBehaviour, CanDrop

{

public GameObject element;
public float borderDimension;
public int viewcount;
public int count = 0;
public int maxsize;
public TransformToTransform moveMethod;
public bool initialized = false, CallOnAwake = false, quickscale = false, invert = false;
public DropType type;

GameObject placement;
Vector3 topleftcorner;
float elementw, elementl;
float width, length;
float elementx, elementz;
float offset = 0;
float maxoffset = 0;
int scrollcount = 0;

so essentially, I can’t figure out how to get DropType (enum) type into the custom editor. any help would be good.

you can use “target” to get the first object your editor is editing, and “targets” to get an array of all objects your editor is editing (if you want to support multi-editing). You will probably want to assign a variable with the correct type so you dont have to keep casting the target

Then you can do

Container targetContainer = (Container)target;
targetContainer.type = (DropType)EditorGUILayout.EnumMaskField("Label string", targetContainer.type);

It is generally better to do this using SerializedProperty and SerializedObject to benefit from automatic undo/redo and persistence:

[CustomEditor(typeof(Container))]
public class ContainerEditor : Editor {

    SerializedProperty typeProp;

    void OnEnable() {
        typeProp = serializedObject.FindProperty("type");
    }

    public override void OnInspectorGUI() {
        serializedObject.Update();
        
        typeProp.intValue = (int)(DropType)EditorGUILayout.EnumMaskField("Label string", (DropType)typeProp.intValue);
        
        serializedObject.ApplyModifiedProperties();
    }

}

Warning, the above has not been tested, but it should work :slight_smile: