Hi guys,
I am currently learning about Extending the Editor in Unity and I’m curios as to how I might be able to create a drop down list that would allow me to add let’s say a string to a certain variable from another script. Then upon pressing Ok, the string is added.
Note that I work best with Javascript, but I was only able to extend the editor with C#.
Example:
There is the Example.js script with the following variable: var Text : String = “”;
Through the ExtendedWindow.cs script I would like to add the text in the text box(already managed this part) and select to which variable and/or script it should be added to.
Is this even possible?
I do not want you to write the code for me, just to explain it for a better understanding. I’m capable of figuring it out if I get the logic in it (just couldn’t find anything like this anywhere).
Regards,
Eugen
Given the following implementation of ExampleBehaviour
#pragma strict
var Text : String = '';
A custom editor can be defined using UnityScript as follows:
#pragma strict
// This is a custom editor for the example behaviour
@CustomEditor(ExampleBehaviour)
class ExampleBehaviourEditor extends Editor {
// We access the property using SerializedProperty to benefit from automatic
// undo/redo support. This approach generally incurs less hassle :-)
var textProp : SerializedProperty;
function OnEnable() {
// Find property from the object that is being edited
textProp = serializedObject.FindProperty("Text");
}
function OnInspectorGUI() {
// Update value of textProp from object
serializedObject.Update();
// Your custom GUI!
EditorGUILayout.TextArea(textProp.stringValue, GUILayout.Height(200));
// Persist any changes that have been made to textProp and
// add undo/redo steps when necessary
serializedObject.ApplyModifiedProperties();
}
}
For a detailed explanation of this please refer to the following documentation: