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