0
\$\begingroup\$

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.

\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

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

\$\endgroup\$
5
  • \$\begingroup\$ 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! \$\endgroup\$
    – user55609
    Commented Dec 25, 2014 at 23:48
  • \$\begingroup\$ 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. \$\endgroup\$
    – JeanLuc
    Commented Dec 26, 2014 at 22:27
  • \$\begingroup\$ 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. \$\endgroup\$
    – JeanLuc
    Commented Dec 26, 2014 at 22:34
  • \$\begingroup\$ 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. \$\endgroup\$
    – jhocking
    Commented Dec 27, 2014 at 14:11
  • \$\begingroup\$ As for scene switching, there's no mention of this in the script reference so I may be mis-remembering, but I think Resources.UnloadUnusedAssets is called automatically (and if not, you could easily call that yourself) docs.unity3d.com/ScriptReference/… \$\endgroup\$
    – jhocking
    Commented Dec 27, 2014 at 14:14
1
\$\begingroup\$

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).

\$\endgroup\$

You must log in to answer this question.