Originally Posted By: preacherX
Or does someone know if there is an easier way to do this in Unity?

Hi,

It is not as difficult as it looks like. wink
But you have to know that you need two different objects.
First you need a sprite object. This can simply be created with Sprite.Create

Code:
mySprite = Sprite.Create(tex, new Rect(0.0f, 0.0f, tex.width, tex.height), new Vector2(0.5f, 0.5f), 100.0f);



But this is only a sprite object in memory.
If you want to render this sprite, you also need a gameObject for it (or many gameObjects
if you want to render that sprite many times in the scene). This gameObject is similar to an 'entity' in 3DGS.

GameObjects can manually be created in the level editor, or at runtime via the following coding:
Code:
GameObject GOname = new GameObject();



A gameObject is only something like a container for all kinds of entities.
It can be a sound, or a mesh, or many other things.
GameObjects can have one or more Components who defines which functionalities this object has.

Such Components can be 'MeshRenderer' or 'AudioSource' or 'VideoPlayer' or physics effects,
or hundreds of other things. And you also can develop your own Components (classes).

And one of these Components is the 'SpriteRenderer'. It is used to render sprites in a scene.

So if you have created a gameObject for the sprite at runtime, you also need to add a Component for
rendering sprites. And this is done by:
Code:
private SpriteRenderer sr;
sr = gameObject.AddComponent<SpriteRenderer>() as SpriteRenderer;



And then you only need to assign your sprite to that SpriteRenderer Component (set parameter 'sprite').
This is done by:
Code:
sr.sprite = mySprite;


That's all. wink
Okay, then you also can use e.g. transform.position to position your sprite, or transform.rotation,
or something like that. Because each gameObject automatically has a 'transform' Component which
can be used to position, rotate or scale a gameObject.


Here is a typical example for a class, developed for Unity.
It has one parameter for the texture (public Texture2D tex;).
It creates an empty sprite renderer, with a light grey non transparent background color.
And it creates a button with text 'Add sprite' on the screen.
When you press that button, it is assigning the texture to the sprite.
Code:
// Create a Sprite at start-up.
// Assign a texture to the sprite when the button is pressed.

using UnityEngine;

public class spriteCreate : MonoBehaviour
{
    public Texture2D tex;
    private Sprite mySprite;
    private SpriteRenderer sr;

    void Awake()
    {
        sr = gameObject.AddComponent<SpriteRenderer>() as SpriteRenderer;
        sr.color = new Color(0.9f, 0.9f, 0.9f, 1.0f);

        transform.position = new Vector3(1.5f, 1.5f, 0.0f);
    }

    void Start()
    {
        mySprite = Sprite.Create(tex, new Rect(0.0f, 0.0f, tex.width, tex.height), new Vector2(0.5f, 0.5f), 100.0f);
    }

    void OnGUI()
    {
        if (GUI.Button(new Rect(10, 10, 100, 30), "Add sprite"))
        {
            sr.sprite = mySprite;
        }
    }
}



Here the documentation:
https://docs.unity3d.com/ScriptReference/Sprite.Create.html

Greetings,
Harry