Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question how to see a function in selection screen

Discussion in 'Scripting' started by ozan1911, Dec 31, 2022.

  1. ozan1911

    ozan1911

    Joined:
    Feb 21, 2022
    Posts:
    8
    basically i want to see my function
    Code (CSharp):
    1. public static Sprite Grass()
    in



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

    Attached Files:

  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    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?"
     
  3. ozan1911

    ozan1911

    Joined:
    Feb 21, 2022
    Posts:
    8

    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.
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,003
    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.
     
  5. helpmehelpyouhelpme

    helpmehelpyouhelpme

    Joined:
    Oct 15, 2017
    Posts:
    19
    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]
     
    Last edited: Jan 1, 2023
  6. ozan1911

    ozan1911

    Joined:
    Feb 21, 2022
    Posts:
    8
    upload_2023-1-1_21-43-39.png

    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, 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.
    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.
     
  7. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    717
    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..?
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    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://forum.unity.com/threads/right-way-for-performance-divide-scene.1023673/#post-6630961
    https://forum.unity.com/threads/right-way-for-performance-divide-scene.1023673/#post-6754330

    https://forum.unity.com/threads/problem-with-canvas-ui-prefabs.1039075/#post-6726169

    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://forum.unity.com/threads/removing-duplicates-on-load-scene.956568/#post-6233406

    Timing of scene loading:

    https://forum.unity.com/threads/fun...ject-in-the-second-scene.993141/#post-6449718

    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://forum.unity.com/threads/daily-events-and-content-changes.1108202/#post-7143713

    https://forum.unity.com/threads/uni...on-before-other-scripts.1153739/#post-7401794
     
    chemicalcrux likes this.
  9. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    717
    I do need to start looking into additive scenes. Thanks, I'll check that stuff out sometime (once I finish tormenting myself with netcode...)
     
    Kurt-Dekker likes this.