- Home /
Question by
Mars113 · Mar 05, 2017 at 11:21 AM ·
editorserializefield
[Solved] to assign a serialize-field property in other class via editor class
Hello guys, here's my situation.
Here's a class to storage some data.
[Serializable]
public class MyData
{
[SerializeField]
string str;
[SerializeField]
List<string> list;
}
public class MainClass : ScriptableObject
{
[SerializeField]
MyData data;
}
And then I'd like to change assign the value via editor.
[CustomEditor(typeof(MainClass))]
[CanEditMultipleObjects]
public class MainClassEditor : Editor
{
MainClass source;
SerializedProperty data;
void OnEnable()
{
source = target as StoryScript;
data = serializedObject.FindProperty("data");
}
public override void OnInspectorGUI()
{
serializedObject.ApplyModifiedProperties();
EditorGUILayout.PropertyField(data, true);
}
}
I could assign the data class value from inspector panel, it works fine.
But I have no idea how to assign the values via script in editor class.
Is here any way could do that?
Comment
Answer by Mars113 · Mar 05, 2017 at 01:47 PM
Solved
var data = serializedObject.FindProperty("data");
data.FindPropertyRelative("str").stringValue = "Test";
Your answer
![](https://koobas.hobune.stream/wayback/20220612112505im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
[SerializeField] Not Working Properly? 1 Answer
SerializeField does not work when a value set using code 0 Answers
How to get a drop-down list of options defined by another object in editor? 0 Answers
Access serialized field in custom editor 2 Answers
C#6 Serializefield is not working with getter and setter 1 Answer