how to see a function in selection screen

basically i want to see my function

public static Sprite Grass()

in

idk if this is possible, but it will be really helpful.

Perhaps you’re looking for a ScriptableObject? With those you can make instances of them and thus gain instance access to a particular function you want within the SO. The function itself won’t be visible however.

Otherwise, your post sounds like a classic XY Problem, so let’s go back to: “What are you trying to accomplish here exactly?”

public static Sprite Grass()
{
var texture = new Texture2D(32, 32);
texture.requestedMipmapLevel = 0;
for (int i = 0; i < 32 * 32; i++)
{
var selector = new int[ ] { i % 32, i / 32 };
var ran = Random.Range(0.3f, 0.6f);
texture.SetPixel(selector[0], selector[1], new Color(ran, 1, ran));
}

texture.Apply();
texture.filterMode = 0;

return Sprite.Create(texture, Rect.MinMaxRect(0, 0, 32, 32), Vector2.zero, 32);
}

i wanted to have random generation, thats why i wanted to have the code in editor.

Static anything only exists in memory. Static anything can’t be serialised in the inspector either, save using reflection (like UnityEvents do).

You need to be more clear on what you’re trying to achieve.

Maybe you want the ability to preview your sprite/texture and the generating code simultaneously–similar to particles?
You still have to use an external IDE to change code, use public variables for tweaking in inspector, and run/play to visualize. Since i’m relatively new to Unity, I could be missing something for textures.
I’m vaguely aware that there image editing apps that are scriptable, and perhaps more geared to what you want.

[edit for clarity]

i can use the function like a script in code, but theres a switch statement that use it according to tile id.
i want to assign function in editor, as you can see sprite section is empty,8697756--1173897--upload_2023-1-1_21-47-23.png
thats because that sprite comes from function,
theres that tiledata which does this, sprite in grass is not actually used, i want to put function that returns sptire
instead of a sprite into sprite.
8697756--1173903--upload_2023-1-1_21-48-50.png

i slowly start to realize how its not going to work, even if i succeed it probably would run function once and just copy paste same sprite to everything.

I’ve wanted to do something similar before. I was creating an infinite runner game for Ludum Dare, and had several different “strategies” for obstacle spawning. One would just place objects in a line; another would place them completely randomly.

I figured I could make a list of strategies, then randomly draw from them to assemble the course.

I wound up creating a class deriving from ScriptableObject for each strategy. Then, I created one or more assets from each of the classes. Those assets went into the course’s strategy list.

It felt very clunky. I wanted to put config data in the course definition (e.g. setting how many obstacles a specific strategy should spawn). I wound up putting that data in the ScriptableObjects themselves.

Perhaps someone else has found a nicer way to do such a thing…?

It’s a hard problem and it gets harder the more you bind the data definitions to the game behaviors.

Simplest might be a spawner that says “make this prefab at X,Y” and you just have lists of those.

But once you start getting more and more intricate in terms of what it can and cannot do, what your program expects to learn from the data definition, it can quickly balloon in complexities.

Base classes like you used are one solution. Another solution would be to implement interfaces in your ScriptableObjects. But again, the more back-and-forth with the data definitions you contemplate, then the more boilerplate and plumbing has to go in.

That’s why to the greatest extent possible I really like to just put everything into scenes. I will have an “EnemiesScene” (additively loaded) that contains one copy of every enemy in my games, all parented under one GameObject that I can easily locate.

The enemy spawner lives in that scene and knows where those template enemies are and then can spawn them where they need to spawn in a level.

On loading the enemy spawner waits for the level spawnpoints to appear (just checking every frame), and when they do, then it begins to spawn enemies, using the templates in the scene.

Scenes are just incredibly powerful in Unity once you start slicing and dicing them up. They’re almost better than prefabs in some ways, although prefabs certainly have an important place in the Unity workflow.

Additive scene loading notes:

https://discussions.unity.com/t/820920/2
https://discussions.unity.com/t/820920/4

https://discussions.unity.com/t/824447/2

A multi-scene loader thingy:

https://pastebin.com/Vecczt5Q

My typical Scene Loader:

https://gist.github.com/kurtdekker/862da3bc22ee13aff61a7606ece6fdd3

Other notes on additive scene loading:

https://discussions.unity.com/t/805654/2

Timing of scene loading:

https://discussions.unity.com/t/813922/2

Also, if something exists only in one scene, DO NOT MAKE A PREFAB out of it. It’s a waste of time and needlessly splits your work between two files, the prefab and the scene, leading to many possible errors and edge cases.

Two similar examples of checking if everything is ready to go:

https://discussions.unity.com/t/840487/10

https://discussions.unity.com/t/851480/4

1 Like

I do need to start looking into additive scenes. Thanks, I’ll check that stuff out sometime (once I finish tormenting myself with netcode…)

1 Like