I want to define a bunch of pairs of strings. For each pair, I want to perform something. This sounds like the job for a jagged array, but apparently you can’t define that in the Inspector.
Is there a good way to do this without having to define classes?
I’m not sure if those solutions could be helpful. Here’s what I have at this point, which is not bad, but like I said, I think I should be able to do it without classes. But there’s no way I’m not using the Editor to input values!
This is something I attach to whatever game object, in order to be able to input/store the names in the Editor. I would like for pairs to be a string[ ][ ], not a Pair[ ].
using UnityEngine;
public class _in_editor_setup : MonoBehaviour {
public Pair[] pairs;
[System.Serializable]
public class Pair
{
public string nameA;
public string nameB;
}
}
This is a script that will be attached to a prefab that gets instantiated if any objects in an FBX file match the name of the prefab. Hence, because I will be reimporting that FBX file countless times, there’s no way I’m going to reassign all the “_linkedThing” variables. In my current game, that’s about 10 variables or so, and they’re buried deep in a hierarchy.
using UnityEngine;
public class _thing : MonoBehaviour {
public Transform _linkedThing;
}
Finally, here’s a menu item that automatically assigns the variable appropriately.
using UnityEngine;
using UnityEditor;
class _setup {
[ MenuItem("Custom/Setup") ]
static void Setup ()
{
_in_editor_setup inEditorSetup = Object.FindObjectOfType(typeof(_in_editor_setup) ) as _in_editor_setup;
foreach (_in_editor_setup.Pair pair in inEditorSetup.pairs)
{
GameObject GameObjectA = GameObject.Find(pair.nameA);
GameObject GameObjectB = GameObject.Find(pair.nameB);
if (GameObjectA GameObjectB)
{
_thing ScriptA = (GameObjectA.GetComponentInChildren(
typeof(_thing) ) as _thing);
_thing ScriptB = (GameObjectB.GetComponentInChildren(
typeof(_thing) ) as _thing);
Transform thingA = ScriptA.transform;
Transform thingB = ScriptB.transform;
ScriptA.linkedThing = thingB;
ScriptB.linkedThing = thingA;
}
}
}
}
(Also, I’ve started using underscores for class names like that, because I’m not dealing with Unity lowercasing all the stuff in my Assets folder anymore.)
It’s a bit low-tech, but it is common in programming to use alternate elements of a string array for pairs, say for key/value combinations. I’ve found this kind of thing quite handy for editor scripts, even though it is untidy and slightly error-prone.
Thanks, andeeee. I considered that, and due to the fact that all the pairs are going to have names like “Thing 1A”, “Thing 1B”, “Thing 2A”, “Thing 2B”, etc., it’s probably not a bad solution.