Use of ScriptableObject Unity - unity3d

Yesterday I've found out about Scriptable Objects in Unity.The first thing that came to my mind is : hey , this is really simillar to inheritance.Let's say I have this:
[CreateAssetMenu(menuName = "Scriptableobjects/enemy_data")]
public class enemydata : ScriptableObject
{
public float HP;
public float mana;
public float damage;
}
If I have three enemies A , B , C i will just create 3 instances of enemy_data in my project assets , and complete HP , mana,damage individually.On each of my enemy monobehaviour i'll say :
public enemydata data;
And I'll drag the instances from project assets in their inspector.This is what I understood about scriptable objects from the tutorials I have seen.But , what if i did this:
public class enemydata
{
public float HP;
public float mana;
public float damage;
}
And just inherit this class?Wouldn't this be the same thing?
public class enemy1:MonoBehaviour , enemydata
{
void print()
{
Debug.Log(this.HP + " " this.mana + " " + this.damage);
}
I know I am missing something so please correct me.Thanks!

It is exactly the same thing, yes.
BUT ScriptableObjects are Assets and therefore you can
reference it at multiple places (re-use it)
you can have multiple instances but with different values => easily exchange e.g. settings without having to recompile your code
you can have a base class and inherit different types of ScriptableObjects and still reference them via the Inspector
This last point can be used to e.g. implement exchangeable behaviour. A bit like an interface but you can exchange the actual method implementation without having to recompile so it can even happen on runtime.
This is a huge advantage against the basic [Serializable] public class EnemyData { ... } for which you would already in code have to define which type to use exactly.
This makes ScriptableObjects quite powerful and they have a lot of usecases. (See e.g. How to pass data between scenes in Unity)
Most Unity configurations are based on ScriptableObject e.g. the AnimatorController.
Your last example
public class enemy1:MonoBehaviour , enemydata
{
void print()
{
Debug.Log(this.HP + " " this.mana + " " + this.damage);
}
}
makes no sense. You can only derive from one class so either MonoBehaviour or enemyData!
If something it would need to be a field like
public class enemy1:MonoBehaviour
{
[SerializeField] private enemydata data;
void print()
{
Debug.Log(data.HP + " " data.mana + " " + data.damage);
}
}

You can not inherit from 2 classes and only ScriptableObject assets are draggable in the inspector.
The main use for SO is to easily share the same values/data across different components through simple drag and drop. For example your player have a component that adds/removed hit points from PlayerHealth SO. Then you can have on some completely unrelated place LevelReseter component, you just drag PlayerHealth asset and monitor when it reaches zero to reset the level. It should be noted that ScriptableObjects does not serialize their modified values when closing the game, i.e. they can not be directly used to save data across different app runs.
BTW another use for ScriptableObjects is to achieve Strategy Pattern through inspector drag and drop, i.e. swapping algorithms by swapping ScriptableObject assets implementing the same interface (or even better inheriting from the same base class).

Related

Public variables created in Rider do not show up in Unity

I just started using Unity (which came with VSC), but I had better experience using JetBarin products such as IntelliJ IDEA, so I went and switched to Rider. However, I am now unable to connect public variables (int, float, GameObject) to my Unity projects.
I tried updating Rider and changing some setting, but came none the wiser.
UPDATE: There have been (obvious) request for my code to see the exact issue, so I hope this helps clear up the issue a little bit:
Code written in VSC
The resulting public variables showing up in Unity
Similar code written using Rider
No interactive variables showing up in Unity
Unity serializes only fields (not properties with get or set) of MonoBehaviours. All public fields are serialized unless have [System.NonSerialized] attribute.
DO NOT get confused with the [HideInInspector] attribute, it won't be visible in the inspector (if you don't have a custom inspector) but WILL BE serialized.
class Foo
{
// Bar won't be shown in the inspector or serialized.
[System.NonSerialized]
public int Bar = 5;
}
To serialize a non-public field use [SerializeField] attribute for primitive types (such as int, float, bool).
public class SomePerson : MonoBehaviour
{
// This field gets serialized because it is public.
public string name = "John";
// This field does not get serialized because it is private.
private int age = 40;
// This field gets serialized even though it is private
// because it has the SerializeField attribute applied.
[SerializeField]
private bool isMale = true;
}
If you wanna serialize own class or struct, use [System.Serializable] attribute.
[System.Serializable]
public struct PlayerStats
{
public int level;
public int health;
}

Unity: How do I pass as a parameter, a nested list initialized in its own class, to a method?

