Hello all. I’m looking on how I can declare a public string in c# and it will offer a drop down list within the inspector. What can I google to look to see how it’s done?
A drop down list, do you mean a set of choice among a list?
You may want to use an enum then:
public enum State{
walking,idle,attacking,eating,sleeping
}
public class AClass:MonoBehaviour{
public State current;
}
This will show the variable with a list of possible choices
Here is a simpler way - Multiple Drop Down Selection
Simple 4 Steps
Step 1 : Make a new Script “EnumFlagsAttribute”
using UnityEngine;
using System.Collections;
public class EnumFlagsAttribute : PropertyAttribute
{
public EnumFlagsAttribute() { }
}
Step 2 : Make another Script “EnumFlagsAttributeDrawer”
using UnityEngine;
using System.Collections;
using System;
using UnityEditor;
[CustomPropertyDrawer(typeof(EnumFlagsAttribute))]
public class EnumFlagsAttributeDrawer : PropertyDrawer
{
public override void OnGUI(Rect _position, SerializedProperty _property, GUIContent _label)
{
_property.intValue = EditorGUI.MaskField( _position, _label, _property.intValue, _property.enumNames );
}
}
Step 3: Enum Declaration
[System.Flags]
public enum FurnitureType
{
None , SofaPrefab , Curtains , Table , Chairs
}
[EnumFlagsAttribute]
public FurnitureType enumType;
Step 4: Getting Selected Elements
List<int> ReturnSelectedElements()
{
List<int> selectedElements = new List<int>();
for (int i = 0; i < System.Enum.GetValues(typeof(FurnitureType)).Length; i++)
{
int layer = 1 << i;
if (((int) enumType & layer) != 0)
{
selectedElements.Add(i);
}
}
return selectedElements;
}
There is another short trick for this
public enum State{
walking,idle,attacking,eating,sleeping
}
public class AClass:MonoBehaviour{
public static string[] names = {"walking","idle","attacking","eating","sleeping"}
public State current;
public string getCurrentState(){
return names[(int)current];
}
}
,