Question by
Shadowblitz16 · Feb 03, 2019 at 10:58 PM ·
animationanimatoranimationclipcustom
how do I create a custom sprite class
hello I am trying to create a custom sprite class for my 2d project
I was wondering how do I draw a animationclip without a spriterenderer or animator components attached to my game object.
I have tried creating animators, animations and animationclips inside the script privately to handle the drawing, but it seems that the animations or animationclips are always null, or i just don't know what functions to use.
this is my current script but it complains about a null reference exception, which i am guessing because I can't create new animations in script or something.
using UnityEngine;
using System.Collections.Generic;
using Texture = UnityEngine.Sprite;
namespace Sigma
{
[System.Serializable]
[ExecuteInEditMode]
public class Sprite : MonoBehaviour
{
[SerializeField]
private Animation _animation = new Animation();
[SerializeField]
private AnimationClip _animationClip;
[SerializeField]
private float _frameTime = 0;
[SerializeField]
private float _frameSpeed = 0;
[SerializeField]
private WrapMode _wrapMode = WrapMode.Default;
public AnimationClip animationClip
{
get
{
return _animationClip;
}
set
{
_animationClip = value;
if (_animation.GetClip("default") != null)
_animation.RemoveClip("default");
_animation.AddClip(value, "default");
_animation.Play("default");
}
}
public float frameTime
{
get
{
return _frameTime;
}
set
{
_frameTime = value;
_animation["default"].time = value;
}
}
public float frameSpeed
{
get
{
return _frameSpeed;
}
set
{
_frameSpeed = value;
_animation["default"].speed = value;
}
}
public WrapMode wrapMode
{
get
{
return _wrapMode;
}
set
{
_wrapMode = value;
_animation["default"].wrapMode = value;
}
}
public bool IsPlaying()
{
return _animation.IsPlaying("default");
}
void OnValidate()
{
_animation = new Animation();
animationClip = _animationClip;
frameTime = _frameTime;
frameSpeed = _frameSpeed;
wrapMode = _wrapMode;
}
void Start()
{
_animation.playAutomatically = true;
}
}
}
I honestly don't know the difference between a animator, animation and animationclip. my guess would be that animation is just another way to do the animator.
Comment