- Home /
loading texture from folder dynamically?
silly question i think...
if i place a texture in the same folder as the .exe, is it possible to dynamically load that texture to a 3d object?
Answer by FlashX · Feb 08, 2017 at 09:27 AM
So i figgured it out! its taken me all day but with the help of AR_Rizvi its all working!!!
the problem... you cant use a friggin PSD file like you can if you were to just drag and drop it onto the object!!! so annoying!!!
// Add this script to a GameObject. The Start() function fetches an
// image from the documentation site. It is then applied as the
// texture on the GameObject.
using UnityEngine;
using System.Collections;
public class tex : MonoBehaviour {
private string url = "file:///Users/theuser/Desktop/map.png";
IEnumerator Start() {
Texture2D tex;
tex = new Texture2D(4, 4, TextureFormat.DXT1, false);
WWW www = new WWW(url);
yield return www;
www.LoadImageIntoTexture(tex);
GetComponent<Renderer>().material.mainTexture = tex;
}
}
Hope this helps someone else out cos ive been beating my head over it all day!!
Answer by AR_Rizvi · Feb 07, 2017 at 12:44 PM
Try This
Thanks AR_Rizvi,
I am able to get images from the web working though im having some difficulty getting them directcly from a mac hard drive. ideally id like to have a folder called textures at the same level as the exported .dmg file. ive tried a number of things though i keep getting a "?" texture on my object.
any ideas?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class loadTex : $$anonymous$$onoBehaviour {
private string url = "file:///~/textures/testmap.psd";
// Use this for initialization
IEnumerator Start() {
WWW www = new WWW(url);
yield return www;
Renderer renderer = GetComponent<Renderer>();
renderer.material.mainTexture = www.texture;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612110529im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Assigning UV Map to model at runtime 0 Answers
Terrain Procedural Texturing based on height? 4 Answers
Choose a texture in-game 1 Answer
Leaking Textures in custom editor 2 Answers
Image loaded with LoadImageIntoTexture gets inverted 0 Answers