- Home /
Issue with EditorGUILayout.PropertyFeild
I am having trouble with List and the PropertyField on my custom editor inspector, I am using the following code which displays the list but i am unable to access it from the inspector in the unity editor, it is greyed out.
[System.Serializable]
public class Window
{
[SerializeField] public float Win_Width;
[SerializeField] public float Win_Height;
[SerializeField] public float Win_Depth;
[SerializeField] public float Win_FrameSize;
[SerializeField] public int HS;
[SerializeField] public int WS;
[SerializeField] public bool MultiSectionWidnow;
[SerializeField] public Material GlassMaterial;
[SerializeField] public GameObject Parent;
}
[CustomEditor(typeof(CreateWallScript), true)]
public class CreateWallEditor : Editor
{
GameObject Parent;
public List<Window> Win = new List<Window>();
public override void OnInspectorGUI()
{
CreateWallScript CW = (CreateWallScript)target;
ScriptableObject t = this;
SerializedObject so = new SerializedObject(t);
SerializedProperty stringsProperty = so.FindProperty("Win");
so.Update();
EditorGUILayout.PropertyField(stringsProperty,new GUIContent("Window Setup:"), true);
so.ApplyModifiedProperties();
Parent = CW.gameObject;
if (GUILayout.Button("Create"))
{
CW.Clear();
CW.Create(Parent, 3, 5, 0.1f, false, false, 0);
}
}
}
This is what is displayed,
Did you ever happen to solve this? I'm facing similar issues.
It is showing up as read-only because he tried to serialize CreateWallEditor : Editor
, an editor window class (which is not even serializable), by mistake. The correct thing to do is to work with CreateWallScript
component which is serializable just fine.
Answer by andrew-lukasik · Sep 05, 2021 at 09:38 AM
These two lines are at fault here:
ScriptableObject t = this;
SerializedObject so = new SerializedObject(t);
Correct code:
SerializedObject so = this.serializedObject;
CreateWallScript.cs
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class CreateWallScript : MonoBehaviour
{
public List<Window> Win = new List<Window>();
public void DoSomething () => Debug.Log("button works");
}
[System.Serializable]
public class Window
{
public float Width, Height, Depth;
public Material GlassMaterial;
public GameObject Parent;
}
#if UNITY_EDITOR
[CustomEditor( typeof(CreateWallScript) , editorForChildClasses:true )]
public class CreateWallEditor : Editor
{
public override void OnInspectorGUI ()
{
var component = (CreateWallScript) target;
EditorGUI.BeginChangeCheck();
{
EditorGUILayout.PropertyField( serializedObject.FindProperty(nameof(CreateWallScript.Win)) , new GUIContent("Window Setup:") , includeChildren:true );
if( GUILayout.Button("Create") ) component.DoSomething();
}
if( EditorGUI.EndChangeCheck() )
serializedObject.ApplyModifiedProperties();
}
}
#endif
Your answer
Follow this Question
Related Questions
Check the material in script [C#] 1 Answer
Convert List of Floats to Colors (Color Map) 2 Answers
Why wont my script detect the input of my controller? 1 Answer
Script Not Working On Prefab 2 Answers
Grab and Drop Script goes through walls. 0 Answers