简体   繁体   English

我怎样才能使列表像属性一样?

[英]How can i make a List to be like properties?

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

    }

In the end in the variable string clip i'm getting the name. 最后,在可变字符串剪辑中,我得到了名称。 And in the List animations i have the length of each clip. 在列表动画中,我具有每个剪辑的长度。

But i wonder if i could do something like that if i will type in visual studio in the code only: clip. 但是我想知道如果我只在代码中键入visual studio是否可以做这样的事情:剪辑。 And after the point(clip.) i will have a list of options of each clip name and it's length. 在point(clip。)之后,我将列出每个剪辑名称及其长度的选项列表。 For example if i type today animations. 例如,如果我键入今天的动画。 i get a list of properties like: animations.Add or animations.Insert or animations.IndexOf 我得到的属性列表如下:animations.Add或animations.Insert或animations.IndexOf

What i want to do is to create some so if i will type Clip. 我想做的就是创建一些,如果我要键入Clip。 i will get a list of all clips names and the length for example: Clip.anim_001_length_10 or Clip.myanim_length_21 我将获得所有剪辑名称和长度的列表,例如:Clip.anim_001_length_10或Clip.myanim_length_21

So if i want to use it later it will be easier to find the clip you want to use. 因此,如果我以后要使用它,找到要使用的剪辑会更容易。

Hopefully I understood you correctly, but why not just use a list of AnimationClip instead of doing the string manipulation? 希望我正确地理解了您,但是为什么不只使用AnimationClip列表而不是进行字符串操作呢?

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

Then later you can populate it by creating new AnimationClip objects and copying the properties from the controller's collection: 然后,您可以通过创建新的AnimationClip对象并复制控制器集合中的属性来填充它:

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

Now if you want to get a list of all clip names, you can do something like: 现在,如果要获取所有剪辑名称的列表,则可以执行以下操作:

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

Or if you want all clips with a length < 30: 或者,如果您希望所有长度小于30的剪辑:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM