I'm currently making a game in XNA. I have been going well so far but I don't know how to change the texture of a sprite whilst running the game. An example of this would be. When the character is still that's one image, then when he walks that's a different image. How would I make this happen?
Run checks and just set the instance's Texture2D texture to some other texture from your preloaded library.
I usually load all the textures in my content folder into a dictionary and use like so:
var StringTextureDic = new Dictionary<string, Texture2D>();
// code that loads all textures into the dictionary, file names being keys
// whenever I need to assign some texture, I do this:
if (!playerIsMoving)
Player.texture = StringTextureDic["player standing"];
if (playerIsMoving)
Player.texture = StringTextureDic["player moving"];
Well, actually, change the player texture midgame is a bad idea. In such cases, i prefer to use a texture sheet.
Rectangle frame;
int curFrame;
int frameWidth;
int frameHeight;
int runAnimationLength;
Update()
{
//Handle your "animation code"
if(playerIsMoving)
curFrame++; //Running
if(curFrame == runAnimationLength)
curFrame =0;
else
curFrame = 0; //Standing still
}
Draw(SpriteBatch spriteBatch)
{
frame = new Rectangle(curFrame*frameWidth,curFrame*frameHeight,frameWidth,frameHeight);
spriteBatch.Draw(
texture,
position,
**frame**,
color,
rotation,
origin,
SpriteEffects.None,
1);
}
Related
I have a camera in my scene that has a Target Texture set as a Render Texture. I want that camera to render the scene into the Render Texture, but then stop rendering anymore, essentially freezing the frame into the Render Texture. How can I achieve this? Disabling the camera or setting its target texture to null seems to just make the Render Texture appear invisible.
If you want to have like a snapshot function you could do it like this
public class SnapshotController : MonoBehaviour
{
[Tooltip("If you want to capture a specific camera drag it here, otherwise the MainCamera will be used")]
[SerializeField] private Camera _camera;
[Tooltip("If you have a specific RenderTexture for the snapshot drag it here, otherwise one will be generated on runtime")]
[SerializeField] private RenderTexture _renderTexture;
private void Awake()
{
if(!_camera) _camera = Camera.main;
if(!_renderTexture)
{
_renderTexture = new RenderTexture(Screen.width, Screen.height , 24 , RenderTextureFormat.ARGB32);
_renderTexture.useMipMap = false;
_renderTexture.antiAliasing =1;
}
}
public void Snapshot(Action<Texture2D> onSnapshotDone)
{
StartCoroutine(SnapshotRoutine(onSnapshotDone));
}
private IEnumerator SnapshotRoutine (Action<Texture2D> onSnapshotDone)
{
// this also captures gui, remove if you don't wanna capture gui
yield return new WaitForEndOfFrame();
// If RenderTexture.active is set any rendering goes into this RenderTexture
// instead of the GameView
RenderTexture.active = _renderTexture;
_camera.targetTexture = _renderTexture;
// renders into the renderTexture
_camera.Render();
// Create a new Texture2D
var result = new Texture2D(Screen.width,Screen.height,TextureFormat.ARGB32,false);
// copies the pixels into the Texture2D
result.ReadPixels(new Rect(0,0,Screen.width,Screen.height),0,0,false);
result.Apply();
// reset the RenderTexture.active so nothing else is rendered into our RenderTexture
RenderTexture.active = null;
_camera.targetTexture = null;
// Invoke the callback with the resulting snapshot Texture
onSnapshotDone?.Invoke(result);
}
}
You would then use it like e.g.
// Pass in a callback telling the routine what to do when the snapshot is ready
xy.GetComponent<SnapshotController>(). Snapshot(HandleNewSnapshotTexture);
...
private void HandleNewSnapshotTexture (Texture2D texture)
{
var material = GetComponent<Renderer>().material;
// IMPORTANT! Textures are not automatically GC collected.
// So in order to not allocate more and more memory consider actively destroying
// a texture as soon as you don't need it anymore
if(material.mainTexture) Destroy (material.mainTexture);
material.mainTexture = texture;
}
You can take a snapchot of the actual render texture in the frame where you start freezing, get this image data and replace the render texture by an actual texture with this freeze image applied.
I would like to draw a GameObject in front of all other components in my project and GUI Textures as well.
I created a second Camera and set Depth and Layer but it still not work. I hope you can help me to find the error or something I forgot.
Here is my MainScript which is drawing a simple Texture:
using UnityEngine;
using System.Collections;
public class MainScript : MonoBehaviour
{
Texture2D texture;
// Use this for initialization
void Start()
{
texture = new Texture2D(Screen.width, Screen.height);
for (int y = 0; y < texture.height; y++)
{
for (int x = 0; x < texture.width; x++)
{
texture.SetPixel(x, y, Color.blue);
}
}
texture.Apply();
}
void OnGUI()
{
GUI.DrawTexture(new Rect(0, 0, texture.width, texture.height), texture);
}
}
I also created two cameras and a GameObject which displays a GUI Texture. The Texture is visible in the preview screen but on runtime the Texture which is drawing in the MainScript is foregrounded.
I made two more Screenshots of my Camera Objects. See here:
I can also supply the whole project for you. It is just a basic test project.
Here is the link to the Project in Google Drive: Download
set depth of camera2 to camera1.depth+1, Clear Flags of camera2 to depth only and Clear Flags of camera1 to skybox. Uncheck GUILayer at Camera2 and check GUILayer in camera1. That should do it...
You cannot draw 3D objects in front of the GUI elements, OnGUI code always renders in top of everything.
To achieve this you can use Render Textures (Unity Pro only): have two cameras in your scene, place your 3D objects in one camera, render this camera to a texture, and finally use that texture as the source of a GUI.DrawTexture().
I'm attempting my first 2D updown scroller really, and i can't seem to get the background size to fully work. for whatever reason it stays small and it is of no help.
I think in the beginning of the parallax scrolling class i was told to initialize variables and I know which one I have to change.
public Texture2D picture;
public Vector2 position = Vector2.Zero;
public Vector2 offset = Vector2.Zero;
public float depth = 0.0f;
public float moveRate = 0.0f;
public Vector2 pictureSize = Vector2.Zero;
public Color color = Color.White;
I believe i must change pictureSize in order to make it bigger as the current sizing i get this!
The final question i have is, How can I remove the whitespace from the ship image?
Thanks!
If any more code is needed please tell me, the reason I did not put it there, is due to the length.
EDIT*:
Here is the code for the rendering of the actual ship
public void Render(SpriteBatch batch)
{
batch.Begin();
batch.Draw(shipSprite, position, null, Color.White, 0.0f, spriteOrigin, 1.0f,
SpriteEffects.None, 0.0f);
batch.End();
}
The whitespace seems to be provided by XNA as I've cleared it several times using photoshop.
Because i can't directly upload to the site I have put it here: http://www.justbeamit.com/nk95n
And for the background rendering its
public void Draw()
{
layerList.Sort(CompareDepth);
batch.Begin();
for (int i = 0; i < layerList.Count; i++)
{
if (!moveLeftRight)
{
if (layerList[i].position.Y < windowSize.Y)
{
batch.Draw(layerList[i].picture, new Vector2(0.0f,
layerList[i].position.Y), layerList[i].color);
}
if (layerList[i].position.Y > 0.0f)
batch.Draw(layerList[i].picture, new Vector2(0.0f,
layerList[i].position.Y - layerList[i].pictureSize.Y),
layerList[i].color);
else
batch.Draw(layerList[i].picture, new Vector2(0.0f,
layerList[i].position.Y + layerList[i].pictureSize.Y),
layerList[i].color);
}
else
{
if (layerList[i].position.X < windowSize.X)
{
batch.Draw(layerList[i].picture, new Vector2(layerList[i].position.X,
0.0f), layerList[i].color);
}
if (layerList[i].position.X > 0.0f)
batch.Draw(layerList[i].picture, new Vector2(layerList[i].position.X -
layerList[i].pictureSize.X, 0.0f), layerList[i].color);
else
batch.Draw(layerList[i].picture, new Vector2(layerList[i].position.X +
layerList[i].pictureSize.X, 0.0f), layerList[i].color);
}
}
batch.End();
}
^
This is not my code but rather from a book I'm reading on how to do XNA(first attempt at C# to :/)
Without all the relevant code and details it is a bit hard to tell the problem, But I assume your scrolling code has a spriteBatch.Draw() call simlar to this:
spriteBatch.Draw(Texture, Destination, Source, Color.White);
TheDestination is probably equal too new Rectangle(0,0,pictureSize.X,pictureSize.Y), I'm assume with no code shown that the pictureSize is the size of the picture (No really!), with the spritebatch Destination parameter, you can "stretch" your image to your viewport.
With this your PictureSize should be the size of your viewport
pictureSize = new Vector2(graphics.PreferredBackBufferWidth,graphics.PreferredBackBufferHeight);
You can also use the Scale parameter of the SpiteBatch to enlarge it (However it will get blurred) or just create a bigger image for your needs.
Now for the spaceship having a white background, is XNA doing this or is the image white? I assume the latter, you will need a photo editing tool such as GIMP to remove the white background of the image.
How can I remove the whitespace from the ship image?
You could try making white transparent, using the Bitmap.MakeTransparent method. Something like this might work:
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
Bitmap test = new Bitmap("C:\Myimage.jpg");
test.MakeTransparent(Color.White);
Very odd problem, when I try and draw my billboard sprite it always appears as a white block, changing the .draw color property still draws it as white, it also doesn't matter it I use a jpeg, or transparent png.
[EDIT]
So I'm now trying to use a Viewport instead of a basic effect to just get an x and y screen coordinate, I'll fix any scaling issue later, however now the image stays in the exact same spot (on the screen, it doesn't change position based on the camera) and doesn't get any bigger or smaller based on how far away it is
My new billboard rendering function:
public void Draw(Camera camera, GraphicsDevice device, SpriteBatch spriteBatch, Texture2D Texture)
{
Viewport viewport = new Viewport(new Rectangle(0, 0, 800, 480));
Vector3 viewSpaceTextPosition = viewport.Project(this.position, camera.Projection, camera.View, camera.World);
spriteBatch.Begin();
spriteBatch.Draw(Texture, new Vector2(viewSpaceTextPosition.X, viewSpaceTextPosition.Y), null, Color.White, 0, new Vector2(Texture.Bounds.Center.X, Texture.Bounds.Center.Y), this.Scale, SpriteEffects.None, viewSpaceTextPosition.Z);
spriteBatch.End();
device.RasterizerState = RasterizerState.CullCounterClockwise;
device.BlendState = BlendState.Opaque;
device.DepthStencilState = DepthStencilState.Default;
}
So is my use of Viewport wrong or do I just need to use it's information differently in spriteBatch.Draw()?
I think this should do the trick using viewport project... taking two projection points and calculating its distance you get a value affected by depth... so if it's deeper that value will be smaller.
public void Draw(Camera camera, GraphicsDevice device, SpriteBatch spriteBatch, Texture2D Texture)
{
Vector3 pos1= device.Viewport.Project(
this.position,
camera.Projection, camera.View, camera.World);
Vector3 pos2= device.Viewport.Project(
this.position+ Vactor3.UnitY*10,
camera.Projection, camera.View, camera.World);
Vector2 pos = new Vector2(pos1.X, pos1.Y);
Vector2 origin = new Vector2(Texture.Bounds.Center.X, Texture.Bounds.Center.Y);
float Scale = Vector3.Distance(pos1, pos2) * CustomRatio;
spriteBatch.Begin();
spriteBatch.Draw(Texture, pos, null, Color.White, 0,
origin, Scale, SpriteEffects.None, 0);
spriteBatch.End();
device.RasterizerState = RasterizerState.CullCounterClockwise;
device.BlendState = BlendState.Opaque;
device.DepthStencilState = DepthStencilState.Default;
}
In other hand... your previous code seems to be extracted from a source that drinks from this article made by the guy behind Xna that explain how to use basiceffect to draw billboards in 3D with spritebatch...
http://blogs.msdn.com/b/shawnhar/archive/2011/01/12/spritebatch-billboards-in-a-3d-world.aspx
I hope it helps you
Figured it out, you have to enable textures on the effect and then set effect.Texture to the Texture2D you want to use just before calling spriteBatch.Begin() in the draw function
Alright, let's say that I have a tile texture of some floor or something. And I'd like that my player will walk on that.
How can I set this tile to make it a as a floor?
I need this tile texture to be all over the screen width right?
How am I doing it?
Thanks
If you want a really easy way, here it is:
First you create a new Class and name it Tile:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework; // Don't forget those, they will let you
using Microsoft.Xna.Framework.Content; // access some class like:
using Microsoft.Xna.Framework.Graphics; // Texture2D or Vector2
namespace Your_Project _Name
{
class Tile
{
}
{
So far so good, now create the Texture and Position in your class just like this:
namespace Your_Project _Name
{
class Tile
{
Texture2D texture;
Vector2 position;
public void Initialize()
{
}
public void Draw()
{
}
}
{
As you can see I also created two Methods, Initialize and Draw, now we will Initialize our
texture and position for the tile texture in the public void Initialize(),
I don't know how you use your ContentManager but here is a easy way:
public void Initialize(ContentManager Content)
{
texture = Content.Load<Texture2D>("YourfloorTexture"); //it will load your texture.
position = new Vector2(); //the position will be (0,0)
}
Now we need to draw our texture a number of time how will we do that? The way thasc said, the code can be more complex but here is one that you will understand, I will add a SpriteBatch so I can Draw. All this is done in the public void Draw():
public void Draw(SpriteBatch spriteBatch)
{
for (int i=0; i<30;i++) //will do a loop 30 times. Each Time i will =
//a valor from 0 to 30.
{
spriteBatch.Draw(texture, position, Color.White);
//Will draw the texture once, at the position Vector2
//right now position = (0,0)
spriteBatch.Draw(texture, new Vector2((int)i,(int)i), Color.White);
//Will Draw the texture 30 times, the first time on the position (0,0)
//Second Time on (1,1) .. third (2,2) etc...
spriteBatch.Draw(texture, new Vector2((int)position.X + (i * texture.Width), (int)position.Y + (i * texture.Height), Color.White));
//Will Draw the Texture 30 times Spaced by the Width and height
//of the texture (this is the code you need)
}
}
I didn't tried it but it should work, now its just a sample, you can figure out the rest. There is a lot of other methods to do it but this one is really easy. Ok, now the final step is to implement this class so go in your principal class where you have all your code and before this:
public Game1()
Create a new instance of your tile class
Tile tile;
and Initialize it in the protected override void Initialize():
tile = new Tile();
tile.Initialize(Content);
Now you have to draw it on the screen go at the end of the class and find protected override void Draw(GameTime gameTime) and call the draw method of our class:
spriteBatch.Begin();
tile.Draw(spriteBatch);
spriteBatch.End();
This is all the steps to complete a plain simple tile system. As I said there is a lot of others methods you just have to read tutorials about them or create them on your own.
If you don't plan on doing anything extra with the tiled background, I'd recommend thasc's solution and tile the sprite in a single call.
To do that, you create a rectangle as large as your background, and pass SamplerState.LinearWrap to SpriteBatch.Begin, then call Draw on the background rectangle.
Rectangle backgroundRect = new Rectangle(0, 0, backWidth, backHeight);
spriteBatch.Begin(..., ..., SamplerState.LinearWrap, ..., ...);
spriteBatch.Draw(backgroundTexture, backgroundRect, Color.White);
spriteBatch.End();
In case you're curious, what this does is create a single polygon that covers the background area, which will grab coordinates off your texture from 0.0f to backWidth. Textures are usually mapped between (0.0f, 0.0f) and (1.0f, 1.0f), which represent the corners of the given texture. If you go beyond these boundaries, TextureAddressMode defines how these coordinates will be treated:
Clamp will cut down the coordinates back into the 0-1 range.
Wrap will wrap the coordinates back to 0, so 0.0 = 2.0 = 4.0 = etc. and 1.0 = 3.0 = 5.0 = etc.
Mirror will also wrap, but mirroring the texture every other pass, basically going left-to-right-to-left-etc. as the polygon is rendered.