I understand that I cannot access non-static variables with static functions.
So, how do I create a custom editor script with static functions (init, drawGizmo) and access non-static variables?
I need non-static variables for undo functionality.
Code is below, I want to use the variable “sliderValue” for the static drawGizmo function, but how?:
using UnityEditor;
using UnityEngine;
public class sliderTester2 : EditorWindow{
[SerializeField] float sliderValue = 1f;
GameObject selectedObject;
[MenuItem("Slider Tester/ Slider Test v2")]
static void Init() {
sliderTester2 myWindow = (sliderTester2)EditorWindow.GetWindow(typeof(sliderTester2));
myWindow.autoRepaintOnSceneChange = true;
}
void OnGUI(){
GUILayout.Label("Test Slider");
sliderValue = EditorGUILayout.Slider(sliderValue, .1f, 5f);
} // on GUI
[DrawGizmo (GizmoType.SelectedOrChild | GizmoType.NotSelected)]
static void RenderVisibleVertices (Transform obj, GizmoType gizmoType){
Handles.color = Color.red;
if(selectedObject) {
Handles.DrawWireDisc(selectedObject.transform.position, Vector3.up, sliderValue);
}
}
}
} // class definition
Ok, so I managed to get the window by calling:
sliderTester2 myWindow = (sliderTester2)EditorWindow.GetWindow(typeof(sliderTester2));
Handles.DrawWireDisc(myWindow.selectedObject.transform.position, Vector3.up, myWindow.sliderValue);
But this never allows me to close the window, since it is constantly grabbing it.
Any ideas how to do this properly?
Trying to destroy the window manually just throws errors; Is there something simple I’m missing?
not sure exactly what you are doing here, but you can access non-static variables from a static context, you just have to have a static reference to the non-static object. Below is an example of how you might do it, though I don;t know if it applies to what you want to do:
ALso, note that it is conventional to have a class name be Capitalized and an instance be lowercased… for clarity you should rename sliderTester2 to SliderTester2.
public class sliderTester2 : EditorWindow{
static sliderTester2 instance;
float sliderValue = 1f;
...
static void Init() {
instance = (sliderTester2)EditorWindow.GetWindow(typeof(sliderTester2));
instance.autoRepaintOnSceneChange = true;
//example of access an instance variabel from a static context:
Debug.Log("sliderValue is "+instance.sliderValue);
}
then you can access any variable of that instance as instance.variable from a static context. Look-up “singleton” for better examples
Thanks for the reply.
OK, I got it to work using this example, but it never seems to clear the instance. I draw a gizmo attached to the selected Object, and now when I close the sliderValue window, the gizmo persists…
Shouldn’t it be destroyed in OnDestroy()?
///EDIT
Ha, OK got it working by inserting :
if(instance){ do the gizmo code; }
pakfront Thanks!