I will try to keep this to the point. I have created a nested list of GameObjects with:
[System.Serializable] public class TabSlots { public List<GameObject> tabslots; } [System.Serializable] public class Tablature { public List<TabSlots> tablature; } public Tablature allSlots = new Tablature();
This worked quite well, and I was able to drop the appropriate GameObject(s)
into their respective slots in the Inspector, which hierarchy looked like this:
AllSlots (This is the root of the nested list)
Tablature (12 elements)
Tabslots (3 elements)
I was able to successfully access the components of each GameObject in Tabslots. In the following example, I move an object in deck[0] to the position of allSlots.tablature[0].tabslots[0]. (deck is simply a one-level list of GameObjects)
deck[0].transform.position = Vector2.MoveTowards(deck[0].transform.position, allSlots.tablature[0].tabslots[0].transform.position, 70f * Time.deltaTime);
My QUESTION: How do I send the nested list created by: public Tablature allSlots = new Tablature(); to an IEnumerator method?
I have tried every possible combination I can think of for the call ( ??? represents the undiscovered code).
StartCoroutine(MyTest(deck, ??? ));
And also tried everything I could think of with the parameter list in the IEnumertor method ( ??? represents the undiscovered code).
IEnumerator MyTest((List<GameObject> list1, ??? ))
Can someone help me fill in the ??? blanks?
Thank you forward.
I have tried all the combinations of reference that I could think of.
You can just pass the type that you created:
...
StartCoroutine(MyTest(deck, allSlots));
}
private IEnumerator MyTest(List<GameObject> deck, Tablature tablature)
{
return null;
}

Zenject: MonoBehaviour injection

I'm new to Zenject and this is my first project using this asset. I'm having injection problems! Maybe someone knows what I am doing wrong or where the error might be. In the code below, _spawnArea is not initialized.
public class BootstrapIniter : MonoInstaller
{
[SerializeField] private Camera _mainCamera;
[Space(10)]
[SerializeField] private Spawner _spawner;
public override void InstallBindings()
{
BindMain();
BindBallHandle();
}
private void BindMain()
{
Container.Bind<Camera>().FromInstance(_mainCamera).AsSingle();
}
private void BindBallHandle()
{
Container.Bind<Spawner>().FromInstance(_spawner).AsSingle();
}
}
[RequireComponent(typeof(SpawnArea))]
public class Spawner : MonoBehaviour
{
private SpawnArea _spawnArea;
private void Awake()
{
_spawnArea = GetComponent<SpawnArea>();
}
[Inject]
public void Construct(Camera camera)
{
Rect cameraRect = camera.pixelRect;
_spawnArea.Init(cameraRect);
}
}
Thanks in advance for the answer or direction in which to look for a solution
I think that you did not inject your instance.
From the documentaiton "FromInstance - Adds a given instance to the container. Note that the given instance will not be injected in this case. If you also want your instance to be injected at startup, see QueueForInject" (QueueForInject will queue the given instance for injection once the initial object graph is constructed). Basically you need to inject your instance for the injected methods to execute.
On the other hand I dont see the point of binding a monobehaviour from instance, as you have to generate the instance bind it to the container and then inject it. You have binding methods that do this all at once for you, check the section "Construction Methods".
Check for example: FromComponentInNewPrefabResource - Instantiate the given prefab (found at the given resource path) as a new game object, inject any MonoBehaviour's on it, and then search the result for type ResultType in a similar way that GetComponentInChildren works (in that it will return the first matching value found).
Note that for the injection to take place succesfully you have to previously wire up the dependency in the container with the Container.Bind statement so that the container knows what needs to be injected and how.
I suggest to read carefully the documentation which is very good and follow the examples along.

Serialization of a list of custom objects in unity

