Loadimageintotexture not working?
I am working on a script from a directory if they are .png. and then creates an animation clip from them, and then overrides an animation with them.
Before you look at my long code. Here is the summary:
A function called "importspritesfromDirectory" checks for any png files in specified directory. and it calls a coruoutine function called "Uploadingimages"
Uploading images loops through all the png files and use loadimagetotexture to create a new texture and then stores it in a new sprite, finally adds that new sprite to a list of sprites.
The list of sprites is passed as an input for "createspriteanimationclip" function which creates an animation using these sprites. which is then used to override an existing animation using animation controller override
Here is my code:
public class SwapAnimation : MonoBehaviour {
public string folderDirectory = "C:/Users/Hossam/Documents/filesgoesHere";
var newFolder = Directory.CreateDirectory(folderDirectory + "/Idle");
idleInputDirectory = folderDirectory + "/Idle";
idleInputSprite = importSpritesFromDirectory(idleInputDirectory);
clipOverrides["Idle"] = CreateSpriteAnimationClip("Idle", idleInputSprite, 30);
animatorOverrideController.ApplyOverrides(clipOverrides);
}
private List<Sprite> importSpritesFromDirectory(string Directory)
{
List<Sprite> importedSprites = new List<Sprite>();
string[] files = System.IO.Directory.GetFiles(Directory, "*.png");
print(files.Length);
StartCoroutine(uploadingFile(files, importedSprites));
/*
foreach (FileInfo image in new DirectoryInfo(Directory).GetFiles())
{
if (image.Extension.Contains("png") || image.Extension.Contains("jpg"))
{
Sprite newSprite = new Sprite();
uploadingFile(image.Directory + "/" + image.Name,newSprite);
importedSprites.Add(newSprite);
numberofFramesFound++;
}
}
*/
for(int i = 0;i < importedSprites.Count; i++)
{
print(importedSprites[i]);
}
return importedSprites;
}
private AnimationClip CreateSpriteAnimationClip(string name, List<Sprite> sprites, int fps)
{
int framecount = sprites.Count;
float frameLength = 1f / 30f;
AnimationClip clip = new AnimationClip();
clip.frameRate = fps;
AnimationUtility.GetAnimationClipSettings(clip).loopTime = true;
EditorCurveBinding curveBinding = new EditorCurveBinding();
curveBinding.type = typeof(SpriteRenderer);
curveBinding.path = spriteToBeEdited.name;
curveBinding.propertyName = "m_Sprite";
ObjectReferenceKeyframe[] keyFrames = new ObjectReferenceKeyframe[framecount];
for (int i = 0; i < framecount; i++)
{
ObjectReferenceKeyframe kf = new ObjectReferenceKeyframe();
kf.time = i * frameLength;
kf.value = sprites[i];
keyFrames[i] = kf;
}
clip.name = name;
clip.wrapMode = WrapMode.Once;
AnimationUtility.SetObjectReferenceCurve(clip, curveBinding, keyFrames);
return clip;
}
private IEnumerator uploadingFile(string[] allFiles,List<Sprite> inputSprite)
{
/*
Texture2D tex;
tex = new Texture2D(4, 4, TextureFormat.DXT1, false);
yield return www;
www.LoadImageIntoTexture(tex);
Rect rec = new Rect(0, 0, GetComponent<SpriteRenderer>().bounds.size.x, GetComponent<SpriteRenderer>().bounds.size.y);
inputSprite = Sprite.Create(tex, rec, new Vector2(0, 0), 1);
*/
Texture2D[] textList = new Texture2D[allFiles.Length];
int dummy = 0;
foreach (string tstring in allFiles)
{
string pathTemp = pathPrefix + tstring;
WWW www = new WWW(pathTemp);
yield return www;
Texture2D texTmp = new Texture2D(1024, 1024, TextureFormat.DXT1, false);
www.LoadImageIntoTexture(texTmp);
textList[dummy] = texTmp;
dummy++;
inputSprite.Add(Sprite.Create(texTmp, new Rect(0, 0, spriteToBeEdited.bounds.size.x, spriteToBeEdited.bounds.size.y), Vector2.zero));
}
print("Finished");
}
}
But for some reason, the images are detected correctly in the folder along with their number. But the images are not loaded correctly. I tried just loading one image at a time to see if they are working, and that didn't work either.
Your answer
Follow this Question
Related Questions
Why don't created animations play the way you create them to play? 0 Answers
Procedural AnimationClip loop broken when using EnsureQuaternionContinuity 1 Answer
How to read sprites/textures from an Animator current animation? 0 Answers
Create custom animation via C# script at runtime? 0 Answers
How can I create animation keyframes using C# scripting? 0 Answers