How to make a SerializedProperty with a list of values?
Hi
I am trying to make a custom editor with a slider, that only shows specific values. e.g.: 5, 7,8,11,321,512.
From https://docs.unity3d.com/ScriptReference/EditorGUILayout.Slider.html it looks like that
public static void Slider(SerializedProperty property, float leftValue, float rightValue, params GUILayoutOption[] options);
is the function to use. But I do not find out how to make the SerializedProperty.
Do not manege to save the topics: Editor, Slider, SerializedProperty
Answer by dan_wipf · Apr 10, 2019 at 02:12 PM
If you want to have a custom Slider, which shows an Array of integer Values (5, 7,8,11,321,512) you need to do something like this:
Editor Script
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(Value))]
public class ValueEditor : Editor
{
SerializedProperty intRange;
float select = 0;
public override void OnInspectorGUI()
{
intRange = serializedObject.FindProperty("intRange");
select = CustomIntSlider(intRange,select);
}
float CustomIntSlider(SerializedProperty property,float selected){
EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField("My Slider",GUILayout.Width(100));
selected = GUILayout.HorizontalSlider(Mathf.Round(selected),0,property.arraySize-1);
EditorGUILayout.IntField(property.GetArrayElementAtIndex((int)selected).intValue,GUILayout.Width(50));
EditorGUILayout.EndHorizontal();
return selected;
}
}
Monobehaviour
using System.Collections.Generic;
using UnityEngine;
[UnityEditor.CanEditMultipleObjects]
public class Value : MonoBehaviour
{
public List<int> intRange = new List<int>(){5,7,8,11,321,512};
}
Perfect!
It relay did what I was hoping for and thank you wherry much. :) But unsure if I should change the question since you did not use EditorGUILayout.Slider.
Here is the function I did end up using, simplify it since the slider was already in a begin/endHorizontal with a label. The source is the keys from a dictionary.
int PropertyValueSlider(int[] values, int selectedIndex)
{
selectedIndex = (int)$$anonymous$$athf.Round(GUILayout.HorizontalSlider(selectedIndex, 0, values.Length - 1));
EditorGUILayout.IntField(values[selectedIndex], GUILayout.Width(40));
return selectedIndex;
}