I'm new to libgdx, and I get this exception:
com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load file: C:/Users/myname/Desktop/myLibgdxGame/android/assets/sprites.png/sprites.png
on the following function on the textureAtlas = new TextureAtlas("sprites.txt"); line and I can't understand what's wrong.
#Override
public void create() {
batch = new SpriteBatch();
textureAtlas = new TextureAtlas("sprites.txt");
banana = textureAtlas.createSprite("spr");
}
Anyone knows what is the problem?
Thank you!
The path which you have given for texture atlas is wrong . In your case, Run time exception occurs because the java is unable to locate your texture atlas. So make sure that the texture atlas path is correct.
Related
Currently I'm working on a android program using Mobile Vision. I am using the "TextRecognizer" class and one of the methods is .detect(Frame frame). Right now I have a image I want to input into it, however, the image is the file type "Bitmap". I have tried to convert it to "Frame" by casting it but that hasn't worked. If anyone has any suggesting it would be much appreciated.
Use the setBitmap method in the Frame.Builder class:
Frame outputFrame = new Frame.Builder().setBitmap(myBitmap).build();
I am trying to download an image using this code in my unity project my an android game.
public static Texture2D userTexture=new Texture2D(64, 64, TextureFormat.DXT5, false); //TextureFormat must be DXT5
public static IEnumerator UserPictureCallback(){
Debug.Log("USerPicture called");
FbDebug.Log("USerPicture called");
WWW url = new WWW("http://i1.wp.com/a0.twimg.com/sticky/default_profile_images/default_profile_1_normal.png?resize=64%2C64");
yield return url;
//profilePic.renderer.material.mainTexture = textFb2;
Debug.Log(url);
url.LoadImageIntoTexture(userTexture);
Debug.Log("Working");
}
And then I am trying to draw it in GUI using DrawTexture.
GUI.DrawTexture(new Rect(5,0,50,50),FB.userTexture,ScaleMode.StretchToFill, false, 0.0f);
But the image texture is not getting downloaded or at least it is not showing in the GUI. Can somebody help me with this?
I recently faced the same problem. I was creating an scrollable image gallery in Unity, where images are downloaded in real-time from the a web server. On iOS devices I was reaching very fast the memory limit (with consequent app crash). That was caused due to a very big number of www objects leaved in the memory and never deleted or released. Also a very big number of Texture2D objects leaked.
The main misunderstanding is considering a Texture2D like a variable of an object that self-contained the image information. Texture2D only point to a referenced asset so we need to create one before assigning to our GUI object. Without creating one we will overwrite the referenced asset in our project, leading to very crazy behaviour.
So this the code that works for me on iOS and just use the minimum of memory
public void DownloadImage(string url)
{
StartCoroutine(coDownloadImage(url));
}
IEnumerator coDownloadImage(string imageUrl)
{
WWW www = new WWW( imageUrl );
yield return www;
thumbnail.mainTexture = new Texture2D(www.texture.width, www.texture.height, TextureFormat.DXT1, false);
www.LoadImageIntoTexture(thumbnail.mainTexture as Texture2D);
www.Dispose();
www = null;
}
A brief comment:
You receive a url you will download the image from.
Start the coroutine that manage the download
Create the www object in the local scope
yield waiting the download to complete
Create a new Texture2D object/asset with parameters and assign to your final object mainTexture or what do you want
Use the "www.LoadImageIntoTexture()" function to COPY the image inside the created asset (this is the fundamental part)
Dispose and set to null the www object to prevent memory leaking or orphans
Hope it helps who will face the same problem.
An interesting lecture is this website where they implement a WebImageCache system to avoid re-downloading the same image many times.
http://studiofive27.com/index.php/unity-cached-web-images/
I'm developing a game for Android using Unity, and I need load some texture from the Resources path. But this only works in the editor, and when I build the project for Android, the image doesn't load.
My code looks like this:
public void setTipTextTexture(string texture){
texture = "TipTexts/" + texture;
this.tipTexture = Resources.Load(texture) as Texture;
}
What am I doing wrong?
I am trying to load .obj files into an Android project with LibGDX. The files have no texture file, but include materials in .mtl files. I'm using the latest official nightly, and rendering the object file only results in the object appearing white. How do I get the ObjLoader to use the .mtl file?
#Override
public void create() {
objLoader = new ObjLoader();
model = objLoader.loadObj( Gdx.files.internal("data/obj.obj"), true);
}
#Override
public void render() {
Gdx.gl.glClearColor(0, 0, 0, 0);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.begin();
model.render();
batch.end();
}
This is how the code to render the object is called.
Here is a link to the ObjLoader class
https://github.com/libgdx/libgdx/blob/master/gdx/src/com/badlogic/gdx/graphics/g3d/loaders/wavefront/ObjLoader.java
What am I doing wrong? And why won't it load the .mtl file? From what I can understand, it should load a .mtl file that's in the same folder and same name as the .obj file.
EDIT
I have messed around a bit, putting some lines into the ObjLoader class to log what it is and isn't loading. It looks like it's loading the mtl file, and assigning each mtl to a Material instance, and it also looks like the obj is correctly asking for those materials.
Is there something I need to enable or otherwise do on the OpenGL end to make sure it's using these materials properly?
The ObjLoader and especially the MtlLoader it uses is very limited. Try using the new 3D api with fbx instead. Here's explained how to load a model: http://blog.xoppa.com/loading-models-using-libgdx/.
I found the very same issue, and that's exactly why I both reported it here:
https://github.com/libgdx/libgdx/issues/2441
and committed a fix to it here:
https://github.com/libgdx/libgdx/commit/d7e716351d26ddfba19ce9e0b3bdfb449dbc81b7
, supporting virtually all MTL parameters out there. Note that this is a WIP, and (hopefully) will get into trunk once it's finished.
Shouldn't this work?
TextureAtlas atlas = new TextureAtlas(Gdx.files.internal("data/texture.png"));
AtlasRegion region = atlas.findRegion("ape_glad");
Sprite ape= new Sprite(region);
Instead I get: com.badlogic.gdx.utils.GdxRuntimeException: Error reading pack file: data/texture.png at the first line above O.o
Thanks for helping!
First, you need to create a texture atlas, using TexturePacker is the recommended way for libgdx. It results in the texture image and another file (containing the required information for libgdx TextureAtlas).
In your code, you need to provide the atlas file to the constructor, see TextureAtlas() documentation, instead of the image itself:
TextureAtlas atlas = new TextureAtlas(Gdx.files.internal("data/texture.atlas"));
(Notice the use of the 'atlas'-file instead of the image file)