Skip to main content

using UnityEngine; using System.Collections;

/**

  • Animate a mesh by cycling through different meshes.

  • @author bummzack */ public class MeshAnimation : MonoBehaviour {

    public Mesh[] Meshes; public bool Loop; public float FrameDuration;

    private int _index; private bool _playing; private float _accumulator; private MeshFilter _meshFilter;

    public void Start() { _meshFilter = GetComponent(); _index = 0; }

    public void Update() { if(!_playing){ return; }

     _accumulator += Time.deltaTime;
    
     if(_accumulator >= FrameDuration){
         _accumulator -= FrameDuration;
         _index = (_index + 1) % Meshes.Length;
    
         if(_index == 0 && !Loop){
             Stop();
             return;
         }
    
         _meshFilter.mesh = Meshes[_index];
     }
    

    }

    // play the animation public void Play() { _playing = true; }

    // stop the animation public void Stop() { _playing = false; }

    // restore the first frame public void Reset() { _index = 0; _accumulator = 0.0f; _meshFilter.mesh = Meshes[_index]; }

    // Mouse down to toggle Stop/Play, just for testing public void OnMouseDown() { if(_playing){ Stop(); Reset(); } else { Play(); } } }

using UnityEngine;
using System.Collections;

/**
 * Animate a mesh by cycling through different meshes.
 * @author bummzack
 */
public class MeshAnimation : MonoBehaviour {
    
    public Mesh[] Meshes;
    public bool Loop;
    public float FrameDuration;
    
    private int _index;
    private bool _playing;
    private float _accumulator;
    private MeshFilter _meshFilter;
    
    public void Start() 
    {
        _meshFilter = GetComponent<MeshFilter>();
        _index = 0;
    }
    
    public void Update()
    {
        if(!_playing){
            return;
        }
    
        _accumulator += Time.deltaTime;
        
        if(_accumulator >= FrameDuration){
            _accumulator -= FrameDuration;
            _index = (_index + 1) % Meshes.Length;
            
            if(_index == 0 && !Loop){
                Stop();
                return;
            }
            
            _meshFilter.mesh = Meshes[_index];
        }
    }
    
    // play the animation
    public void Play()
    {
        _playing = true;
    }
    
    // stop the animation
    public void Stop()
    {
        _playing = false;
    }
    
    // restore the first frame
    public void Reset()
    {
        _index = 0;
        _accumulator = 0.0f;
        _meshFilter.mesh = Meshes[_index];
    }
    
    // Mouse down to toggle Stop/Play, just for testing
    public void OnMouseDown()
    {
        if(_playing){
            Stop();
            Reset();
        } else {
            Play();
        }
    }
}

using UnityEngine; using System.Collections;

/**

  • Animate a mesh by cycling through different meshes.

  • @author bummzack */ public class MeshAnimation : MonoBehaviour {

    public Mesh[] Meshes; public bool Loop; public float FrameDuration;

    private int _index; private bool _playing; private float _accumulator; private MeshFilter _meshFilter;

    public void Start() { _meshFilter = GetComponent(); _index = 0; }

    public void Update() { if(!_playing){ return; }

     _accumulator += Time.deltaTime;
    
     if(_accumulator >= FrameDuration){
         _accumulator -= FrameDuration;
         _index = (_index + 1) % Meshes.Length;
    
         if(_index == 0 && !Loop){
             Stop();
             return;
         }
    
         _meshFilter.mesh = Meshes[_index];
     }
    

    }

    // play the animation public void Play() { _playing = true; }

    // stop the animation public void Stop() { _playing = false; }

    // restore the first frame public void Reset() { _index = 0; _accumulator = 0.0f; _meshFilter.mesh = Meshes[_index]; }

    // Mouse down to toggle Stop/Play, just for testing public void OnMouseDown() { if(_playing){ Stop(); Reset(); } else { Play(); } } }

using UnityEngine;
using System.Collections;

/**
 * Animate a mesh by cycling through different meshes.
 * @author bummzack
 */
public class MeshAnimation : MonoBehaviour {
    
    public Mesh[] Meshes;
    public bool Loop;
    public float FrameDuration;
    
    private int _index;
    private bool _playing;
    private float _accumulator;
    private MeshFilter _meshFilter;
    
    public void Start() 
    {
        _meshFilter = GetComponent<MeshFilter>();
        _index = 0;
    }
    
    public void Update()
    {
        if(!_playing){
            return;
        }
    
        _accumulator += Time.deltaTime;
        
        if(_accumulator >= FrameDuration){
            _accumulator -= FrameDuration;
            _index = (_index + 1) % Meshes.Length;
            
            if(_index == 0 && !Loop){
                Stop();
                return;
            }
            
            _meshFilter.mesh = Meshes[_index];
        }
    }
    
    // play the animation
    public void Play()
    {
        _playing = true;
    }
    
    // stop the animation
    public void Stop()
    {
        _playing = false;
    }
    
    // restore the first frame
    public void Reset()
    {
        _index = 0;
        _accumulator = 0.0f;
        _meshFilter.mesh = Meshes[_index];
    }
    
    // Mouse down to toggle Stop/Play, just for testing
    public void OnMouseDown()
    {
        if(_playing){
            Stop();
            Reset();
        } else {
            Play();
        }
    }
}
Source Link
user55609
user55609

how can I put an array of objects into a script?

I want to create an animation from an object sequence. I already achieved exactly what I needed with the "MeshAnimaton script" from bummzack:

using UnityEngine; using System.Collections;

/**

  • Animate a mesh by cycling through different meshes.

  • @author bummzack */ public class MeshAnimation : MonoBehaviour {

    public Mesh[] Meshes; public bool Loop; public float FrameDuration;

    private int _index; private bool _playing; private float _accumulator; private MeshFilter _meshFilter;

    public void Start() { _meshFilter = GetComponent(); _index = 0; }

    public void Update() { if(!_playing){ return; }

     _accumulator += Time.deltaTime;
    
     if(_accumulator >= FrameDuration){
         _accumulator -= FrameDuration;
         _index = (_index + 1) % Meshes.Length;
    
         if(_index == 0 && !Loop){
             Stop();
             return;
         }
    
         _meshFilter.mesh = Meshes[_index];
     }
    

    }

    // play the animation public void Play() { _playing = true; }

    // stop the animation public void Stop() { _playing = false; }

    // restore the first frame public void Reset() { _index = 0; _accumulator = 0.0f; _meshFilter.mesh = Meshes[_index]; }

    // Mouse down to toggle Stop/Play, just for testing public void OnMouseDown() { if(_playing){ Stop(); Reset(); } else { Play(); } } }

That worked fine with an object sequence which consisted of 20 meshes. But now I have to do the same with sequences that consist of big amounts of objects. Is there a way to put an array of certain object names into the script instead of dragging each mesh file separately onto the script component? I would appreciate any kind of help.