Answer the question
In order to leave comments, you need to log in
Editor script not working correctly in unity, how to solve?
I need to display a list of objects that contain a sheet of strings. When creating a new object in the editor, it is displayed incorrectly, but if the code is updated, then everything is correct.
editor script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using UnityEditor.SceneManagement;
[CustomEditor(typeof(BundlesManager))]
public class BMEditor : Editor
{
private BundlesManager bm;
public void OnEnable(){
bm = (BundlesManager)target;
}
public override void OnInspectorGUI(){
EditorGUILayout.BeginVertical();
if (bm.bundles.Count > 0)
foreach (Bundle bundle in bm.bundles) {
EditorGUILayout.BeginVertical("box");
bundle.name = EditorGUILayout.TextField ("Bundle name", bundle.name);
if (GUILayout.Button("Remove bundle", GUILayout.Width(110), GUILayout.Height(20)))//удаление объекта
{
bm.bundles.Remove(bundle);
break;
}
if (bundle.engDictionary.Count <= 0)//если нет слов, создать
{
bundle.engDictionary.Add("");
bundle.kazDictionary.Add("");
bundle.rusDictionary.Add("");
}
EditorGUILayout.PrefixLabel(bundle.name + " : " + bundle.engDictionary.Count);// отображение кол-ва слов в объекте
bundle.show = EditorGUILayout.Foldout(bundle.show, "Слова");//выпадающий список
if (bundle.show)
{
GUILayout.BeginVertical("box");
if (bundle.engDictionary.Count > 0)
for (int i = 0; i <= bundle.engDictionary.Count; i++)
{
EditorGUILayout.BeginHorizontal();
bundle.engDictionary[i] = GUILayout.TextField(bundle.engDictionary[i], GUILayout.Width(75));
bundle.kazDictionary[i] = GUILayout.TextField(bundle.kazDictionary[i], GUILayout.Width(75));
bundle.rusDictionary[i] = GUILayout.TextField(bundle.rusDictionary[i], GUILayout.Width(75));
if (GUILayout.Button("X", GUILayout.Width(20), GUILayout.Height(15), GUILayout.Height(15)))
{
bundle.engDictionary.RemoveAt(i);
bundle.kazDictionary.RemoveAt(i);
bundle.rusDictionary.RemoveAt(i);
break;
}
EditorGUILayout.EndHorizontal();
}
GUILayout.EndVertical();
}
if (GUILayout.Button("+", GUILayout.Height(20), GUILayout.Width(20)))
{
bundle.engDictionary.Add("");
bundle.kazDictionary.Add("");
bundle.rusDictionary.Add("");
}
EditorGUILayout.EndVertical();
}
else EditorGUILayout.LabelField("No bundles in memory");
if (GUILayout.Button ("Add bundle"))
{
bm.bundles.Add(new Bundle());
}
EditorGUILayout.EndVertical();
if (GUI.changed) SetObjectDirty(bm.gameObject);//Если были изменения, сделать сцену грязной (для сохранения)
}
static void SetObjectDirty(GameObject obj)
{
EditorUtility.SetDirty(obj);
EditorSceneManager.MarkSceneDirty(obj.scene);
}
}
public class BundlesManager : MonoBehaviour
{
public List<Bundle> bundles;
}
[System.Serializable]
public class Bundle
{
public string name;
public List<string> engDictionary;
public List<string> kazDictionary;
public List<string> rusDictionary;
public bool enable; // активен ли бандл
public bool show;// раскрыть список слов
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question