how to see list of strings in Inspector?

I would have thought this would work:

public List<string> myStrings;

But it doesn’t.

Why not? How to amend so that these show up in the Inspector?

Have also tried with an Array, it also doesn’t show its contents in the Inspector:

using UnityEditor;
using UnityEngine;

public class AnimControlNames : MonoBehaviour {

    [SerializeField] private Animation _anim;
    [SerializeField] private AnimationClip[] _clips;
    [SerializeField] private int _clipCount;
    [SerializeField] private int clipToRun;
    public string[] _clipNames;

    void Start ( ) {
        if ( _anim == null ) _anim = GetComponent<Animation>(  );
        _clipCount = _anim.GetClipCount( );
        _clips = new AnimationClip[_clipCount];
        _clips = AnimationUtility.GetAnimationClips( _anim.gameObject );
        _clipNames = new string[_clipCount];
        for ( int i = 0; i < _clips.Length; i++ ) {
            _clipNames[i] = _clips[i].name;
        }
    }
}

Check you don’t have any compiler errors in your project, they need to be fixed first. Otherwise, check that you don’t have a custom editor for your type.

Also that’s some very claustrophobic code.

And I’ll also note the UnityEditor stuff can’t be used in a built project.

2 Likes

It was a bug built up through usage… if I clicked on debug mode for inspector… the contents of the Arrays and Lists would show up. If I then unclicked/returned to normal mode, the list and array content would continue to show… but only AFTER I’d done this initial reveal through the use of the debug mode.

A restart of Unity and I no longer need to trigger debug mode of the inspector for the contents of the array and list (elements) to be shown…

Yes, claustrophobic code. I don’t like wide open spaces, only eating the cows that graze on them.

This is a very good point. And reminder. Thank you!!!

Is there any way to get at these names of an Animation that’ll work in a build?

You’ll want to make a custom editor for your type, use that to draw the default inspector and then add a button that does the task on the editor side of things.

So we can cut your monobehaviour down to this:

using UnityEngine;

public class AnimControlName : MonoBehaviour
{
    #region Inspector Fields

    [SerializeField]
    private Animation _anim;
 
    public AnimationClip[] _clips;

    [SerializeField]
    private int clipToRun;

    public string[] _clipNames;

    #endregion

    #region Properties

    public Animation Anim => _anim;

    public AnimationClip[] Clips => _clips;

    public int ClipCount => _anim.GetClipCount();

    #endregion
}

(I removed the clip count component as it could just be a property)

And then we make a custom inspector:

#if UNITY_EDITOR
using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(AnimControlName))]
public class AnimControlNameEditor : Editor
{
   private AnimControlName animControlName;

   private void OnEnable()
   {
       animControlName = (AnimControlName)this.target;
   }

   public override void OnInspectorGUI()
   {
       base.OnInspectorGUI();

       if (GUILayout.Button("Find Clip Names"))
       {
           animControlName._clips = AnimationUtility.GetAnimationClips(animControlName.gameObject);

           animControlName._clips = new AnimationClip[animControlName.ClipCount];

           for (int i = 0; i < animControlName._clips.Length; i++)
           {
               animControlName._clipNames[i] = animControlName._clips[i].name;
           }

           EditorUtility.SetDirty(target);
       }
   }
}
#endif

Which gives us this:
8748165--1185033--upload_2023-1-22_21-26-54.png

Didn’t actually test if it works as I don’t have random animations on hand to test it with. But you should get the idea. In practice you’d also use SerializedProperty’s to adjust the values of the component rather than just making them public, but that can be your homework.

Sorry, how does this solve the problem of UnityEditor features (like AnimationUtility) only being available in build being the limitation on getting at the animation clips in their array? As far as I can tell, this still wouldn’t work in a build.

It doesn’t need to… see how Spiney guarded the entire thing with #if UNITY_EDITOR ? It all goes away at build time. It’s an editor-only piece of code.

Mmm… steak!

If your code is too compact then bugs in one part can leap from one part and infest another part.

If you have more than one or two dots (.) in a single statement, you’re just being mean to yourself.

How to break down hairy lines of code:

http://plbm.com/?p=248

Break it up, practice social distancing in your code, one thing per line please.

“Programming is hard enough without making it harder for ourselves.” - angrypenguin on Unity3D forums

“Combining a bunch of stuff into one line always feels satisfying, but it’s always a PITA to debug.” - StarManta on the Unity3D forums

Perhaps I wasn’t clear in the question. It wasn’t how to avoid this usage of an Editor feature being in a Build, it’s how do I access the Animations array (the array of clips) in a similar manner in a build without reliance on these Editor helper facilities? I suspect there’s no way to do it. But don’t know that for sure.

On the code… I disagree that my code was claustrophobic or otherwise confining. And, for me, it’s got a lot of visual cues that help me see what is what, and find what I’m looking for, fast.

But I still want steak, almost everyday. And pork. And especially free range chickens.

1 Like

The point is to gather all the data you need outside of runtime and have it ready to use in build, so that you don’t need to use it in runtime.

Your Start callback was just caching the data needed, so instead we’re caching it through the editor. Same result, different means.

It’s pretty common practice to use Editor tools to pre-ready data that would either be costly to do at run time, or - as you’re experiencing - unavailable at run time.