Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by officialbabblebytes · Oct 03, 2017 at 05:01 AM · texture2danimationclipanimationcurveurldirectory

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:

  1. A function called "importspritesfromDirectory" checks for any png files in specified directory. and it calls a coruoutine function called "Uploadingimages"

  2. 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.

  3. 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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

120 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges