Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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<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();
        }
    }
}

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.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

In the Unity Editor can select multiple files in the project view and drag them on the array in the inspector. Make sure the inspector is locked on the game object, so that it does not lose focus.

enter image description here

share|improve this answer
    
Thanks JeanLuc, that makes it definitely much easier! You really helped me to save a lot of time. And also thanks jhocking, that's a helpful information to write some code that would work without any dragging. I'm relatively new to programming but I'm gonna try to build in the Resources commands. Anyway thanks again guys! –  anyway Dec 25 '14 at 23:48
    
Yes, but be aware when using @jhocking approach with Resources.Load, if you don’t need the resource (i.e. your mesh) anymore, you should unload it by calling Resources.UnloadAsset, otherwise you might run into OutOfMemory issues, where on Mobile Platforms you app will crash and on PCs it will behave badly. –  JeanLuc Dec 26 '14 at 22:27
    
Oh and please accept an answer that helped you to fix your problem. Read also this. –  JeanLuc Dec 26 '14 at 22:31
    
If you use Unity Serialization with public variables or the attribute SerializeField in the Unity Editor Inspector, then the Unity Engine will handle the resource loading and unloading, when you load another scene. –  JeanLuc Dec 26 '14 at 22:34
    
Similar to what he said about UnloadAsset, when you link meshes directly then they are all loaded. I forget if not unloading resources will cause a memory leak or if it will reference the already loaded asset (you'd need to test it) but if the latter then the worse case is it uses up as much memory as loading all the meshes. –  jhocking Dec 27 '14 at 14:11

You need to use Resources.Load() in order to load objects by name. Basically, put your model in a folder called "Resources" and then they can be loaded using the Resources commands. If the models aren't in a "Resources" folder then the only way Unity would know about those assets is to link directly as an array of meshes (ie. what you're already doing and want to change).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.