I need to have dynamic class objects with 4 numeric values such as:
Object1.var1, Object1.var2 etc
The trick is I want the “Object1” to be dinamically created and deleted and also have its name to be dynamic. So I am looking for a way to create object from a given class at runtime like say a function :
CreateObject(name, var1, var2, var3, var4) and also have a way to destroy it. I need it to show in inspector the same way as if I declared it as public in code… Is it possible?
You can also do this with plain old C# objects, as long as they are [Serializable] and contain only serializable fields:
using System;
[Serializable]
public class MySerializableClass {
public string Name;
public int Var1;
public int Var2;
public float Var3;
public float Var4;
public MySerializableClass(string name, int var1, int var2, float var3, float var4) {
this.Name = name;
this.Var1 = var1;
this.Var2 = var2;
this.Var3 = var3;
this.Var4 = var4;
}
}
Now you can declare fields of this type in your MonoBehaviours and they will show in the inspector:
using UnityEngine;
public class MyTestMonoBehaviour : MonoBehaviour {
public MySerializableClass Test;
}
This is totally not what I wanted. I know we can define things like that, the problem is you need to do it in code like you did in your example with Test variable. I want this Test to be defined in game (including its name) and changed/deleted dynamically
I have a package called Datasacks that is generally for UI interoperability, but you can make Datasacks as much as you want, and use them as blobs of data in your game.
Here’s how it works:
Each one is just a ScriptableObject. The normal use is to make them in advance as assets, but you can make them on the fly.
Allowing editing them on the fly in the finished game would just be a trivial UI operation, since these things are in fact intended to interoperate UnityEngine.UI objects in the first place.