Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Can't load sprites in editor via code.

Discussion in 'Scripting' started by BabouDev, Oct 3, 2018.

  1. BabouDev

    BabouDev

    Joined:
    Feb 3, 2015
    Posts:
    12
    So basically I have a lot of art assets and I'm trying to do all the grudge work in code so that I can control it very easily in the editor using my own widgets.

    I got most of it up except I can't seem to create a sprite in code.

    Code (CSharp):
    1.  
    2.                 foreach ( var MoveSet in MoveToPngsMap)
    3.                 {
    4.                     List<Sprite> SpriteList = new  List<Sprite>();
    5.  
    6.                     foreach (var SpecificMove in MoveSet.Value)
    7.                     {
    8.                         byte[] fileData = File.ReadAllBytes(SpecificMove);
    9.                         Texture2D texture2D = new Texture2D(2, 2);
    10.                         texture2D.LoadImage(fileData);
    11.                         texture2D.name = MoveSet.Key;
    12.                         Sprite CurrSprite = Sprite.Create(texture2D, new Rect(0, 0, texture2D.width, texture2D.height), new Vector2(1.0f, 1.0f));
    13.                         CurrSprite.name = MoveSet.Key;
    14.                         SpriteList.Add(CurrSprite);
    15.                     }
    16.  
    17.                     string MoveFolderString = CharFolderString + "/" + MoveSet.Key;
    18.                     string AnimationClipPath = MoveFolderString + "/" + CharName + "_" + MoveSet.Key + ".anim";
    19.  
    20.                     AnimationClip animclip = AssetDatabase.LoadAssetAtPath<AnimationClip>(AnimationClipPath);
    21.  
    22.                     if (animclip == null)
    23.                     {
    24.                         AnimationClip animClip = new AnimationClip();
    25.                         animClip.frameRate = 25;
    26.                         animClip.name = MoveSet.Key;
    27.  
    28.                         EditorCurveBinding spriteBinding = new EditorCurveBinding();
    29.                         spriteBinding.type = typeof(SpriteRenderer);
    30.                         spriteBinding.path = "";
    31.                         spriteBinding.propertyName = "m_Sprite";// + MoveSet.Key;
    32.  
    33.                         ObjectReferenceKeyframe[] spriteKeyFrames = new ObjectReferenceKeyframe[SpriteList.Count];
    34.                         for (int i_spr = 0; i_spr < (SpriteList.Count); i_spr++)
    35.                         {
    36.                             spriteKeyFrames[i_spr] = new ObjectReferenceKeyframe();
    37.                             spriteKeyFrames[i_spr].time = i_spr;
    38.                             spriteKeyFrames[i_spr].value = SpriteList[i_spr];
    39.                         }
    40.                         AnimationUtility.SetObjectReferenceCurve(animClip, spriteBinding, spriteKeyFrames);
    41.  
    42.                         Directory.CreateDirectory(MoveFolderString);
    43.                         AssetDatabase.CreateAsset(animClip, AnimationClipPath);
    44.  
    45.                         //AssetDatabase.AddObjectToAsset(animClip, CharAnimController);
    46.                         AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(animClip));
    47.                         CharAnimController.AddMotion(animClip);
    48.                     }
    49.  
    50.                     AssetDatabase.SaveAssets();
    51.                     AssetDatabase.Refresh();
    52.                 }
    My animation controller contains all the animation clips and the key frames are all there. But the sprites are blank. No matter what I do I can't seem to get the sprites to appear. Anybody has any idea what I'm doing wrong.



    A few trivial questions

    1) If I create animationcontroller with animationclips programatically, can I then assign this animation controller during runtime and swap as necessary?

    2) From what I know, sprites will be packed during the build phase. Since I'm loading my sprites as individual images. How can i tell unity to pack the images into specific spritesheets for each character. Or will it just jumble up all the sprites and do the best case scenario (totally fine by me)

    3) What should be the names of the Texture2D and Sprite. Can they have duplicate names? Must they be unique? I tried many different things but the sprites simply won't come up.

    4) What is the difference between doing what I did above and resources.load<sprite>?

    Thank you all very much for helping.

    edit : I should mention, this extends the editor. It's done using a window in the editor.
     
    Last edited: Oct 3, 2018
  2. BabouDev

    BabouDev

    Joined:
    Feb 3, 2015
    Posts:
    12
    Solved by using
    Code (CSharp):
    1. Sprite CurrSprite = AssetDatabase.LoadAssetAtPath<Sprite>(RelativePathToImg);
    instead of manually creating it.

    Any idea why my code wasn't working regardless? Also would appreciate it if anyone could answer my trivial questions. Would save me a lot of time digging around. Thanks a bunch!

    I'm mainly concerned if my sprites will be packed and how I can control them. Thanks