- Home /
How to manually create property fields of a class
Hey all, here is my problem.
I have a class that I want to reference another base class say Class2 from which other classes will inherit. However I want to be able to dynamically add it to inspector without it being a MonoBehavior added to a GameObject. I want to take it from the project pane and assign it there.
If this isn't possible can I simply show the properties of a Type Class2? I could then save them to a text file and load them manually, while instantiating the class in game.
Thanks for reading.
Answer by VesuvianPrime · Feb 02, 2015 at 02:11 PM
I suspect you want your Class2 to be a ScriptableObject http://docs.unity3d.com/ScriptReference/ScriptableObject.html
@VesuvianPrime Hm, that's interesting. I didn't know that. I tried to use it but I failed. I'm not sure what I'm doing wrong.
I've made this code for the Editor script but it never creates an instance of the script. What I'm doing is assigning an object variable of the script and then checking its type and Creating an Instace from the ScriptableObject class.
Then again I may be doing something wrong. Haven't used ScriptableObjets ever.
public override void OnInspectorGUI()
{
DrawDefaultInspector();
if(class2 == null)
{
if(class2Object != null)
{
System.Type type = class2Object.GetType();
class2 = ScriptableObject.CreateInstance(type) as Class2;
}
}
}
$$anonymous$$y algorirthm was wrong. I used an Object as a variable in inspector where I should use a $$anonymous$$onoScript. Seems to work so far.
Answer by fafase · Feb 02, 2015 at 04:53 PM
Let' consider you have
[System.Serializable]
public class NonMono:ParentNonMono
{
[SerializeField]
private bool dataBool;
[SerializeField]
private string dataString;
}
then you can use:
public class Test:MonoBehaviour{
[SerializeField]
NonMono nonMono = null;
void Start(){
nonMono = new NonMono();
}
}
This will show the member of nonData.
This is for the second case if you don't want to use ScriptableObject
Can I declare in the Test the variable non$$anonymous$$ono as a ParentNon$$anonymous$$ono and assign a Non$$anonymous$$ono on it? I need to use inheritance to make my project work.
Yes you should. I suspect you need ot also set the parent as Serializable.