While trying to make a script for building assets, I ran into an issue with unity's serialization. I have a class in which I store some arbitrary information, which is then stored in an array in a MonoBehaviour on a prefab. I cannot for the life of me get the array to save however, as when I make the object into a prefab it loses the list's values. I have tried using [System.Serializable] and ScriptableObject, but both seem to pose their own new issues.
For instance, using ScriptableObject would mean having to save the data objects as assets, which would become way too much since these objects can get to hundreds in number.
Am I making a mistake in my understanding of unity's serialization? Is there a way to get this working without the ScriptableObject approach of saving every ArbitraryInfo object in an asset?
Data object:
[System.Serializable]
public class ArbitraryInfo{
public int intValue;
public Vector3 vectorValue;
}
OR
public class ArbitraryInfo : ScriptableObject {
public int intValue;
public Vector3 vectorValue;
void OnEnable() {
hideflags = HideFlags.HideAndDontSave;
}
}
Behaviour:
public class MyBuilder : MonoBehaviour {
public ArbitraryInfo[] infoArray;
}
Editor:
[CustomEditor(typeof(MyBuilder))]
public class MyBuilderEditor : Editor {
private SerializedProperty infoArrayProperty;
void OnLoad() {
infoArrayProperty = serializedObject.FindProperty("infoArray");
}
void OnInspectorGUI() {
serializedObject.Update();
for (var i = 0; i < infoArrayProperty.arraySize; i++) {
if (i > 0) EditorGUILayout.Space();
var info = infoArrayProperty.GetArrayElementAtIndex(i).objectReferenceValue as ArbitraryInfo;
EditorGUILayout.LabelField("Info " + i, EditorStyles.boldLabel);
info.intValue = EditorGUILayout.IntField(info.intValue);
info.vectorValue = EditorGUILayout.Vector3Field(info.vectorValue);
}
serializedObject.ApplyModifiedProperties();
}
}
EDIT 1, Thank you derHugo
I changed my code to incorporate the changes. Now there are errors for ArbitraryInfo not being a supported pptr value.
Secondly, ArbitraryInfo no longer being a ScriptableObject poses the question of how to initialize it. An empty object can be added to infoArrayProperty through infoArrayProperty.arraySize++, but this new empty object seems to be null in my case. This might be due to the pptr issue mentioned above.
EDIT 2
The issue I was having was caused by another piece of code where I tried to check if infoArrayProperty.objectReferenceValue == null. I changed this to another check that did the same thing and everything worked!
No, no ScriptableObject needed.
But note that GetArrayElementAtIndex(i) returns a SerializedProperty. You can not simply parse it to your target class.
so instead of
var info = infoArrayProperty.GetArrayElementAtIndex(i).objectReferenceValue as ArbitraryInfo;
and
info.intValue = EditorGUILayout.IntField(info.intValue);
info.vectorValue = EditorGUILayout.Vector3Field(info.vectorValue);
you have to get the info's SerializedPropertys by using FindPropertyRelative:
var info = infoArrayProperty.GetArrayElementAtIndex(i);
var intValue = info.FindPropertyRelative("intValue");
var vectorValue = info.FindPropertyRelative("vectorValue");
than you can/should use PropertyFields
EditorGUILayout.PropertyField(intValue);
EditorGUILayout.PropertyField(vectorValue);
allways try to avoid using direct setters and use those SerializedProperties instead! This provides you with Undo/Redo functionality and marking the changed Behaviour/Scene as unsaved automatically. Otherwise you would have to tak care of that manually (... don't ^^).

Actionscript in a single class or in multiple scenes?

I am completely new to Actionscript and Adobe Flash CS6 and for a little bit of fun I have decided to try and make a little game. I had a few newbie (or noob-y) questions to ask about a general implementation approach.
The documentation I've been reading so far suggests creating a new flash project, and then create a document class so:
package {
import flash.display.MovieClip;
public class MyMainClass extends MovieClip {
public function MyMainClass() {
}
}
}
and I am wondering if I use this MainClass to code the whole game or include actionscript within a scene and have multiple scenes, or some combination of both.
Lets say I had a wanted 5 Levels in my game, would I do something like:
package {
import flash.display.MovieClip;
public class MyMainClass extends MovieClip {
public function MyMainClass() {
StartLevel1();
StartLevel2();
StartLevel3();
StartLevel4();
StartLevel5();
}
public function StartLevel1() {
// Do something
}
public function StartLevel2() {
// Do something
}
public function StartLevel3() {
// Do something
}
public function StartLevel4() {
// Do something
}
public function StartLevel5() {
// Do something
}
}
}
or create 5 scenes with actionscript in each scene?
Can anyone provide me with a bit of a starting point?
Thanks
I don't know of anyone who has anything good to say about scenes.
However, as you intuit, the timeline itself is a wonderful tool for managing the state of your Flash assets over time. If you use it, you also get the hidden advantage that you don't have to download 100% of your file to be able to use it (so you can reduce or even eliminate the need for a preloader by unchecking "Export in frame N" on your library symbols.
Lars has quite rightly pointed out that there are very few developers who understand this technique, and I know of exactly one who can and will help people who are interested in exploring this technique. That person is helping you right now. So if you choose to go that way, keep in mind you are mostly on your own except if I happen to notice your post and respond to it.
I am not in favor of timeline scripts, with a very few exceptions. What I suggest is a "both and" approach, where you use a Document Class to control timeline instances.
Your document Class might look something like this:
public class Game extends MovieClip {
protected var _level:ILevel;//Interface your Level MovieClips will implement
protected var levelController:LevelController = new LevelControler();
protected var currentLevel:int;
protected var maxLevels:int = 5;
public function Game() {
levelController.addEventListener(LevelEventKind.LEVEL_COMPLETE, nextLevel);
levelController.addEventListener(LevelEventKind.LEVEL_FAILED, gameOver);
startLevel(currentLevel);
}
public function startLevel(levelNumber:int):void {
goToLabel('Level' + String(levelNumber));
}
public function get level():ILevel {
return _level;
}
public function set level(value:ILevel):void {
_level = value;
//internally, this should release all listeners to the last
//level object (if any) so you don't get a memory leak
levelController.level = _level;
}
protected function nextLevel(e:Event):void {
if (currentLevel < maxLevels) {
startLevel(++currentLevel);
} else {
//do you won logic here
}
}
protected function gameOver(e:Event):void {
//do bombed out logic here
}
protected function goToLabel(label:String):void {
for each (var frameLabel:FrameLabel in currentLabels) {
if (frameLabel.name==label) {
//if your swf is media-heavy, may want to check that frame
//is loaded if you chose to reduce/eliminate preloader
goToAndStop(label);
return;
}
}
trace('no such label as', label);
}
}
What this gets you is a game where you can change how the different levels look without changing a single line of ActionScript, and you can change how they work by assigning different Base Classes that implement ILevel slightly differently. You can also change your functionality by swapping out different flavors of LevelController, but your Main Document Class (Game in this instance) would be aware of this change (wheras the other changes could be made without altering Game at all).