I have the following class
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PixelSystem
{
public GameObject pixelPrefab;
public Texture2D[] style;
public List<Chunk> chunks;
public GameObject mGameObject;
public PixelSystem(GameObject _mGameObject)
{
mGameObject = _mGameObject;
chunks = new List<Chunk>();
}
}
Chunk
and Pixel
are structs but I don't think they are relevant.
I would like pixelPrefab
and style
to be serialized but cannot seem to be able to make that work. This script is not attatched to a GameObject.
I have looked into getting the assets at runtime with Resources
or Addressables
but I would like to be able to set these fields from the editor.
I have tried making the class inherit from MonoBehaviour
and having [SerializeField] public GameObject pixelPrefab;
and [SerializeField] public Texture2D[] style;
, but the result of this is that pixelPrefab
is settable from the editor but instead of showing style
it shows mGameObject
, for some reason... ¯\_(ツ)_/¯
Edit: Turns out that the [SerializeFields]
do nothing here, the result is the same without them.
I've also tried puting [System.Serializable]
before the class, but as far as I can tell this does nothing at all.
Someone plz help.
Edit:
Due to some misunerstandings I would like to clarify a few things:
- I have the prefab asset already, it is in my assets folder not in my scene, creating the assets is not the problem.
pixelPrefab
would work fine as astatic
property, however,style
would not.- Style only needs serializing for a default value and so is not a priority.
- This code is very incomplete, the constructor is incomplete, I want an answer to this qusetion before I spend time writing code that may be unusable.
- I want to select the assets in the editior
- I do not want to have a function that must be called before the classes are used, so that the assets are loaded
- I do not want to load the assets each time they are referenced, so no
get {load();}
This is what I want to see in the editor when I select my script:
And this is the closest thing to what I want so far:
Which I get by setting the class to inherit from MonoBehaviour
, I'd rather not inherit from anything, and idk if you are meant to have MonoBehaviour
classes that aren't attatched to gameObjects, but if this is necessary then so be it.
So, what to do if you want to view your class in an Editor pane and you also want to see pixelPrefab
and style
?
- Have something that can be serialized, like a
MonoBehavior
, hold a reference toPixelSystem
OR havePixelSystem
inheritMonoBehavior
. - If
PixelSystem
is not aMonoBehavior
then it needs to have[System.Serializable]
on the line abovepublic class PixelSystem
. - Do what you've already done and have
public GameObject pixelPrefab
. - Convert
style
from an array to a list so Unity knows how to draw it.
You should wind up with the following:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class PixelSystem
{
public GameObject pixelPrefab;
public List<Texture2D> style = new List<Texture2D>();
public List<Chunk> chunks;
public GameObject mGameObject;
public PixelSystem(GameObject _mGameObject)
{
mGameObject = _mGameObject;
chunks = new List<Chunk>();
}
}
You can also have Chunk
be serialized and shown in your Editor pane if you tag that class with [System.Serializable]
, and you only need the [SerializeField]
tag for fields that aren't already public
. You can still add that tag to public fields just like you can add [System.Serializable]
to a MonoBehaviour
but it's redundant. Not required.