I'm looking for a way to store variables of same type in an array, and access them by their names. Like in PHP you do:
我正在寻找一种方法来在数组中存储相同类型的变量,并通过它们的名称访问它们。就像在PHP中一样:
$array = array();
$array['first member']=$var1;
$array['another member']=$var2;
// so on
I want to do a similar thing in Unity3D's JavaScript. I have AudioClip variables, currently used like this:
我想在Unity3D的JavaScript中做类似的事情。我有AudioClip变量,目前使用如下:
#pragma strict
var Audio_Goal:AudioClip;
var Audio_Miss:AudioClip;
var Audio_Saved:AudioClip;
function PlaySound_Goal()
{
audio.PlayOneShot(Audio_Goal);
}
function PlaySound_Miss()
{
audio.PlayOneShot(Audio_Miss);
}
of course this requires one function per audioclip, which is ugly.
当然,这需要每个audioclip一个功能,这是丑陋的。
I want to do it like this:
我想这样做:
function PlayAudio(audioname:String)
{
audio.PlayOneShot(AudioArray[audioname]);
}
How can I do this?
我怎样才能做到这一点?
3 个解决方案
#1
4
You need to use something like a dictionary:
你需要使用类似字典的东西:
#pragma strict
import System.Collections.Generic;
var AudioArray = new Dictionary.<string,AudioClip >();
// add to dict
AudioArray["one"] = AudioClip.Create(/**/); //see AudioClip.Create(...) for arguments
// play from dict
audio.clip = AudioArray["one"];
audio.Play();
#2
0
You can do something like:
你可以这样做:
var myAudioClips: AudioClip[];
function Start() {
// Your stuff...
}
function PlayAudio(audioname:String)
{
for (var myClip in myAudioClips) {
if (audioname.Equals(myClip.name)) {
audio.PlayOneShot(myClip);
}
}
}
And then add all of your clips in the array by dragging them using the editor.
然后使用编辑器拖动它们,将所有剪辑添加到数组中。
#3
0
You should be able to use Enumerable.Zip>, but because it's not available in UnityScript, I think this is the best you can do:
您应该能够使用Enumerable.Zip>,但由于它在UnityScript中不可用,我认为这是您可以做的最好的:
var names : List.<String>;
var clips : List.<AudioClip>;
var keyFunc = function(kvp : KeyValuePair.<String, AudioClip>) kvp.Key;
var valueFunc = function(kvp : KeyValuePair.<String, AudioClip>) kvp.Value;
var dictionary = names.Select(
function(name, index) new KeyValuePair.<String, AudioClip>(name, clips[index])
).ToDictionary(keyFunc, valueFunc);
I don't know why you need to store the Funcs. This code compiles, but won't run:
我不知道为什么你需要存储Funcs。此代码编译,但不会运行:
var dictionary = names.Select(
function(name, index) new KeyValuePair.<String, AudioClip>(name, clips[index])
).ToDictionary(function(kvp) kvp.Key, function(kvp) kvp.Value);
Report it as a bug if you want. I don't want to, because I don't think the devs need to waste any more time on this language.
如果您愿意,请将其报告为错误。我不想,因为我认为开发者不再需要浪费这种语言。
#1
4
You need to use something like a dictionary:
你需要使用类似字典的东西:
#pragma strict
import System.Collections.Generic;
var AudioArray = new Dictionary.<string,AudioClip >();
// add to dict
AudioArray["one"] = AudioClip.Create(/**/); //see AudioClip.Create(...) for arguments
// play from dict
audio.clip = AudioArray["one"];
audio.Play();
#2
0
You can do something like:
你可以这样做:
var myAudioClips: AudioClip[];
function Start() {
// Your stuff...
}
function PlayAudio(audioname:String)
{
for (var myClip in myAudioClips) {
if (audioname.Equals(myClip.name)) {
audio.PlayOneShot(myClip);
}
}
}
And then add all of your clips in the array by dragging them using the editor.
然后使用编辑器拖动它们,将所有剪辑添加到数组中。
#3
0
You should be able to use Enumerable.Zip>, but because it's not available in UnityScript, I think this is the best you can do:
您应该能够使用Enumerable.Zip>,但由于它在UnityScript中不可用,我认为这是您可以做的最好的:
var names : List.<String>;
var clips : List.<AudioClip>;
var keyFunc = function(kvp : KeyValuePair.<String, AudioClip>) kvp.Key;
var valueFunc = function(kvp : KeyValuePair.<String, AudioClip>) kvp.Value;
var dictionary = names.Select(
function(name, index) new KeyValuePair.<String, AudioClip>(name, clips[index])
).ToDictionary(keyFunc, valueFunc);
I don't know why you need to store the Funcs. This code compiles, but won't run:
我不知道为什么你需要存储Funcs。此代码编译,但不会运行:
var dictionary = names.Select(
function(name, index) new KeyValuePair.<String, AudioClip>(name, clips[index])
).ToDictionary(function(kvp) kvp.Key, function(kvp) kvp.Value);
Report it as a bug if you want. I don't want to, because I don't think the devs need to waste any more time on this language.
如果您愿意,请将其报告为错误。我不想,因为我认为开发者不再需要浪费这种语言。