Home > Blockchain >  How to get a reference to a prefab asset in unity
How to get a reference to a prefab asset in unity

Time:05-26

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:

  1. I have the prefab asset already, it is in my assets folder not in my scene, creating the assets is not the problem.
  2. pixelPrefab would work fine as a static property, however, style would not.
  3. Style only needs serializing for a default value and so is not a priority.
  4. 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.
  5. I want to select the assets in the editior
  6. I do not want to have a function that must be called before the classes are used, so that the assets are loaded
  7. 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: If you can see this the image probably failed to load but idk

And this is the closest thing to what I want so far: If you can see this the image probably failed to load but idk

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.

OP's class in Editor

So, what to do if you want to view your class in an Editor pane and you also want to see pixelPrefab and style?

  1. Have something that can be serialized, like a MonoBehavior, hold a reference to PixelSystem OR have PixelSystem inherit MonoBehavior.
  2. If PixelSystem is not a MonoBehavior then it needs to have [System.Serializable] on the line above public class PixelSystem.
  3. Do what you've already done and have public GameObject pixelPrefab.
  4. 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.

  • Related