Quantcast
Viewing all articles
Browse latest Browse all 78

How should I declare a nested class to find it latter with SerializedObject.FindProperty?

Hello. I'm trying to imitate what was explained on the [Unite 11 Intro to Editor Scripting][1] on a ScriptableObject class. The class contain an array of another "class of the class". Example : public class A : ScriptableObject { [System.Serializable] public class B { public B() { } Rect rectangle; Vector2 position; } public A() { myArray = new B[1]; myArray[0] = new B(); } private B[] myArray; } With this class, the serialization works like a charm. Now, I have an EditorWindow wich I use to populate my A's with some B's. For that, following the Unite 11 video, I use a SerializedObject and access its properties with SerializedObject.FindProperty(), like that : public class AEditor : EditorWindow { private SerializedObject myObject; public void SetA(A a) { myObject = new SerializedObject(a); } public void OnGUI() { if(myObject == null) return; myObject.Update(); A.B[] bs = GetBs(); // Do stuff myObject.ApplyModifiedProperties(); } private A.B[] GetBs() { int arraySize = myObject.FindProperty("myArray.Array.size").intValue; A.B[] bs = new A.B[arraySize]; for(int i = 0; i < arraySize; i++) { Object o = myObject.FindProperty(string.Format ("myArray.Array.data[{0}]", i)).objectReferenceValue; if(o == null) { Debug.LogError ("Object not found in array."); continue; } A.B b = o as A.B; if(b == null) { Debug.LogError("B object not loaded"); continue; } bs[i] = b; } return bs; } } With this, The script won't compile because of line 23 "A.B b = o as A.B;", I have this : > error CS0039: Cannot convert type 'UnityEngine.Object' to 'A.B' via a built-in conversion So, I made B inherit from System.Object : public class A : SerializedObject { public class B : UnityEngine.Object { ... } ... } Here, FindProperty find my Bs as UnityEngine.Object, but when I try to cast them, I got a null. > B object not loaded Here is my question : how should I declare my class B so it can be found by FindProperty() AND be castable afterward? [1]: http://video.unity3d.com/video/3699926/unite-11-intro-to-editor

Viewing all articles
Browse latest Browse all 78

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>