fork(1) download
  1. using UnityEditor;
  2. using UnityEngine;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5.  
  6. public class Animation2DGenerator : MonoBehaviour {
  7. public Texture2D texture;
  8. public Sprite sprite;
  9. public Animation myAnimation;
  10.  
  11. // Use this for initialization
  12. void Start () {
  13. int frameWidth = 255, frameHeight = 255;
  14. string[] pngPaths = new string[] {
  15. "Assets/Animation2DGenerator/TestData/AnimFrame1.png",
  16. "Assets/Animation2DGenerator/TestData/AnimFrame2.png",
  17. "Assets/Animation2DGenerator/TestData/AnimFrame3.png" };
  18. texture = new Texture2D(frameWidth, frameHeight);
  19.  
  20. Sprite[] sprites = new Sprite[pngPaths.Length];
  21. int[] durations = new int[pngPaths.Length];
  22. for (int i = 0; i < pngPaths.Length;i++)
  23. {
  24. texture.LoadImage(System.IO.File.ReadAllBytes(pngPaths[i]));
  25. texture.filterMode = FilterMode.Point;
  26. sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0, 0), 100f);
  27. sprites[i] = sprite;
  28. durations[i] = 30 * (i + 1);
  29. }
  30. // sprite のテスト。 3 が表示された。
  31. SpriteRenderer sr = gameObject.AddComponent<SpriteRenderer>();
  32. sr.sprite = sprite;
  33.  
  34. // animation
  35. AnimationClip animClip = new AnimationClip();
  36. animClip.frameRate = 12f;
  37. animClip.wrapMode = WrapMode.Loop;
  38.  
  39. // First you need to create e Editor Curve Binding
  40. EditorCurveBinding curveBinding = new EditorCurveBinding();
  41.  
  42. // I want to change the sprites of the sprite renderer, so I put the typeof(SpriteRenderer) as the binding type.
  43. curveBinding.type = typeof(SpriteRenderer);
  44. // Regular path to the gameobject that will be changed (empty string means root)
  45. curveBinding.path = "";
  46. // This is the property name to change the sprite of a sprite renderer
  47. curveBinding.propertyName = "m_Sprite";
  48.  
  49. // An array to hold the object keyframes
  50. ObjectReferenceKeyframe[] keyFrames = new ObjectReferenceKeyframe[pngPaths.Length];
  51.  
  52. for (int i = 0; i < pngPaths.Length; i++ )
  53. {
  54. keyFrames[i] = new ObjectReferenceKeyframe();
  55. // set the time
  56. keyFrames[i].time = durations[i];
  57. // set reference for the sprite you want
  58.  
  59. // tileid は 0 から始まるので gid にするために 1 加算する。
  60. keyFrames[i].value = sprites[i];
  61. }
  62. AnimationUtility.SetAnimationType(animClip, ModelImporterAnimationType.Generic);
  63. AnimationUtility.SetObjectReferenceCurve(animClip, curveBinding, keyFrames);
  64.  
  65. AssetDatabase.CreateAsset(animClip, "Assets/Animation2DGenerator/TestData/anim1.anim");
  66. EditorUtility.SetDirty(animClip);
  67.  
  68. // animator コンポーネントを設定
  69. myAnimation = gameObject.AddComponent<Animation>();
  70. myAnimation.AddClip(animClip, "anim1");
  71. myAnimation.Play("anim1");
  72. myAnimation.wrapMode = WrapMode.Loop;
  73. myAnimation.transform.position = new Vector3(-2, 0, -1);
  74.  
  75. }
  76.  
  77. // Update is called once per frame
  78. void Update () {
  79.  
  80. }
  81. }
  82.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cs(1,7): error CS0246: The type or namespace name `UnityEditor' could not be found. Are you missing an assembly reference?
prog.cs(2,7): error CS0246: The type or namespace name `UnityEngine' could not be found. Are you missing an assembly reference?
prog.cs(6,37): error CS0246: The type or namespace name `MonoBehaviour' could not be found. Are you missing an assembly reference?
prog.cs(7,12): error CS0246: The type or namespace name `Texture2D' could not be found. Are you missing an assembly reference?
prog.cs(8,12): error CS0246: The type or namespace name `Sprite' could not be found. Are you missing an assembly reference?
prog.cs(9,12): error CS0246: The type or namespace name `Animation' could not be found. Are you missing an assembly reference?
Compilation failed: 6 error(s), 0 warnings
stdout
Standard output is empty