- Home /
How to create custom inspector from my script (dropdown from list)
Hello people I have this script to create 2 lists getting the values from a text file. I use the first list to complete a dropdown options and later I use the dropdown to set the selected value into a 2 strings.
The idea always was make this work in the inspector, but I really dont know how to make it.
I added: [ExecuteInEditMode] with this, the lists are filled in edit mode. mi next step is show a dropdown menu in the inspector. with this im totally lossed. I tried with a enum, it create the dropdown but idk how get the list values and use it for enum options, I think not is possible?.
my script so far:
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
[ExecuteInEditMode] //disable this for excute only in play mode
class Dropdownfromlistv2 : MonoBehaviour
{
public List<string> ListCode;
public List<string> ListDesc;
public Dropdown DropdowntoComplete;
public string NombreObjetoFromList;
public string DescrObjetoFromList;
private void Awake()
{
ListCode.Clear();
ListDesc.Clear();
using var reader = new StreamReader(@"Assets\SomeFile.txt");
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(';');
ListCode.Add(values[0]);
ListDesc.Add(values[1]);
}
}
void Start()
{
DropdowntoComplete.options.Clear();
foreach (string t in ListCode)
{
DropdowntoComplete.options.Add(new Dropdown.OptionData() { text = t });
}
}
public void DropdownValueChanged(int value)
{
ListCode[value].ToString();
ListDesc[value].ToString();
NombreObjetoFromList = ListCode[value].ToString();
DescrObjetoFromList = ListDesc[value].ToString();
}
}
I tried some basic for create a custom inspector in the same script:
[CustomEditor(typeof(Dropdownfromlistv2))]
public class Newcustomdropdown : Editor
{
string[] _choices = new[] { "foo", "foobar" };
int _choiceIndex = 0;
public override void OnInspectorGUI()
{
// Draw the default inspector
DrawDefaultInspector();
if (_choiceIndex < 0)
_choiceIndex = 0;
_choiceIndex = EditorGUILayout.Popup(_choiceIndex, _choices);
}
}
this show me a dropdown, but eiter Idk how to asign the list values in _choices var
Can someone give me some guidance?, what is the correct/easy way to do this?.
Answer by Koyemsi · May 11, 2021 at 02:16 PM
Could this help : https://answers.unity.com/questions/1623675/enum-like-menu-to-display-list-elements.html ?
Hello, thanks for answer. with this I have the same situation, I dont know how to fill the list with the values from text file using the custom inspector. I use the [ExecuteInEditMode] to fill the list in a simple script (monobehaviour class) I think I cant mix [ExecuteInEditMode] with the custom inspector?. Im triying something like this:
[CustomEditor(typeof(StatList))]
public class StatListEditor : Editor
{
public List<string> ListCode;
//public List<string> ListDesc;
/*public List<string> statList = new List<string>()
{
"hp", "mp", "str", "lck", "dmg"
};*/
public int selectedIndex = 0;
public override void OnInspectorGUI()
{
serializedObject.Update();
EditorGUILayout.PrefixLabel("Attributes");
//selectedIndex = EditorGUILayout.Popup(selectedIndex, statList.ToArray());
selectedIndex = EditorGUILayout.Popup(selectedIndex, ListCode.ToArray());
serializedObject.ApplyModifiedProperties();
}
[ExecuteInEditMode] //disable this for excute only in play mode
private void Awake()
{
ListCode.Clear();
using var reader = new StreamReader(@"Assets\SomeFile.txt");
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(';');
ListCode.Add(values[0]);
}
}
}
with this, the StatList script not show the dropdown in inspector.
I'm not quite sure to understand the problem, and not sure to find the right words to describe it. But I think the problem could come from the fact that you refer to ListCode the same way in both scripts (the monbehaviour and the editor script). But looking back to your code it seems that there is only one script (the editor one), whereas I first thought that what was co$$anonymous$$g after [ExecuteInEditMode] was part of your Monobehaviour script. If this is right, you don't need this [EIEM] as editor scripts always exec in edit mode. But if you have 2 separate scripts, and if your list is defined in the monobehavior script, I think you should make a reference to it with a SerializedProperty in your editor script. Hope this is not too confused ;)
hi again. sorry for my previous post. I couldn't find a way to explain my scripting problems and what I wanted to do I had a lot of syntax errors.
I rewrite the code and now is almost working as I want. actual code:
[CustomEditor(typeof(InspectorDropdownV2))]
public class CustomEditorDropdownV2 : Editor
{
public int selectedIndex = 0;
public SerializedProperty Code;
public SerializedProperty Desc;
string code = "";
string desc = "";
void OnEnable()
{
Code = serializedObject.FindProperty("Código");
Desc = serializedObject.FindProperty("Descripción");
}
public override void OnInspectorGUI()
{
serializedObject.Update();
List<string> ListCode = new List<string>();
List<string> ListDesc = new List<string>();
using var reader = new StreamReader(@"Assets\SomeFile.txt");
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(';');
ListCode.Add(values[0]);
ListDesc.Add(values[1]);
}
EditorGUILayout.PrefixLabel("Dropdown Code selection:");
selectedIndex = EditorGUILayout.Popup(selectedIndex, ListCode.ToArray());
//selectedIndex = EditorGUILayout.Popup(selectedIndex, ListDesc.ToArray()); //disabled because I dont want to show a dropdown with the description
if (GUILayout.Button("Set strings"))
{
code = ListCode[selectedIndex].ToString();
desc = ListDesc[selectedIndex].ToString();
Debug.Log("this works?");
}
EditorGUILayout.PrefixLabel("String Code:");
code = EditorGUILayout.TextField(code);
EditorGUILayout.PrefixLabel("String Desc:");
desc = EditorGUILayout.TextField(desc);
serializedObject.ApplyModifiedProperties();
}
}
the script of above is for the custom inspector. and have this one:
public class InspectorDropdownV2 : MonoBehaviour
{
public string Código;
public string Descripción;
}
just to show/set the strings with the values of the lists
one thing I need is a autocomplete dropdown (I mean I write some text and show me the closest results from the inspector dropdown) can you give me some guidance how approach it?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
how to keep track of Lean Tween function? 0 Answers
How can i check and fire an event when the user look at specific object ? 0 Answers
Make event stop 3 Answers