如何使列表像属性一样


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AnimationCamera : MonoBehaviour
{
    public Camera animationCamera;
    public Camera mainCamera;
    Animator _anim;
    List<string> animations = new List<string>();
    private void Start()
    {
        animationCamera.enabled = false;
        mainCamera.enabled = true;
        _anim = GetComponent<Animator>();
        foreach (AnimationClip ac in _anim.runtimeAnimatorController.animationClips)
        {
            animations.Add(ac.name + " " + ac.length.ToString());
        }
        int cliptoplay = animations[0].IndexOf(" ");
        string clip = animations[0].Substring(0, cliptoplay);
    }

最后,在变量字符串剪辑中,我得到了这个名字。在列表动画中,我有每个剪辑的长度。

但我想知道如果我只在代码中输入 Visual Studio :clip,我是否可以做这样的事情。在点(剪辑(之后,我将有一个每个剪辑名称及其长度的选项列表。例如,如果我输入今天的动画。我得到一个属性列表,例如:动画。添加或动画。插入或动画。索引

我想做的是创建一些,所以如果我输入剪辑。 我将获得所有剪辑名称和长度的列表,例如:Clip.anim_001_length_10或Clip.myanim_length_21

因此,如果我想稍后使用它,将更容易找到您要使用的剪辑。

希望我理解正确,但为什么不只使用AnimationClip列表而不是进行字符串操作呢?

List<AnimationClip> animations = new List<AnimationClip>();

稍后,您可以通过创建新的AnimationClip对象并从控制器的集合中复制属性来填充它:

foreach (AnimationClip ac in _anim.runtimeAnimatorController.animationClips)
{
    animations.Add(new AnimationClip {name = ac.name, length = ac.length});
}

现在,如果您想获取所有剪辑名称的列表,您可以执行以下操作:

List<string> clipNames = animations.Select(clip => clip.name).ToList();

或者,如果您想要长度<30 的所有剪辑:

List<AnimationClip> shortClips = animations.Where(clip => clip.length < 30).ToList();

相关内容

最新更新