banner



How To Draw A Image At A Screen Position Programmatically Unity

Unity does non have this kind of "direct fashion" rendering, yous won't be calling Describe in any update loops for Unity.

In Unity you need to create a game object, and then attach scripts to that game object. Those scripts will control how the object behaves, if and how it's displayed on screen, if it's part of the physics arrangement etc.

To create a new sprite to draw on screen you lot'll demand to create a new game object, and then attach the SpriteRenderer script to information technology and set up its sprite.

          GameObject go = new GameObject(); SpriteRenderer renderer = go.AddComponent<SpriteRenderer>();  renderer.sprite = Resources.Load("Sprites/Actor", typeof(Sprite)) as Sprite;                  

This assumes a directory structure of

          -Assets --Resources ---Sprites ----<Your 2D assets, prepare with their texture type to Sprite (2nd and UI)>                  

For example:

enter image description here

And so you'd had a unlike script for changing the position, rotation etc, depending how you lot want it to acquit.

The easiest way to accomplish all of this without having to write every bit much code? Create the game object in the Unity editor first. In one case the game object is equipped with the sprite you want and the behavior scripts and all that. Elevate and drop information technology into a resource directory. This creates a prefab. Prefabs are just what they sound similar, pre-fabrications. They're consummate objects that you tin then reference and create on the fly.

You lot'd create those like:

          public void CreateObject(string prefabName) {    GameObject newObject =        GameObject.Instantiate(Resource.Load(prefabName)) equally GameObject; }                  

This will spawn a clone of the prefab you created before. And then, if for instance, you lot'd given your prefab the behavior of moving towards the role player and attacking, every time you spawn a new prefab of that type, they will start moving towards the player and attacking. The scripts you just had to write one time control all of those game objects.

I think the master difference you're seeing hither is the heavy apply of components. Which you likely haven't seen in other game engines like XNA. Unity really is a powerful engine, but your frustration comes from trying to employ it like other engines, when it'south non like other engines. Check out my contour for some training, I added a link for a free trial and so you lot don't take to pay.

Source: https://gamedev.stackexchange.com/questions/96058/unity-5-2d-drawing-sprites-programmatically

Posted by: leehaturat.blogspot.com

0 Response to "How To Draw A Image At A Screen Position Programmatically Unity"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel